CREATE TABLE table2 AS
(select *,
min(nullif (("col1"),0)) over (partition by col2,col3,col4) as "col6",
min(nullif("col5",0)) over (partition by col1,col2,col3,col4) as "col7"
FROM table1);
I dont know whats the error?
You can't use the wildcard "*" for columns like that. Because you are adding additional columns, you need to list out all the columns ...
So if your table1 has columns "col1 and col2", then your query would be:
CREATE TABLE table2 AS
(select col1, col2,
min(nullif (("col1"),0)) over (partition by col2,col3,col4) as "col6",
min(nullif("col5",0)) over (partition by col1,col2,col3,col4) as "col7"
FROM table1);
Related
Do you know how to create an Oracle query that will list my table name, followed by all the column names in that table? I have 5 tables in total.
Something like:
Table
Column1
Column2
Column3
Table2
Column1
Table3
Column1
Column2
I'm hard-pressed to imagine why you would want a single-column result that doesn't do anything to differentiate between what is a column name and what is a table name. You can do something like
select pseudo_column_name
from (
select table_name, table_name pseudo_column_name, 0 column_id
from user_tables
union all
select table_name, column_name, column_id
from user_tab_columns
)
order by table_name, column_id
I have a simple SQL insert query, however two of the data entires come from two other tables,
I know how to get data from from one table in an insert but how do i get data from two tables?
Example (One Table Data)
INSERT INTO TABLE (COL1, COL2, COL3)
SELECT :COL1, :COL2, TABLE2.ID
FROM TABLE2
WHERE TABLE2.NAME = :LEVEL0
The above works fine and pulls relevant data out of table2, issue is i need to add a third table.
INSERT INTO TABLE (COL1, COL2, COL3, COL4)
SELECT :COL1,
:COL2,
(TABLE2.ID FROM TABLE2 WHERE TABLE2.NAME = :LEVEL0),
(TABLE3.ID FROM TABLE3 WHERE TABLE3.NAME = :LEVEL1)
doesn't work i get SQL errors
Subqueries are queries, so they need their own SELECT:
INSERT INTO TABLE (COL1, COL2, COL3, COL4)
SELECT :COL1,
:COL2,
(SELECT ID FROM TABLE2 WHERE NAME = :LEVEL0),
(SELECT ID FROM TABLE3 WHERE NAME = :LEVEL1);
And when all values are computed by subqueries, you do not need to use the SELECT form of the INSERT:
INSERT INTO TABLE (COL1, COL2, COL3, COL4)
VALUES(:COL1,
:COL2,
(SELECT ID FROM TABLE2 WHERE NAME = :LEVEL0),
(SELECT ID FROM TABLE3 WHERE NAME = :LEVEL1));
Literally as i asked the question i found the solution 'cross join'
INSERT INTO TABLE1 (COL1, COL2, COL3, COL4)
SELECT :COL1, :COL2, TABLE2.ID, TABLE3.ID
FROM TABLE2 CROSS JOIN TABLE3
WHERE TABLE2.NAME = :LEVEL0 AND TABLE3.NAME = :LEVEL1
I have a table in my database I do a Select all on:
SELECT * FROM TableA;
I want to append a column that is true or false if there's a related column in anther table. I can do it with this:
SELECT *, (SELECT COUNT(Id) > 0 FROM TableB WHERE Id = TableA.Id) FROM TableA;
But I don't want to have to count EVERY row in TableB to work this out as its ineffient. I essentially want an EXISTS check instead of count.
How do I replace the COUNT with EXISTS?
Thanks!
By using a left join
SELECT a.*, b.id is not null as condition_check
FROM TableA a
LEFT JOIN TableB b ON a.Id = b.Id
Ah - just realised the answer to my own question
SELECT *, EXISTS(SELECT Id FROM TableB WHERE Id= TableA.Id) AS DoesExist FROM TableA
How to achieve the result set in sql query
You could work along
WITH
T1 AS (
SELECT
val,
ROW_NUMBER() OVER (PARTITION BY NULL ORDER BY id) rn
FROM Table1
),
T2 AS (
SELECT
val,
ROW_NUMBER() OVER (PARTITION BY NULL ORDER BY id) rn
FROM Table2
),
T3 AS (
SELECT
val,
ROW_NUMBER() OVER (PARTITION BY NULL ORDER BY id) rn
FROM Table3
)
SELECT
T1.val column1
, T2.val column2
, T3.val column3
FROM T1
JOIN T2
ON T1.rn = T2.rn
JOIN T3
ON T2.rn = T3.rn
ORDER BY T1.rn
;
You'd need to
put the statements, which are now going into the UNION into "T1" through "T3", and
move your current sort orders to the ROW_NUMBER analytic functions respectively.
… and should be done: SQL Fiddle
Please comment, if and as further detail is required.
I want to insert records from first table to second in such a way that only unique records get inserted. That is if table one and two has overlapping (duplicate) records, only the unique records get inserted into table one from table two. For example I have one table named Table_One,
and another table named Table_Two,
now I want to add the records from Table_Two the records that are not already in Table_one which is (XYZ | UVW) so that the output is,
I tried using this technique,
INSERT INTO TableB(Col1, Col2, Col3, ... , Coln)
SELECT DISTINCT A.Col1, A.Col2, A.Col3, ... , A.Coln
FROM TableA A
LEFT JOIN TableB B
ON A.KeyOfTableA = B.KeyOfTableB
WHERE B.KeyOfTableB IS NULL
but the duplicate rows also got inserted, any suggestions as to what could be the solution ??? I am using sqlite3 db.
INSERT INTO TableB (Col1, Col2, Col3, ... , Coln)
SELECT DISTINCT A.Col1,
A.Col2,
A.Col3,
... ,
A.Coln
FROM TableA A
WHERE A.KeyOfTableA NOT IN (SELECT B.KeyOfTableB
FROM TableB B)
Simply use UNION.
Query
sqlite> CREATE TABLE table1(firstName VARCHAR(50),lastName VARCHAR(50));
sqlite> INSERT INTO table1 VALUES('ABC','DEF');
sqlite> INSERT INTO table1 VALUES('GHI','JKL');
sqlite> INSERT INTO table1 VALUES('MNO','PQR');
sqlite>
sqlite> CREATE TABLE table2(first VARCHAR(50),last VARCHAR(50));
sqlite> INSERT INTO table2 VALUES('ABC','DEF');
sqlite> INSERT INTO table2 VALUES('XYZ','UVW');
sqlite> .headers ON
sqlite> SELECT * FROM table1 UNION SELECT * FROm table2;
OUTPUT
firstName|lastName
ABC|DEF
GHI|JKL
MNO|PQR
XYZ|UVW