sql - Sharing row in a union - group by? -
i have query this:
select * ( select id, sum( orderlinetotal ) orderline1, null orderline 2 orders orders.date < @today group id union select id, null orderline1, sum(orderlinetotal2) orderline 2 orders orders.date = @today group id ) o group o.id, o.orderline1, o.orderline2
i'm getting result this:
id orderline1 orderline2 ----------------------------------------- 1 105.00 null 1 null 204.00 2 49.30 null 2 null 94.24
is there way modify query return this?
id orderline1 orderline2 ----------------------------------------- 1 105.00 204.00 2 49.30 94.24
you want 1 query , conditional aggregation:
select id, sum(case when orders.date < @today orderlinetotal end) orderline1, sum(case when orders.date = @today orderlinetotal2 end) orderline2 orders group id
by way, there 2 columns, orderlinetotal
, orderlinetotal2
? suspect there 1 , second sum()
should change accordingly.
Comments
Post a Comment