i have a single table called journal.
Journal has 4 columns:
- Date (date)
- Account(int)
- Type(varchar(1) (Can accept chars D or C)
- Amount(decimal(18,2))
JOURNAL
Date Account Amount Type
2012-05-31 20001 300 D
2012-05-31 20002 700 C
2012-05-31 20003 600 D
2012-05-31 20004 900 C
The type column can only take 2 types of character values: D or C.
So i need a query that will give me 4 columns, such that the resulting columns are:
Date Account D C
2012-05-31 200101 300 0
2012-05-31 200102 0 700
2012-05-31 200103 600 0
2012-05-31 200104 0 900
with the D and C columns filled with values from Amount, whether they are null or not.
You can accomplish this with a PIVOT:
See a SQL Fiddle with a Demo
If you want to join the account names, then you will want to perform a JOIN on that table. See update SQL Fiddle with a demo: