Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Comments on SQL Preference

$
0
0

I am running into a number of people who are using conditions in a Join that I believe should be used in a where clause. Curious on your comments on what is preferred and why. Below is an example, it is not important the tables that are joined, but the method. Focus on the Joins...

SELECT SINVOICE.INVDAT_0 AS INVDATE, SINVOICE.NUM_0 AS INVNUM, SINVOICE.ROWID as LINENUM, 0 as CUSTPO, BPCUSTOMER.BPCGRU_0 as GROUPCUST,
BPGROUP.BPCNAM_0 as GROUPNAME, SINVOICE.BPCORD_0 as CUSTOMER, BPCUSTOMER.BPCNAM_0 as CUSTNAME, SINVOICE.ITMREF_0 as ITEMNUM,
ITMMASTER.ITMDES1_0 as ITEMDESC, ITMMASTER.ITMDES2_0 as LONGITEMDESC, (SINVOICE.GROPRI_0 * SINVOICE.QTY_0) as GROSS$$,
(SINVOICE.NETPRI_0 * SINVOICE.QTY_0) as NET$$, SINVOICE.GROPRI_0 as ITEMPRICE, SINVOICE.NETPRI_0 as NETITEMPRICE, SINVOICE.QTY_0 as QTY,
ITMMASTER.SAU_0 as UM, ITMMASTER.SAUSTUCOE_0 as UMWEIGHT, '' as ORDERNUM, '03/28/2014' as INVSYSDATE,
SINVOICE.SHIDAT_0 as SHIPDATE, '1' as FISCALWEEK, PERIOD.PERNUM_0 as FISCALMONTH, '1' as FISCALQTR, PERIOD.FIYNUM_0 as FISCALYEAR,
BPCUSTOMER.REP_0 as SALESREP, CSALESREP.REPNAM_0 as REPNAME, BPCUSTOMER.REP_1 as SBROKER, CBROKER.REPNAM_0 as BROKERNAME,
BPGROUP.REP_0 as GROUPREP, GSALESREP.REPNAM_0 as GREPNAME, BPGROUP.REP_1 as GBROKER, GBROKER.REPNAM_0 as GBROKERNAME,
ITMMASTER.SAU_0 as SALESUNIT, ITMMASTER.SAUSTUCOE_0 as SALESWEIGHT, ITMMASTER.PCU_0 as PAKCUNIT1, ITMMASTER.PCU_1 as PACKUNIT2,
ITMMASTER.PCUSTUCOE_0 as PU1WEIGHT, ITMMASTER.PCUSTUCOE_1 as PU2WEIGHT, ITMMASTER.Z_PALNUM_0 as PALQTY,
(ITMMASTER.Z_PALNUM_0 * IIf(ITMMASTER.PCUSTUCOE_1 > 0, ITMMASTER.PCUSTUCOE_1,ITMMASTER.PCUSTUCOE_0)) as PALWEIGHT,
CCHANNEL.DES_0 as SALESCHANNEL, GCHANNEL.DES_0 as GROUPCHANNEL, IPRODUCT.DES_0 as PRODUCT, IGRAIN.DES_0 as GRAIN,
IPRODCAT.DES_0 as PRODCAT, ITMMASTER.ITMDES3_0 as BRAND, BPADDRESS.BPAADDLIG_0 as ADDLN1, BPADDRESS.BPAADDLIG_1 as ADDLN2, BPADDRESS.CTY_0 as CITY,
BPADDRESS.SAT_0 as ADDSTATE,BPADDRESS.POSCOD_0 as ZIPCODE
FROM

