I have a table in SQL Server called [orders]:
I have another table called [order_details]:
as you can see, if the media_type is various in [orders] table, there will be 2 corresponding rows in [order_details].
how can i make the below table?
i was trying the method that the admins suggested even before they closed down my original question but what i was getting was the below:
i need this to be like:
You seem to just want conditional aggregation on order_details
:
select od.order_id,
sum(case when od.media_type = 'Loose Cartons' then qty else 0 end) as loose_cartons,
sum(case when od.media_type = 'Loose Units' then qty else 0 end) as loose_units
from order_details od
group by od.order_id;
you would just perform an aggregation on the query that got you these results
select [order id]
,max(pallets) as pallets
,max([total cartons]) as [total cartons]
,max(units) as units
,max(sets) as sets
,max(GOH) as GOH
,max([Loose Cartons]) as [Loose Cartons]
,max([Loose Units]) as [Loose Units]
from (<insert query that got you the results with two records >
)
group by [order id]
ok guys sorted.
So i had to create a separate table with just the order_id and 3 more columns with Pallets, Cartons and Sets.
i had to use "max" not "sum" otherwise it would double the quantity.
so what i used was:
create view SSDView_2ND_VB as
select
a.ORDER_ID
,max(case when b.MEDIATYPE_ID = '1' then b.ORDER_DETAIL_QUANTITY else null end) as [Pallets]
,max(case when b.MEDIATYPE_ID = '2' then b.ORDER_DETAIL_QUANTITY else null end) as [Cartons]
,max(case when b.MEDIATYPE_ID = '3' then b.ORDER_DETAIL_QUANTITY else null end) as [Sets]
from
SSDView_1ST_VB as a
left join ORDER_DETAIL as b
on a.ORDER_ID = b.ORDER_ID
group by a.ORDER_ID
The SSDView_1ST_VB was just another view which only had media type 'VARIOUS'.
I would like to thank you all for your help, really appreciate it guys ^^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.