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

Looking to improve slow running query

$
0
0

Hi,

Is there any way of rewriting this query to make it more efficient? It's running pretty slow for me and If there are any glaring problems with the way its been written I'd like to fix them.

SELECT     TOP (100) PERCENT dbo.CUSTPACKINGSLIPTRANS.ITEMID AS [Stock Code], dbo.INVENTTABLE.ITEMNAME AS [Stock Desc], 
                      dbo.CUSTPACKINGSLIPTRANS.SALESUNIT AS [Unit of Sale], dbo.DIMENSIONS.DESCRIPTION AS [Product Group], dbo.INVENTTABLE.ITEMGROUPID AS [Item Group], 
                      dbo.INVENTTABLE.SECTION, dbo.INVENTTABLE.GROUPS AS [Group], dbo.INVENTTABLE.SUBGROUPS AS [Sub Group], 
                      dbo.CUSTPACKINGSLIPJOUR.ORDERACCOUNT AS [Cust Account], dbo.CUSTTABLE.INVOICEACCOUNT AS [Invoice Account], dbo.CUSTTABLE.NAME AS [Cust Name], 
                      dbo.CUSTTABLE.CURRENCY, dbo.CUSTTABLE.CIT_CONTROLLER AS [Credit Controller], dbo.CUSTTABLE.CREDITMAX AS [Credit Limit], 
                      dbo.CUSTTABLE.CUSTCLASSIFICATIONID AS Classification, dbo.EMPLTABLE.NAME AS [Sales Person], dbo.EMPLTABLE.ITEMBUYERGROUPID AS [Outside Rooms], 
                      CASE WHEN CUSTTABLE.CUSTCLASSIFICATIONID = 'Cash' THEN dbo.SALESTABLE.SALESGROUP ELSE dbo.CUSTTABLE.SALESGROUP END AS [Sales Rep], 
                      CASE WHEN CUSTTABLE.CUSTCLASSIFICATIONID = 'Cash' THEN dbo.SALESTABLE.DELIVERYSTATE ELSE dbo.CUSTTABLE.STATE END AS [Customer Region], 
                      CASE WHEN CUSTTABLE.CUSTCLASSIFICATIONID = 'Cash' THEN dbo.SALESTABLE.DELIVERYCOUNTY ELSE dbo.CUSTTABLE.COUNTY END AS [Customer County], 
                      dbo.CUSTTABLE.LINEOFBUSINESSID AS [Line Of Business], dbo.SALESLINE.DIMENSION AS [Site/Location], 
                      dbo.CUSTPACKINGSLIPTRANS.PACKINGSLIPID AS [Delivery Ref], dbo.CUSTPACKINGSLIPTRANS.DLVCOUNTRYREGIONID, CONVERT(varchar(12), 
                      dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE, 111) AS [Delivery Date], dbo.CUSTPACKINGSLIPJOUR.DELIVERYADDRESS AS [Delivery Address], 
                      dbo.CUSTPACKINGSLIPTRANS.VALUEMST AS [Delivery Value (CON)], CONVERT(varchar(12), dbo.SALESTABLE.RECEIPTDATEREQUESTED, 111) 
                      AS [Requested Delivery Date], dbo.SALESTABLE.SALESID AS [Order Ref], 
                      CASE dbo.SALESTABLE.SALESSTATUS WHEN '0' THEN 'None' WHEN '1' THEN 'Open Order' WHEN '2' THEN 'Delivered' WHEN '3' THEN 'Invoiced' WHEN '4' THEN 'Canceled'
                       END AS [Sales Status], 
                      CASE dbo.SALESTABLE.SALESTYPE WHEN '0' THEN 'Journal' WHEN '1' THEN 'Quotation' WHEN '2' THEN 'Subscription' WHEN '3' THEN 'Sales Order' WHEN '4' THEN
                       'Returned Item' WHEN '5' THEN 'Blanket Order' WHEN '6' THEN 'Item Requirements' WHEN '7' THEN 'Undefined' END AS [Sales Type], 
                      CASE dbo.SALESTABLE.AG_SALESLOCATION WHEN '0' THEN 'None' WHEN '1' THEN 'OR_DGN' WHEN '2' THEN 'OR_FMT' WHEN '3' THEN 'OR_TME' WHEN '4' THEN
                       'OR_OMA' WHEN '5' THEN 'DSP_DGN' WHEN '6' THEN 'DSP_FMT' WHEN '7' THEN 'DSP_TME' WHEN '8' THEN 'DSP_OMA' WHEN '9' THEN 'DSP_BEL' WHEN '10' THEN
                       'DSP_CDF' WHEN '11' THEN 'DSP_BGY' WHEN '12' THEN 'Credit Control' WHEN '13' THEN 'Internal Sales' WHEN '14' THEN 'CP_FMT' WHEN '15' THEN 'GBSales' END
                       AS [Sales Location], dbo.SALESTABLE.PURCHORDERFORMNUM AS [Customer Requisition], dbo.SALESLINE.LINEDISC AS [Line Disc], 
                      dbo.SALESLINE.LINEPERCENT AS [Line Percent], dbo.SALESLINE.PRICEGROUPID AS [Price Group], dbo.INVENTDIM.INVENTLOCATIONID AS Warehouse, 
                      dbo.CUSTTABLE.NAMEALIAS AS [Search Name], dbo.CUSTPACKINGSLIPJOUR.LORRYID AS [Lorry ID], dbo.CUSTPACKINGSLIPJOUR.LORRYREGNO AS [Lorry Reg], 
                      dbo.CUSTPACKINGSLIPJOUR.LORRYDRIVER AS [Lorry Driver], CASE dbo.SALESLINE.BLOCKED WHEN '0' THEN 'No' WHEN '1' THEN 'Yes' END AS [Stopped?], 
                      dbo.CUSTPACKINGSLIPTRANS.QTY AS [Qty Delivered], dbo.SALESLINE.SALESPRICE AS [Unit Price], dbo.CUSTPACKINGSLIPJOUR.CREATEDBY AS [SDN Creator], 
                      MONTH(dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) AS Month, YEAR(dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) AS Year, DATEPART(week, 
                      dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) AS WeekNo, CASE MONTH(dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) 
                      WHEN '1' THEN '10' WHEN '2' THEN '11' WHEN '3' THEN '12' WHEN '4' THEN '1' WHEN '5' THEN '2' WHEN '6' THEN '3' WHEN '7' THEN '4' WHEN '8' THEN '5' WHEN
                       '9' THEN '6' WHEN '10' THEN '7' WHEN '11' THEN '8' WHEN '12' THEN '9' END AS [Fin Period], 
                       CASE 
						WHEN MONTH(dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) <= '3' THEN (YEAR(dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) - 1) 
						WHEN MONTH(dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) >= '4' THEN YEAR(dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) 
                       END AS [Fin Year], 
                      CASE WHEN dbo.custpackingsliptrans.salesunit = '100' THEN dbo.CUSTPACKINGSLIPTRANS.SALESUNIT * dbo.CUSTPACKINGSLIPTRANS.QTY WHEN dbo.custpackingsliptrans.salesunit
                       = '1000' THEN dbo.CUSTPACKINGSLIPTRANS.SALESUNIT * dbo.CUSTPACKINGSLIPTRANS.QTY ELSE dbo.CUSTPACKINGSLIPTRANS.QTY END AS [No of Units], 
                      CASE dbo.inventtable.itemgroupid WHEN 'Mortar' THEN (dbo.INVENTTABLE.NETWEIGHT * dbo.CUSTPACKINGSLIPTRANS.QTY) 
                      WHEN 'Sand' THEN (dbo.INVENTTABLE.NETWEIGHT * dbo.CUSTPACKINGSLIPTRANS.QTY) 
                      WHEN 'Decorative Gravel' THEN (dbo.INVENTTABLE.NETWEIGHT * dbo.CUSTPACKINGSLIPTRANS.QTY) 
                      WHEN 'Crushed Aggregates' THEN (dbo.INVENTTABLE.NETWEIGHT * dbo.CUSTPACKINGSLIPTRANS.QTY) 
                      WHEN 'Lintels' THEN (dbo.INVENTTABLE.NETWEIGHT * dbo.CUSTPACKINGSLIPTRANS.QTY) 
                      / 1000 WHEN 'TBeams' THEN (dbo.INVENTTABLE.NETWEIGHT * dbo.CUSTPACKINGSLIPTRANS.QTY) 
                      / 1000 WHEN 'Aggregates' THEN (dbo.INVENTTABLE.NETWEIGHT * dbo.CUSTPACKINGSLIPTRANS.QTY) 
                      WHEN 'Lime' THEN (dbo.INVENTTABLE.NETWEIGHT * dbo.CUSTPACKINGSLIPTRANS.QTY * dbo.UNITCONVERT.FACTOR) 
                      WHEN 'Readymix' THEN (dbo.INVENTTABLE.NETWEIGHT * dbo.CUSTPACKINGSLIPTRANS.QTY) 
                      / 1000 ELSE (dbo.INVENTTABLE.NETWEIGHT * dbo.CUSTPACKINGSLIPTRANS.QTY * dbo.UNITCONVERT.FACTOR) / 1000 END AS Tonnage, 
                      dbo.INVENTTABLE.NETWEIGHT AS [Net Weight], dbo.INVENTTABLE.PRODCOSTA, dbo.INVENTTABLE.PRODCOSTB, dbo.INVENTTABLE.PRODCOSTC, 
                      dbo.INVENTTABLE.PRODCOSTD, dbo.INVENTTABLE.PRODCOSTE, dbo.INVENTTABLE.PRODCOSTF, dbo.INVENTTABLE.PRODCOSTG, dbo.INVENTTABLE.PRODCOSTH, 
                      dbo.INVENTTABLE.QTYPERBALE, dbo.INVENTTABLE.M2PERBALE, dbo.SALESTABLE.SALESTYPE, dbo.SALESTABLE.DLVMODE AS [Delivery Mode], 
                      CONVERT(varchar(12), dbo.SALESTABLE.SHIPPINGDATECONFIRMED, 111) AS [Shipping Date Confirmed], CONVERT(varchar(12), dbo.SALESTABLE.CREATEDDATE, 
                      111) AS [Created Date], dbo.INVENTTABLE.MADETOORDER AS [Made To Order], dbo.CUSTTABLE.CUSTGROUP AS [Cust Group], 
                      dbo.SALESTABLE.CREATEDDATE AS [Order Created Date]