[x3v6].PRODUCTION.ZPRISMINVD as SINVOICE  Left Outer Join [x3v6].PRODUCTION.PERIOD as PERIOD on (SINVOICE.INVDAT_0 Between PERIOD.PERSTR_0 And PERIOD.PEREND_0)
 Inner Join [x3v6].PRODUCTION.ITMMASTER as ITMMASTER on (SINVOICE.ITMREF_0 = ITMMASTER.ITMREF_0)
 Left Outer Join [x3v6].PRODUCTION.CACCE as IPRODUCT on (ITMMASTER.CCE_2 = IPRODUCT.CCE_0 And IPRODUCT.DIE_0 = 'PRD')
 Left Outer Join [x3v6].PRODUCTION.CACCE as IGRAIN on (ITMMASTER.CCE_3 = IGRAIN.CCE_0 And IGRAIN.DIE_0 = 'PRG')
 Left Outer Join [x3v6].PRODUCTION.CACCE as IPRODCAT on (ITMMASTER.CCE_6 = IPRODCAT.CCE_0 And IPRODCAT.DIE_0 = 'PRC')
 Inner Join [x3v6].PRODUCTION.BPCUSTOMER as BPCUSTOMER on (SINVOICE.BPCORD_0 = BPCUSTOMER.BPCNUM_0)
 Left Outer Join [x3v6].PRODUCTION.CACCE as CCHANNEL on (BPCUSTOMER.BCGCOD_0 = CCHANNEL.CCE_0 And CCHANNEL.DIE_0 = 'CUS')
 Left Outer Join [x3v6].PRODUCTION.BPCUSTOMER as BPGROUP on (BPCUSTOMER.BPCGRU_0 = BPGROUP.BPCNUM_0)
 Left Outer Join [x3v6].PRODUCTION.CACCE as GCHANNEL on (BPGROUP.BCGCOD_0 = GCHANNEL.CCE_0 And GCHANNEL.DIE_0 = 'CUS')
 Left Outer Join [x3v6].PRODUCTION.SALESREP as CSALESREP on (BPCUSTOMER.REP_0 = CSALESREP.REPNUM_0)
 Left Outer Join [x3v6].PRODUCTION.SALESREP as CBROKER on (BPCUSTOMER.REP_1 = CBROKER.REPNUM_0)
 Left Outer Join [x3v6].PRODUCTION.SALESREP as GSALESREP on (BPGROUP.REP_0 = GSALESREP.REPNUM_0)
 Left Outer Join [x3v6].PRODUCTION.SALESREP as GBROKER on (BPGROUP.REP_1 = GBROKER.REPNUM_0)
 Left Outer Join [x3v6].PRODUCTION.BPADDRESS as BPADDRESS on (BPCUSTOMER.BPCNUM_0 = BPADDRESS.BPANUM_0 And BPCUSTOMER.BPDADD_0 = BPADDRESS.BPAADD_0)
Order by INVDATE, INVNUM

My preference would be ...

