|
Wilso011
Starting Member
4 Posts |
Posted - 2010-08-19 : 12:34:29
|
| I'm running the below query. A HAWB can be associated with multiple MAWBs in this query (up to 4). However, I would like to have the data placed into columns instead of rows. So instead of having the same record showing 2 or more results, I would like to show just 1 result. I think I could do a pivot, but that seems very complicated and wasn't sure if there was an easier option?Select C.*, case when D.MAWB IS null then ' ' else d.MAWB end as MAWB, case when D.MAWBCons IS null then ' ' else d.MAWBCons end as MAWBCons, case when D.ConsType IS null then ' ' else d.ConsType end as ConsType, case when D.AgentConsFlag IS null then ' ' else D.AgentConsFlag end as AgentConsFlag from (Select A.*, B.Charges, B.Profit from (select h.hawbnum as HAWB, h.TotalChargableWGT as ChargeableWeight, CONVERT(varchar(10), h.dateshipped, 101) as ShipDate, h.shipr as Shipper, h.consnameaddr as Cons, h.stnnum as Station, h.stndivisioncode as Division, case when h.stndivisioncode IN ('oceanexpor', 'oceanimpt','poceanimpo') then h.originportcode else h.originairport end as Origin,case when h.stndivisioncode IN ('oceanexpor', 'oceanimpt','poceanimpo') then h.destportcode else h.destairport end as Dest,h.cust as Customer, hp.nameaddrcode as Billto, h.freightpaymentmethod as PaymentMethod from HAWB as h left join NameAddrMstr as n on h.Shipr=n.nameaddrcode left join HAWBPPDBillTo as hp on h.HAWBNum=hp.hawbnum left join CountryMaster as c on n.CountryCode=c.countrycodewhere h.DateShipped between '1/1/2010' And '7/31/2010' and c.CountryName='Japan' group by h.HAWBNum, h.DateShipped, h.shipr, h.consnameaddr, h.StnNum, h.StnDivisionCode, h.OriginPortCode, h.OriginAirport, h.DestPortCode, h.DestAirport, h.Cust,hp.nameaddrcode, h.FreightPaymentMethod, h.TotalChargableWGT)Aleft join (select cd.hawbnum, SUM(case when cd.revexp='r' then cd.proratedamt else 0 end) as Charges,SUM(case when cd.revexp='r' then cd.proratedamt else 0 end) - SUM(case when cd.revexp='e' then cd.proratedamt else 0 end) as Profitfrom ChargeDistribution as cdwhere cd.SpareDate1 between '1/1/10' and '7/31/10' and cd.PostedToAcctg='y'group by cd.hawbnum)B on A.hawb=b.hawbnum)Cleft join(select d.DocNum1, m.MAWBNum as MAWB, m.consnameaddr as MAWBCons, m.ConsTypeCode as ConsType, m.AgentConsFlag as AgentConsFlag from DocumentDocument as d left join MAWB as m on d.DocNum2=m.mawbnum where d.Doc2DocType='mawb')D on C.HAWB=d.docnum1 union allSelect C.*, case when D.MAWB IS null then ' ' else d.MAWB end as MAWB, case when D.MAWBCons IS null then ' ' else d.MAWBCons end as MAWBCons, case when D.ConsType IS null then ' ' else d.ConsType end as ConsType, case when D.AgentConsFlag IS null then ' ' else D.AgentConsFlag end as AgentConsFlag from (Select A.*, B.Charges, B.Profit from (select h.hawbnum as HAWB, h.TotalChargableWGT as ChargeableWeight, CONVERT(varchar(10), h.dateshipped, 101) as ShipDate, h.shipr as Shipper, h.consnameaddr as Cons, h.stnnum as Station, h.stndivisioncode as Division, case when h.stndivisioncode IN ('oceanexpor', 'oceanimpt','poceanimpo') then h.originportcode else h.originairport end as Origin,case when h.stndivisioncode IN ('oceanexpor', 'oceanimpt','poceanimpo') then h.destportcode else h.destairport end as Dest,h.cust as Customer, hp.nameaddrcode as Billto, h.freightpaymentmethod as PaymentMethod from HAWB as h left join NameAddrMstr as n on h.consnameaddr=n.nameaddrcode left join HAWBPPDBillTo as hp on h.HAWBNum=hp.hawbnum left join CountryMaster as c on n.CountryCode=c.countrycodewhere h.DateShipped between '1/1/2010' And '7/31/2010' and c.CountryName='Japan'group by h.HAWBNum, h.DateShipped, h.shipr, h.consnameaddr, h.StnNum, h.StnDivisionCode, h.OriginPortCode, h.OriginAirport, h.DestPortCode, h.DestAirport, h.Cust,hp.nameaddrcode, h.FreightPaymentMethod, h.TotalChargableWGT)Aleft join (select cd.hawbnum, SUM(case when cd.revexp='r' then cd.proratedamt else 0 end) as Charges,SUM(case when cd.revexp='r' then cd.proratedamt else 0 end) - SUM(case when cd.revexp='e' then cd.proratedamt else 0 end) as Profitfrom ChargeDistribution as cdwhere cd.SpareDate1 between '1/1/10' and '7/31/10' and cd.PostedToAcctg='y'group by cd.hawbnum)B on A.hawb=b.hawbnum)Cleft join(select d.DocNum1, m.MAWBNum as MAWB, m.consnameaddr as MAWBCons, m.ConsTypeCode as ConsType, m.AgentConsFlag as AgentConsFlag from DocumentDocument as d left join MAWB as m on d.DocNum2=m.mawbnum where d.Doc2DocType='mawb')D on C.HAWB=d.docnum1order by c.hawb |
|