I am creating a giant SQL query:
Select * from (With tab1 AS ( Select * from abc)
,tab2 AS (select * from cde)
,tab3 AS (select * from tab2)
.
.
.
,tabz AS (select a, b from xyz
UNION
select a, b from cde)
Select tab1.*, tab3.* from
tab1
LEFT OUTER JOIN tab2 ON tab1.a = tab2.b
...
LEFT OUTER JOIN tabz on tab1.a = tabz.a) A
Now using the above as 1 table I need to create another long SQL to calculate percentages and other things with other tables tables.
Say above table is A
then
Select bbb.a,bbb.b from bbb
JOIN A ON bbb.a = A.a and then name it as B
And then finally join A LEFT OUTER JOIN B.
It is a massive Query and I know we can not have Nested WITH statement. Does anyone have any easy way to complete this? OR any suggestion?
I only need to accomplish this using oracle SQL queries.
I think you can rephrase your query as:
WITH
tab1 AS (select * from abc)
,tab2 AS (select * from cde)
,tab3 AS (select * from tab2)
.
.
.
,tabz AS (select a, b from xyz
UNION
select a, b from cde)
,a as (
Select tab1.*, tab3.*
from tab1
LEFT OUTER JOIN tab2 ON tab1.a = tab2.b
...
LEFT OUTER JOIN tabz on tab1.a = tabz.a
),
b as (
Select bbb.a,bbb.b from bbb JOIN A ON bbb.a = A.a
)
select *
from a
left join b on ...
Related
SELECT
a.First_Name
,b.Last_Name
FROM table a
JOIN table b
UNION table c
ON a.ID=b.ID
I'm not even sure how the syntax would be for this. Basically what I want is to UNION table b and c then JOIN the new table to table a.
You can join a subquery:
SELECT
a.First_Name,
bc.Last_Name
FROM a
JOIN
(
SELECT ID, Last_Name FROM b
UNION
SELECT ID, Last_Name FROM c
) bc ON bc.ID = a.ID;
Make the union a subquery:
SELECT
a.First_Name
,bc.Last_Name
FROM table a
JOIN (
SELECT * FROM table b
UNION
SELECT * FROM table c
) AS bc
ON a.ID=bc.ID
Note that the subquery must have its own alias (bc in this case) which should be referenced in the join clause and the select (meaning you can't reference tables b or c directly outside of the subquery).
The result of a union can be join to others table if you the the proper join clause
You could use this sintax
select a.col1, t.name
from table3 c
inner join (
select id, name1 as name
from table1 a
union id, name2
from table2 b
) t on. t.id = a.id
I have a few tables, each containing different columns, which contain different subsets of all the products.
I want to get a list of all the information about all the products, so I want to do a full outer join on the product_id.
I tried
select * from table1
full outer join table2 b on b.product_id = table1.product_id
...
full outer join tableN c on b.product_id = table1.product_id
but this results in multiple rows where a product_id does not exist in table1, but might exist in table2 and tableN.
Is there a way to "coalesce" the join column?
If you use full outer join with more than two tables, then you will end up with something like this:
select *
from table1 a full outer join
table2 b
on b.product_id = a.product_id
... full outer join
tableN c
on c.product_id = coalesce(b.product_id, a.product_id)
The using clause -- supported by Postgres but not SQL Server -- simplifies this. It does assume that the columns all have the same name.
An alternative is a driving table. If you don't have a table of all products handy, you can create one:
select *
from (select product_id from table1 union
select product_id from table2 union
. . .
select product_id from tableN
) driving left join
table1 a
on a.product_id = driving.product_id left join
table2 b
on b.product_id = driving.product_id
... full outer join
tableN c
on c.product_id = driving.product_id;
This should be easier to interpret and the on clauses are simplified.
Finally, perhaps you just want common columns. If so, just use union all:
select product_id, col1, col2, col3 from table1 union all
select product_id, col1, col2, col3 from table2 union all
. . .
select product_id, col1, col2, col3 from tableN;
This prevents the proliferation of columns with NULL values.
select * from table1
inner join
(
SELECT * FROM table2
UNION ALL
SELECT * FROM table3
UNION ALL ........
SELECT * FROM tableN
) N
on N.product_id = table1.product_id
I have SELECT statement with LEFT JOIN and joined tables are sub-queries. And Oracle could not recognize alias of the first sub-query in the second one. It works for DB2 but does not work for Oracle.
How I can implement it or rewrite my query?
SELECT *
FROM
(SELECT E.C3 AS COLUMN3
, E.C4 AS COLUMN4
FROM TBL_1 D
, TBL_2 E
WHERE D.C6 = E.C6 ) B
LEFT JOIN TABLE
(SELECT C.C1
FROM TBL_3 C
WHERE
C.C7 = 'hello'
AND B.C3 = C.C8
UNION ALL
SELECT C.C1
FROM TBL_3 C
WHERE
C.C7 = 'world'
AND B.C4 = C.C8
) A
ON 1 = 1
Oracle error message:
ORA-00904: "B"."C3": invalid identifier
You can simplify this query to the following, removing the sub-queries:
Select A.Col1, B.Col2
From tbl_AJoin A
Left Join tbl_BJoin B On A.col1 = B.col1
You have a syntax error. This:
select * from (select col1 from tbl_Ajoin) A
left join table (select col2 from tbl_Bjoin where A.col1 = tbl_Bjoin.col1) B
ON 1 = 1
should be this:
select * from (select col1 from tbl_Ajoin) A
left join (select col2 from tbl_Bjoin where A.col1 = tbl_Bjoin.col1) B
ON 1 = 1
or more specifically, this:
left join table (select
should not have the word table. It should be this:
left join (select
I have following queries.
Select * from (
Select a, b, c, d, from t1
Union
Select a, b, c, d from t2
) where a is not null and order by b.
Now I have to fetch data from another table based on above result set.
Select * from (Select * from (
Select a, b, c, d, from t1
Union
Select a, b, c, d from t2
)
where a is not null and order by b)
as tempT1 left outer join t3 on tempT1.a = t3.a
I have to further use this result set to form another select query. So, writing in above style will be complex with time. And with time this query will be complex to read.
How to make it simple? Can I dump partial result to another table?
You can create views that will replace these inner SELECTs
From w3schools :
"In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were coming from one single table."
Document them so you wont get lost and that's it...
Use temporary tables. Rewrite your queries as
Select * into #tempResult1 from (
Select a, b, c, d, from t1
Union
Select a, b, c, d from t2
) where a is not null and order by b
Now for next query, use above temp table as
Select * into #tempResult2
from #tempResult1
left outer join t3 on tempT1.a = t3.a
Now instead of writing this query, just use #tempResult2.
select * from #tempResult2
You have some wrong SQL syntax too. Try:
;
WITH Q1
AS ( SELECT a ,
b ,
c ,
d
FROM t1
UNION
SELECT a ,
b ,
c ,
d
FROM t2
),
Q2
AS ( SELECT *
FROM Q1
)
SELECT *
FROM Q2 tempT1
LEFT OUTER JOIN t3 ON tempT1.a = t3.a
WHERE a IS NOT NULL
ORDER BY b
Assuming that you are using SQL Server, You could simplify something like below. Not a compiled version. But you could use this as pseudo code.
with cte as
(
Select a, b, c, d from t1 where a is not null
Union
Select a, b, c, d from t2 where a is not null
)
select * from cte tempT1 left outer join t3 on tempT1.a = t3.a order by tempT1.b
You Can use Comman Table expression or Temp Table or Table Variable
with cte as
(
Select a, b, c, d from t1
Union
Select a, b, c, d from t2
)
,
cte2 as
(
select * cte where a is not null and order by b
)
select * from cte2 c left outer join t3 on c.a = t3.a
Using Temp Table
select * into #temp1
from
(
Select a, b, c, d from t1
Union
Select a, b, c, d from t2
)
select * into #temp2 from #temp1 where a is not null and order by b
select * from temp2 c left outer join t3 on c.a = t3.a
select a,b,c from(SELECT max(modifiedtime) a FROM tabl1 ),
(SELECT max(modifiedtime) b FROM tabl2 ),
(SELECT max(modifiedtime) c FROM tabl3 ) ;
working fine but...output is like..
Row
a b c
1 1421906942814 null null
2 null 1421906942814 null
3 null null 1421906942817
but I want to print data like
a b c
1421906942814 1421906942814 1421906942817
how to do it...
As far I know you need to use a dummykey (even if it's a constant) to be able to do the JOIN on it.
This works:
SELECT t1.value,
t2.value,
t3.value
FROM
(SELECT 1 AS dummykey,
'max1' AS value) t1
JOIN
(SELECT 1 AS dummykey,
'max2' AS value) t2 ON t1.dummykey=t2.dummykey
JOIN
(SELECT 1 AS dummykey,
'max3' AS value) t3 ON t1.dummykey=t3.dummykey
try
SELECT
(SELECT max(modifiedtime)
FROM tabl1) a,
(SELECT max(modifiedtime)
FROM tabl2) b,
(SELECT max(modifiedtime)
FROM tabl3) c;
I would do CROSS JOIN between them:
SELECT * FROM
(SELECT max(modifiedtime) FROM tabl1) a
CROSS JOIN
(SELECT max(modifiedtime) FROM tabl2) b
CROSS JOIN
(SELECT max(modifiedtime) FROM tabl3) c;