SELECT SINVOICE.INVDAT_0 AS INVDATE, SINVOICE.NUM_0 AS INVNUM, SINVOICE.ROWID as LINENUM, 0 as CUSTPO, BPCUSTOMER.BPCGRU_0 as GROUPCUST,
BPGROUP.BPCNAM_0 as GROUPNAME, SINVOICE.BPCORD_0 as CUSTOMER, BPCUSTOMER.BPCNAM_0 as CUSTNAME, SINVOICE.ITMREF_0 as ITEMNUM,
ITMMASTER.ITMDES1_0 as ITEMDESC, ITMMASTER.ITMDES2_0 as LONGITEMDESC, (SINVOICE.GROPRI_0 * SINVOICE.QTY_0) as GROSS$$,
(SINVOICE.NETPRI_0 * SINVOICE.QTY_0) as NET$$, SINVOICE.GROPRI_0 as ITEMPRICE, SINVOICE.NETPRI_0 as NETITEMPRICE, SINVOICE.QTY_0 as QTY,
ITMMASTER.SAU_0 as UM, ITMMASTER.SAUSTUCOE_0 as UMWEIGHT, '' as ORDERNUM, SINVOICE.INVDAT_0 as INVSYSDATE,
SINVOICE.SHIDAT_0 as SHIPDATE, Null as FISCALWEEK, PERIOD.PERNUM_0 as FISCALMONTH, Null as FISCALQTR, PERIOD.FIYNUM_0 as FISCALYEAR,
BPCUSTOMER.REP_0 as SALESREP, CSALESREP.REPNAM_0 as REPNAME, BPCUSTOMER.REP_1 as SBROKER, CBROKER.REPNAM_0 as BROKERNAME,
BPGROUP.REP_0 as GROUPREP, GSALESREP.REPNAM_0 as GREPNAME, BPGROUP.REP_1 as GBROKER, GBROKER.REPNAM_0 as GBROKERNAME,
ITMMASTER.SAU_0 as SALESUNIT, ITMMASTER.SAUSTUCOE_0 as SALESWEIGHT, ITMMASTER.PCU_0 as PAKCUNIT1, ITMMASTER.PCU_1 as PACKUNIT2,
ITMMASTER.PCUSTUCOE_0 as PU1WEIGHT, ITMMASTER.PCUSTUCOE_1 as PU2WEIGHT, ITMMASTER.Z_PALNUM_0 as PALQTY,
(ITMMASTER.Z_PALNUM_0 * IIf(ITMMASTER.PCUSTUCOE_1 > 0, ITMMASTER.PCUSTUCOE_1,ITMMASTER.PCUSTUCOE_0)) as PALWEIGHT,
CCHANNEL.DES_0 as SALESCHANNEL, GCHANNEL.DES_0 as GROUPCHANNEL, IPRODUCT.DES_0 as PRODUCT, IGRAIN.DES_0 as GRAIN,
IPRODCAT.DES_0 as PRODCAT, ITMMASTER.ITMDES3_0 as BRAND, BPADDRESS.BPAADDLIG_0 as ADDLN1, BPADDRESS.BPAADDLIG_1 as ADDLN2, BPADDRESS.CTY_0 as CITY,
BPADDRESS.SAT_0 as ADDSTATE,BPADDRESS.POSCOD_0 as ZIPCODE
FROM [x3v6].PRODUCTION.ZPRISMINVD as SINVOICE
 Left Outer Join [x3v6].PRODUCTION.PERIOD as PERIOD on (SINVOICE.INVDAT_0 Between PERIOD.PERSTR_0 And PERIOD.PEREND_0)
 Inner Join [x3v6].PRODUCTION.ITMMASTER as ITMMASTER on (SINVOICE.ITMREF_0 = ITMMASTER.ITMREF_0)
 Left Outer Join [x3v6].PRODUCTION.CACCE as IPRODUCT on (ITMMASTER.CCE_2 = IPRODUCT.CCE_0)
 Left Outer Join [x3v6].PRODUCTION.CACCE as IGRAIN on (ITMMASTER.CCE_3 = IGRAIN.CCE_0)
 Left Outer Join [x3v6].PRODUCTION.CACCE as IPRODCAT on (ITMMASTER.CCE_6 = IPRODCAT.CCE_0)
 Inner Join [x3v6].PRODUCTION.BPCUSTOMER as BPCUSTOMER on (SINVOICE.BPCORD_0 = BPCUSTOMER.BPCNUM_0)
 Left Outer Join [x3v6].PRODUCTION.CACCE as CCHANNEL on (BPCUSTOMER.BCGCOD_0 = CCHANNEL.CCE_0)
 Left Outer Join [x3v6].PRODUCTION.BPCUSTOMER as BPGROUP on (BPCUSTOMER.BPCGRU_0 = BPGROUP.BPCNUM_0)
 Left Outer Join [x3v6].PRODUCTION.CACCE as GCHANNEL on (BPGROUP.BCGCOD_0 = GCHANNEL.CCE_0)
 Left Outer Join [x3v6].PRODUCTION.SALESREP as CSALESREP on (BPCUSTOMER.REP_0 = CSALESREP.REPNUM_0)
 Left Outer Join [x3v6].PRODUCTION.SALESREP as CBROKER on (BPCUSTOMER.REP_1 = CBROKER.REPNUM_0)
 Left Outer Join [x3v6].PRODUCTION.SALESREP as GSALESREP on (BPGROUP.REP_0 = GSALESREP.REPNUM_0)
 Left Outer Join [x3v6].PRODUCTION.SALESREP as GBROKER on (BPGROUP.REP_1 = GBROKER.REPNUM_0)
 Left Outer Join [x3v6].PRODUCTION.BPADDRESS as BPADDRESS on (BPCUSTOMER.BPCNUM_0 = BPADDRESS.BPANUM_0 And BPCUSTOMER.BPDADD_0 = BPADDRESS.BPAADD_0)
Where IPRODUCT.DIE_0 = 'PRD' AND IGRAIN.DIE_0 = 'PRG' AND IPRODCAT.DIE_0 = 'PRC' And CCHANNEL.DIE_0 = 'CUS' And GCHANNEL.DIE_0 = 'CUS'
Order by INVDATE, INVNUM


Lee


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>