I need to append 3 queries in Oracle's SQL
I need to append three queries and make them look like this:
HEADER1 HEADER2 HEADER3
-------------------------
Total1 Total2 Total3
I've tried UNION
, but that returns the results of the queries in rows l开发者_JS百科ike this:
HEADER
------
total1
total2
total3
Any suggestions?
Not sure, but maybe something like:
select (select x1 from something1) as header1,
(select x2 from something2) as header2,
(select x3 from something3) as header3
from dual
This is an interesting situation when you want to make something into a single row rather than multiple rows. The easiest way is to do a sum() for each "column" and placing your where style clause in an IF() statement.
select sum(sumconstraints) as Header1, sum(sumconstraints) as Header2, etc...
sumconstraints should be an IF(where clause for this total, 1, 0)
I have sometimes found the following to be a useful construct;
select sum(c1) Header1, sum(c2) Header2, sum(c3) Header3
from (
select field c1, 0 c2, 0 c3
from table1
) t1,
(
select 0, field, 0
from table2
) t2
(
select 0,0,field
from table3
) t3
[where clause and joins between t1,t2 and t3]
In the example I have assumed numeric values and used a sum. The same approach can be used with strings by, for example, replacing the 0 with NULL and SUM with MAX.
精彩评论