FROM         dbo.CUSTPACKINGSLIPTRANS INNER JOIN
                      dbo.CUSTPACKINGSLIPJOUR ON dbo.CUSTPACKINGSLIPTRANS.PACKINGSLIPID = dbo.CUSTPACKINGSLIPJOUR.PACKINGSLIPID INNER JOIN
                      dbo.SALESLINE ON dbo.CUSTPACKINGSLIPJOUR.SALESID = dbo.SALESLINE.SALESID AND 
                      dbo.CUSTPACKINGSLIPTRANS.INVENTTRANSID = dbo.SALESLINE.INVENTTRANSID INNER JOIN
                      dbo.INVENTTABLE ON dbo.CUSTPACKINGSLIPTRANS.ITEMID = dbo.INVENTTABLE.ITEMID INNER JOIN
                      dbo.INVENTDIM ON dbo.SALESLINE.INVENTDIMID = dbo.INVENTDIM.INVENTDIMID INNER JOIN
                      dbo.CUSTTABLE ON dbo.CUSTPACKINGSLIPJOUR.ORDERACCOUNT = dbo.CUSTTABLE.ACCOUNTNUM INNER JOIN
                      dbo.SALESTABLE ON dbo.SALESLINE.SALESID = dbo.SALESTABLE.SALESID INNER JOIN
                      dbo.DIMENSIONS ON dbo.INVENTTABLE.DIMENSION2_ = dbo.DIMENSIONS.NUM LEFT OUTER JOIN
                      dbo.UNITCONVERT ON dbo.CUSTPACKINGSLIPTRANS.ITEMID = dbo.UNITCONVERT.ITEMID AND 
                      dbo.CUSTPACKINGSLIPTRANS.SALESUNIT = dbo.UNITCONVERT.TOUNIT LEFT OUTER JOIN
                      dbo.EMPLTABLE ON CASE dbo.SALESTABLE.SALESTAKER WHEN '' THEN 'Unknown' ELSE dbo.SALESTABLE.SALESTAKER END = dbo.EMPLTABLE.EMPLID
WHERE     (dbo.CUSTPACKINGSLIPTRANS.DATAAREAID = 'agl') AND (dbo.CUSTPACKINGSLIPJOUR.DATAAREAID = 'agl') AND (dbo.SALESLINE.DATAAREAID = 'agl') AND 
                      (dbo.INVENTTABLE.DATAAREAID = 'vuk') AND (dbo.INVENTDIM.DATAAREAID = 'agl') AND (dbo.CUSTTABLE.DATAAREAID = 'agl') AND 
                      (dbo.DIMENSIONS.DATAAREAID = 'agl') AND (dbo.EMPLTABLE.DATAAREAID = 'val') AND (dbo.SALESTABLE.DATAAREAID = 'agl') AND 
                      (CASE WHEN MONTH(dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) <= '3' THEN (YEAR(dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) - 1) 
                      WHEN MONTH(dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) >= '4' THEN YEAR(dbo.CUSTPACKINGSLIPTRANS.DELIVERYDATE) END = 2013)

Cheers

Paul


Viewing all articles
Browse latest Browse all 23857

Trending Articles



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