DB2 SQL : Combine two queries and select max of one aggregate column -


i have 2 queries (example version)-

query a:

select    col1 col1,   col2 col2,   sum(col3) col3 table1 join table11 .. group col1, col2; 

query b:

select    col1 col1,   col2 col2,   count(col3) col3 table2 group col1, col2; 

i want join both of them , have output below. tried this

select a.col1, a.col2, greatest(a.col3, b.col3) (query a) union (query b) b 

but getting error

sql0104n unexpected token "end-of-statement" found following "col2) b". expected tokens may include: "join <joined_table>". sqlstate=42601

both queries individually running fine, when combined using union above, giving error.

is there better way achieve this?

it looks need join, not union:

select a.col1, a.col2, greatest(a.col3, b.col3) (query a) join (query b) b on a.col1 = b.col1 , a.col2 = b.col2 

depending on needs, may need left join, right join or full outer join instead of inner join.


Comments

Popular posts from this blog

How to use SUM() in MySQL for calculated values -

loops - Spock: How to use test data with @Stepwise -