| Author |
Topic |
|
newsqluser123
Starting Member
7 Posts |
Posted - 2008-05-13 : 10:15:58
|
| Hello,I was wondering what is the best way to have multiple joins?Here are the two statements I've been trying to combine--------------------------------------------------------------------SELECT CASE when t1.Cust_DB_Shipment_Key = 'Used:' Then Description_1 Else (stuff(t1.Cust_DB_Shipment_Key,1,5,'')) End, t1.Airway_Bill_No, t1.Shipper_Reference, t1.External_Product_Cd, t1.Chargeable_Weight, dt.CountTrackingNumber, dt.SumProductCharge, t1.Consignee_Company_Name, CONVERT(CHAR(8),Ship_DT,112) FROM Shipping t1 Inner Join ( SELECT Cust_DB_Shipment_Key, sum(PRODUCT_CHARGE_AMOUNT) as [SumProductCharge], count(Airway_Bill_No) as [CountTrackingNumber] FROM Shipping Where Ship_DT = '2008-05-12' and status != 'voided' GROUP BY Cust_DB_Shipment_Key ) dt ON (stuff(t1.Cust_DB_Shipment_Key,1,5,'')) = (stuff(dt.Cust_DB_Shipment_Key,1,5,'')) Where Ship_DT = '2008-05-12' and status != 'voided'----------------------------------------------------------------------SELECT CASE when Cust_DB_Shipment_Key = 'Used:' Then Description_1 Else (stuff(Cust_DB_Shipment_Key,1,5,'')) End, Airway_Bill_No, Shipper_Reference, Service_Name, Chargeable_Weight, Consignee_Company_Name, CONVERT(CHAR(8),Ship_DT,112) FROM Shipping s JOIN Shipping..Distinct_Service_by_SAS_Code d ON s.External_Product_CD = d.SAS_Code Where Ship_DT = '2008-05-12' and status != 'voided'Thanks,Stephen |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-13 : 10:54:22
|
| How do you want the result? Both queries merged together as columns or both data as rows? |
 |
|
|
newsqluser123
Starting Member
7 Posts |
Posted - 2008-05-13 : 12:42:16
|
| Sorry I should have made my post more clear. In the second select statement I only need the service name. All the other stuff is redundant. So it should really look like this:SELECT CASE whenCust_DB_Shipment_Key = 'Used:' Then Description_1Else (stuff(Cust_DB_Shipment_Key,1,5,''))End,Service_NameCONVERT(CHAR(8),Ship_DT,112)FROM Shippings JOIN Shipping..Distinct_Service_by_SAS_Code d ON s.External_Product_CD = d.SAS_CodeWhere Ship_DT = '2008-05-12' and status != 'voided'So both joined would return everything in the first one and include service name from the second one. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-13 : 12:51:47
|
| SELECT tmp1.*,tmp2.Service_NameFROM(SELECT CASE when t1.Cust_DB_Shipment_Key = 'Used:' Then Description_1Else (stuff(t1.Cust_DB_Shipment_Key,1,5,''))End AS ShipmentKey,t1.Airway_Bill_No,t1.Shipper_Reference,t1.External_Product_Cd,t1.Chargeable_Weight,dt.CountTrackingNumber,dt.SumProductCharge,t1.Consignee_Company_Name,CONVERT(CHAR(8),Ship_DT,112) AS ShipDateFROM Shipping t1 Inner Join(SELECT Cust_DB_Shipment_Key,sum(PRODUCT_CHARGE_AMOUNT) as [SumProductCharge],count(Airway_Bill_No) as [CountTrackingNumber]FROM Shipping Where Ship_DT = '2008-05-12' and status != 'voided'GROUP BY Cust_DB_Shipment_Key) dtON (stuff(t1.Cust_DB_Shipment_Key,1,5,'')) = (stuff(dt.Cust_DB_Shipment_Key,1,5,''))Where Ship_DT = '2008-05-12' and status != 'voided')tmp1INNER JOIN(SELECT CASE when Cust_DB_Shipment_Key = 'Used:' Then Description_1Else (stuff(Cust_DB_Shipment_Key,1,5,''))End AS ShipmentKey,Service_Name,CONVERT(CHAR(8),Ship_DT,112) AS ShipDateFROM Shippings JOIN Shipping..Distinct_Service_by_SAS_Code d ON s.External_Product_CD = d.SAS_CodeWhere Ship_DT = '2008-05-12' and status != 'voided')tmp2ON tmp2.ShipmentKey=tmp1.ShipmentKeyAND tmp2.ShipDate=tmp1.ShipDate |
 |
|
|
newsqluser123
Starting Member
7 Posts |
Posted - 2008-05-14 : 09:05:51
|
| Thanks for the help Visakh16 |
 |
|
|
|
|
|