I have an issue where my SQL query will not get rid of null values.
I have tried a myriad of different techniques, but I am a novice.
My desired result is an Order number followed by the date which the status occurs all in one line.
select mo.order_id OrderID,
case when osr.order_status_cd = 120 and osr.create_date is not null then osr.create_date end as POCreated,
case when osr.order_status_cd = 220 and osr.create_date is not null then osr.create_date end as Ordered,
case when osr.order_status_cd = 300 and osr.create_date is not null then osr.create_date end as Shipped,
case when osr.order_status_cd = 400 and osr.create_date is not null then osr.create_date end as Received,
case when osr.order_status_cd = 500 and osr.create_date is not null then osr.create_date end as Completed,
from order_status_record osr
inner join msorder mo on mo.order_id = osr.msorder_id
Instead of getting results that look like this:
OrderID POCreated Ordered Shipped Received Completed
497822 11/18/2012 NULL NULL NULL NULL
497822 NULL 11/19/2012 NULL NULL NULL
497822 NULL NULL 11/19/2012 NULL NULL
497822 NULL NULL NULL 11/19/2012 NULL
497822 NULL NULL NULL NULL 11/19/2012
I want this:
OrderID POCreated Ordered Shipped Received Completed
497822 11/18/2012 11/19/2012 11/19/2012 11/19/2012 11/19/2012
Do I need to create a virtual table? Do I need an if function? Why do the Nulls still populate?
Any help would be much appreciated.
Thanks,
Andrew
If you add an aggregate function to each of the
CASEstatements and then aGROUP BYthe records will consolidate into a single row: