I'm facing one annoying problema and I would like some help.
This is the situation.
CREATE TABLE tree_hierarchy (
id NUMBER (20)
,parent_id NUMBER (20)
);
CREATE TABLE tree_information (
id NUMBER (20)
,some_text VARCHAR(20)
,tree_id NUMBER (20)
);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (2, null);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (4, 2);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (9, 4);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (20, null);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (40, 20);
INSERT INTO tree_hierarchy (id, parent_id) VALUES (90, 40);
INSERT INTO tree_information (id, some_text, tree_id) VALUES (10,'Some teste', 2);
INSERT INTO tree_information (id, some_text, tree_id) VALUES (11,'Other tree', 20);
And i would like to do something like this.
SELECT hier.*
FROM tree_information Ti
JOIN (
SELECT
id,
parent_id
FROM tree_hierarchy th
where connect_by_isleaf = 1
START WITH th.id = ti.tree_id
CONNECT BY PRIOR th.id = th.parent_id
) hier on 1=1;
but ti.tree_id is not visible inside the select.
If I change the start with condition for
START WITH th.parent_id is null
Will stay wrong.
Someone has idea how to solve this situation ?
I would appreciate if you provide expected result explicitly.
My best guess is:
SELECT hier.*, ti.*
FROM tree_information Ti
JOIN (
SELECT
id,
parent_id,
connect_by_root th.id as tree_id
FROM tree_hierarchy th
where connect_by_isleaf = 1
START WITH th.id in ( select tii.tree_id from tree_information Tii)
CONNECT BY PRIOR th.id = th.parent_id
) hier on ti.tree_id = hier.tree_id;
ID PARENT_ID TREE_ID ID SOME_TEXT TREE_ID
9 4 2 10 Some teste 2
90 40 20 11 Other tree 20
I am looking into merge statements, but I can not figure out the syntax.
Let's say I have these tables
CREATE TABLE employee (
employee_id NUMBER(5),
first_name VARCHAR2(20),
last_name VARCHAR2(20),
dept_no NUMBER(2),
salary NUMBER(10));
INSERT INTO employee VALUES (1, 'Dan', 'Morgan', 10, 100000);
INSERT INTO employee VALUES (2, 'Helen', 'Lofstrom', 20, 100000);
INSERT INTO employee VALUES (3, 'Akiko', 'Toyota', 20, 50000);
INSERT INTO employee VALUES (4, 'Jackie', 'Stough', 20, 40000);
INSERT INTO employee VALUES (5, 'Richard', 'Foote', 20, 70000);
INSERT INTO employee VALUES (6, 'Joe', 'Johnson', 20, 30000);
INSERT INTO employee VALUES (7, 'Clark', 'Urling', 20, 90000);
CREATE TABLE bonuses (
employee_id NUMBER, bonus NUMBER DEFAULT 100);
INSERT INTO bonuses (employee_id) VALUES (1);
INSERT INTO bonuses (employee_id) VALUES (2);
INSERT INTO bonuses (employee_id) VALUES (4);
INSERT INTO bonuses (employee_id) VALUES (6);
INSERT INTO bonuses (employee_id) VALUES (7);
COMMIT;
and just for the sake of an example I want to update all entries of bonuses with 200 * employee_id. Here is my statement. What's wrong with it?
merge into bonuses b
using
(select employee_id id, 200 bonus from employees) test
on (test.id = b.employee_id)
when matched then update set
b.bonus = test.bonus * test.employee_id
Thanks!
P.S. See also my sqlfiddle here: http://sqlfiddle.com/#!4/ff425/14
merge into bonuses b
using
(select employee_id id, 200 bonus from employee) test
on (test.id = b.employee_id)
when matched then update set
b.bonus = test.bonus * test.id;
Two minor mistakes:
1) employee instead of employees
2) test.id instead of test.employee_id
Hi I am new to Oracle and forgot how to enter a date. I have tried many variations and can't seem to get it, plus I have looked on the internet but can't find the correct example. My table is set up like this: hiredate DATE, (DATE being I assume the default input, but what is the default).
Thanks for any help.
Here's my code:
SQL> SET LINESIZE 120
SQL> SET PAGESIZE 45
SQL> SET FEEDBACK 1
SQL> SET ECHO ON
SQL>
SQL> DROP TABLE employee
2
SQL> CREAT TABLE student
SP2-0734: unknown command beginning "CREAT TABL..." - rest of line ignored.
SQL> CREATE TABLE employee
2 (
3 empno NUMBER(4)CONSTRAINT employee_empno_PK PRIMARY KEY,
4 empname VARCHAR(10),
5 job VARCHAR(10),
6 manager NUMBER(4),
7 hiredate DATE,
8 salary NUMBER(7,2),
9 commission NUMBER(7,2),
10 deptno NUMBER(2)
11 );
Table created.
SQL> INSERT INTO employee
2 VALUES(7839, 'President', NULL, 11/17/1981, 5000, NULL, 10);
INSERT INTO employee
*
ERROR at line 1:
ORA-00947: not enough values
SQL> VALUES(7839, 'President', NULL, '11/17/1978', 5000, NULL, 10);
SP2-0734: unknown command beginning "VALUES(783..." - rest of line ignored.
SQL> INSERT INTO employee
2 VALUES(7839, 'President', NULL, '11/17/1978', 5000, NULL, 10);
INSERT INTO employee
*
ERROR at line 1:
ORA-00947: not enough values
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, 11/17/1981, 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, 11/17/1981, 5000, NULL, 10)
*
ERROR at line 2:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, 19811117, 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, 19811117, 5000, NULL, 10)
*
ERROR at line 2:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '11/17/1981', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '11/17/1981', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '1978/11/17
3 )
4 ;
ERROR:
ORA-01756: quoted string not properly terminated
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President' NULL, '1978/11/17', 5000, NULL, 10);
VALUES(7839, 'King', 'President' NULL, '1978/11/17', 5000, NULL, 10)
*
ERROR at line 2:
ORA-00917: missing comma
Hello, I am new to MySQL and forgot how to properly INSERT at DATE when using MySQL Plus. I have tried many different variations as you will be able to see and I can't seem to figure it out. I also looked on the internet but always find examples where they make there own formats. What is the correct way to insert a date when the table was created using: hiredate
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '1981/11/17', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '1981/11/17', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01861: literal does not match format string
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '1981/17/11', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '1981/17/11', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01861: literal does not match format string
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01847: day of month must be between 1 and last day of month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 100);
VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 100)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King, 'President', NULL, '11/17/1981', 5000, NULL, 10);
ERROR:
ORA-01756: quoted string not properly terminated
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '11/17/81', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '11/17/81', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '81/17/11', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '81/17/11', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01847: day of month must be between 1 and last day of month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '17/81/11', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '17/81/11', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01847: day of month must be between 1 and last day of month
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '1981-11-17', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '1981-11-17', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01861: literal does not match format string
SQL> INSERT INTO employee
2 VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10);
VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10)
*
ERROR at line 2:
ORA-01843: not a valid month
You can use the TO_DATE function:
TO_DATE('11/17/1981', 'MM/DD/YYYY')
It lets you specify a date as a string and then the format that its in so Oracle can convert it into a Date.
Or you can use a date literal:
date '1981-11-17'
Dates are format sensitive in SQL. So you need to enclouse them in single quotes like
('21-Dec-2010'). If your date is not coming in the standard format then you need to convert using Single Row Conversion Function TO_DATE using the specific format model. You can find more of these syntax on my portal here
http://www.suhelsayyad.com/p/blog-page_14.html
I have a table that has 97 columns, I want to sum 96 columns.
select sum(col1+col2+col3+.....+col96)
from tableA where meter_id=x;
I do not want to give all 96 column names, what is the best way to do it?
Regards,
RR
There is no way to avoid writing each column name. All you can do is curse the stupid data modeller and get busy with cut'n'paste.
In the case where there are a significant number of columns, I would look at using the data dictionary tables to help create the query by using a query like the one below:
Select column_name || '+' as column_name_list
From user_tab_columns
Where table_name = 'TABLEA'
Order by column_id
It doesn't change the world but does simplify writing one query.
You could create a virtual column that adds up your 96 columns, something like:
alter table TABLEA add (MY_TOTAL_COL NUMBER GENERATED ALWAYS AS (col1+col2+col3...) VIRTUAL);
Then your query can simply do sum(my_total_col).
You might be best to sum the columns and then put the result in Excel to do the sum of sums. Otherwise this query should do what you need:
SELECT SUM(TOTAL_SUM) FROM (
SELECT SUM(column1) AS TOTAL_SUM FROM your_table
UNION
SELECT SUM(column2) AS TOTAL_SUM FROM your_table
UNION
SELECT SUM(column3) AS TOTAL_SUM FROM your_table
);
SELECT A.consol_key,
A.amt_lcy,
B.amt_lcy,
C.amt_lcy
FROM categ A,
spec B,
stmt C;
SELECT Sum(total_sum)
FROM (SELECT Sum(amt_lcy) AS TOTAL_SUM
FROM categ
UNION
SELECT Sum(amt_lcy) AS TOTAL_SUM
FROM spec
UNION
SELECT Sum(amt_lcy) AS TOTAL_SUM
FROM stmt)
WHERE table_id NOT IN (SELECT table_id
FROM categ
WHERE txn_code = 'COR'
AND system_id <> 'AA');
It could be possible:
Using Can an SQL procedure return a table?
and the answer of Mike Meyers you could write a stored procedure using dynamic sql
sumcolumns(columnfilter,tablename,whereclause)
and use it something like
select *
from table(sumcolumns('column_name <> ''col97''','tableA','meter_id=x'))
Try using UNPIVOT as per example below (still need to specify the column list as others have noted):
with tableA as /* prototype tableA just for example */
(
select 1 meter_id, 101 col1, 10 col2, 20 col3, 30 col4, NULL col5, 101 col11, 10 col12, 20 col13, 30 col14, NULL col15, 101 col21, 10 col22, 20 col23, 30 col24, NULL col25 from dual union
select 2, 102, 40, NULL, 50, NULL, 102, 40, NULL, 50, NULL, 102, 40, NULL, 50, NULL from dual union
select 3, 103, 60, 70, 80, 90, 103, 60, 70, 80, 90, 103, 60, 70, 80, 90 from dual union
select 4, 104, 100, NULL, NULL, NULL, 104, 100, NULL, NULL, NULL, 104, 100, NULL, NULL, NULL from dual
)
, unpivoted_tableA as /* UNPIVOT tableA columns into rows */
(
select *
from tableA
unpivot include nulls
(
col_value for col_ in
(COL1,COL2,COL3,COL4,COL5,COL11,COL12,COL13,COL14,COL15,COL21,COL22,COL23,COL24,COL25)
)
)
/* main query - Sum of all columns that were unpivoted to rows */
select meter_id, sum(col_value)
from unpivoted_tableA
group by meter_id
;
I have 2 tables:
CREATE TABLE employee (
employee_id NUMBER(5),
first_name VARCHAR2(20),
last_name VARCHAR2(20),
dept_no NUMBER(2),
salary NUMBER(10));
Table employee has values:
(1, 'Dan', 'Morgan', 10, 100000);
(2, 'Helen', 'Lofstrom', 20, 100000);
(3, 'Akiko', 'Toyota', 20, 50000);
(4, 'Jackie', 'Stough', 20, 40000);
(5, 'Richard', 'Foote', 20, 70000);
(6, 'Joe', 'Johnson', 20, 30000);
(7, 'Clark', 'Urling', 20, 90000);
CREATE TABLE bonuses (
employee_id NUMBER, bonus NUMBER DEFAULT 100);
Table bonuses has values:
(1,100);
(2,100);
(4,100);
(6,100);
(7,100);
And I did merge based on bonuses table:
MERGE INTO bonuses b
USING (
SELECT employee_id, salary, dept_no
FROM employee
WHERE dept_no =20) e
ON (b.employee_id = e.employee_id)
WHEN MATCHED THEN
UPDATE SET b.bonus = e.salary * 0.1
DELETE WHERE (e.salary < 40000)
WHEN NOT MATCHED THEN
INSERT (b.employee_id, b.bonus)
VALUES (e.employee_id, e.salary * 0.05)
WHERE (e.salary > 40000);
And when I
select * from bonuses;
EMPLOYEE_ID BONUS
------------------
1 100
2 10000
3 2500
4 4000
5 3500
7 9000
My question is "where is my bonuses record for employee_id #6"?
Deleted by DELETE WHERE (e.salary < 40000) statement?.
And You should probably make one of you conditions to include equality.
After all someone salary may be just 40K just like Jackie's.