postgresql - postgres output query within with clause -


i'm trying output of queries within clause of final query csv or sort of text files. have query access, i'm not allowed create tables database. have set of queries calculations on data set, set of queries compute on previous set , yet calculates on final set. don't want run of 3 seperate queries because results first 2 in last one.

with     q1 as(         select col1, col2, col3, col4, col5, col6, col7         table1     ),     q2 as(         select avg(col1) col1avg, max(col1) col1max, col2, col3,col4         q1         group col2, col3, col4     ) select      avg(col1avg), col3     q2 group col3 

i results q1, q2 , final select statement preferably 3 csv files live of in 1 csv file. possible?

thanks!

edit: clarify, columns queries different. i'm pulling more columns first query second. i've edited above code bit make more clear.

to combine results you'd use union all, number , data types of columns must match.

select col1, col2, col2   blah union select col1, col2, col2   blah2 union ... etc 

you can reference cte's in there of course ...

with   cte_1 (     select ... ...),   cte_2 (     select ... ... cte_1),   cte_3 (     select ... ... cte_2) select col1, col2, col2   cte_1 union select col1, col2, col2   cte_2 union select col1, col2, col2   cte_3 

if final output csv looks have multiple row formats in there -- checksums? if so, in queries union might combine columns each query 1 string ...

with   cte_1 (     select ... ...),   cte_2 (     select ... ... cte_1),   cte_3 (     select ... ... cte_2) select col1||','||col2||','||col2   cte_1 union select col1||','||col2   cte_2 union select col1   cte_3 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -