oavaldezi
oavaldezi

Reputation: 79

SQL: need to query single column with several transaction types and output in tabular format

I need to query a single table that contains records for several different types of transactions: table

and produce output in tabular format (without using an Excel pivot table, of course): enter image description here

Is there a better way to do this than by JOINing several subqueries? And if there is no better way, how should I go about the order of the JOINs?

Upvotes: 0

Views: 108

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270623

Just use conditional aggregation:

select yyyymm,
       sum(case when type = 'CNFF Iny' then amount end) as cnff_iny,
       sum(case when type = 'CNFF Ret' then amount end) as cnff_ret,
       . . .   -- and so on for all the values
from t
group by yyyymm
order by yyyymm;

Upvotes: 1

Related Questions