我编写了这个查询,因为我使用了临时表(第一次),它大大破坏了查询的性能。
这是查询。你能发现我正在犯的大错吗?
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
DECLARE @StartDate DATETIME
SET @StartDate = '20160720';
DECLARE @EndDate DATETIME
SET @EndDate = '20160729 23:59:59';
Select
SUM(c.[Chargeable Weight]) AS [1stchargeableweights],
mm.MovementRef [movementref],
MAX (ts.systemstamp) [lastdate]
Into #1schargslack
FROM dbo.whsConsignment wc
INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference
INNER JOIN dbo.tsAdditionalColInfo ts on ts.[ConsignmentReference]= wc.ConsignmentReference
inner join dbo.movConLink m on m.ConsignmentReference = c.[Consignment Reference]
inner join dbo.movMovement mm on mm.MovementID = m.MovementID
--INNER JOIN dbo.whsHeader wh ON wh.WhsHeaderID = wc.whsHeaderID
--AND wc.whsHeaderID = wh.WhsHeaderID
AND wc.StatusCode = 'NL'
AND c.[Service Type] = 'F'
AND ts.SlackNoSlack = 'slack'
or ts.slacknoslack like 'slack - tripped%'
GROUP by mm.MovementRef
Select
SUM(c.[Chargeable Weight]) AS [1stchargeableweightns],
mm.MovementRef [movementref],
MAX (ts.systemstamp) [lastdate]
Into #1chargns
FROM dbo.whsConsignment wc INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference
INNER JOIN dbo.tsAdditionalColInfo ts on ts.[ConsignmentReference]= wc.ConsignmentReference
inner join dbo.movConLink m on m.ConsignmentReference = c.[Consignment Reference]
inner join dbo.movMovement mm on mm.MovementID = m.MovementID
--INNER JOIN dbo.whsHeader wh ON wh.WhsHeaderID = wc.whsHeaderID
--AND wc.whsHeaderID = wh.WhsHeaderID
AND wc.StatusCode = 'NL'
AND c.[Service Type] = 'F'
and ts.SlackNoSlack like 'no slack%'
GROUP BY mm.MovementRef
Select
SUM(c.[Chargeable Weight]) AS [2ndchargeableweights],
mm.MovementRef [movementref],
MAX (ts.systemstamp) [lastdate]
Into #2chargs
FROM dbo.whsConsignment wc
INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference
INNER JOIN dbo.tsAdditionalColInfo ts on ts.[ConsignmentReference]= wc.ConsignmentReference
inner join dbo.movConLink m on m.ConsignmentReference = c.[Consignment Reference]
inner join dbo.movMovement mm on mm.MovementID = m.MovementID
-- INNER JOIN dbo.whsHeader wh ON wh.WhsHeaderID = wc.whsHeaderID
-- AND wc.whsHeaderID = wh.WhsHeaderID
AND wc.StatusCode = 'NL'
AND c.[Service Type] = 'C'
AND ts.SlackNoSlack = 'slack'
or ts.slacknoslack like 'slack - tripped%'
GROUP BY mm.MovementRef
Select
SUM(c.[Chargeable Weight]) AS [2ndchargeableweightns],
mm.MovementRef [movementref],
MAX (ts.systemstamp) [lastdate]
Into #2chargns
FROM dbo.whsConsignment wc
INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference
INNER JOIN dbo.tsAdditionalColInfo ts on ts.[ConsignmentReference]= wc.ConsignmentReference
inner join dbo.movConLink m on m.ConsignmentReference = c.[Consignment Reference]
inner join dbo.movMovement mm on mm.MovementID = m.MovementID
-- INNER JOIN dbo.whsHeader wh ON wh.WhsHeaderID = wc.whsHeaderID
-- AND wc.whsHeaderID = wh.WhsHeaderID
AND wc.StatusCode = 'NL'
AND c.[Service Type] = 'C'
and ts.SlackNoSlack like 'no slack%'
group by mm.MovementRef
Select
SUM(c.[Chargeable Weight]) AS [2ndchargeableweightlate],
mm.MovementRef [movementref],
MAX (ts.systemstamp) [lastdate]
Into #2charglate
FROM dbo.whsConsignment wc
INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference
INNER JOIN dbo.tsAdditionalColInfo ts on ts.[ConsignmentReference]= wc.ConsignmentReference
inner join dbo.movConLink m on m.ConsignmentReference = c.[Consignment Reference]
inner join dbo.movMovement mm on mm.MovementID = m.MovementID
-- INNER JOIN dbo.whsHeader wh ON wh.WhsHeaderID = wc.whsHeaderID
-- AND wc.whsHeaderID = wh.WhsHeaderID
AND wc.StatusCode = 'NL'
AND c.[Service Type] = 'C'
and ts.SlackNoSlack like '%late%'
group by mm.movementref
SELECT --wh.WhsHeaderID,
mu.Number AS Unit,
m.MovementRef AS Reference,
wh.ArrDepDate AS [Departure Date],
wh.LastScannedTime AS [Last Scan],
CONVERT(VARCHAR(5), wh.ArrDepDate, 108) AS [Scheduled Departure (h)],
CONVERT(VARCHAR(5), wh.LastScannedTime, 108) AS [Last Scan (h)],
CASE
WHEN
(
m.DestinationPoint = 'SF'
AND DATEDIFF(MINUTE, wh.ArrDepDate, wh.LastScannedTime) > 180
)
OR (m.DestinationPoint <> 'SF' AND DATEDIFF(MINUTE, wh.ArrDepDate, wh.LastScannedTime) > 20) THEN
'Late'
ELSE
'On Time'
END AS Status,
CAST(ch.ChargeableWt + ISNULL(LO.LeftOffWt, 0) AS DECIMAL(18, 2)) AS [Planned Chargeable Wt],
Cast (isnull ([1stchargeableweights], 0) as decimal (18, 2)) as [1st Dep Left off Chargeable Wt With Slack],
Cast (isnull ([1stchargeableweightns], 0) as decimal (18, 2)) as [1st Dep Left off Chargeable Wt No Slack],
Cast (isnull ([2ndchargeableweights], 0) as decimal (18, 2)) as [2nd Dep Left off Chargeable Wt With Slack],
Cast (isnull ([2ndchargeableweightns], 0) as decimal (18, 2)) as [2nd Dep Left off Chargeable Wt No Slack],
Cast (isnull ([2ndchargeableweightlate], 0) as decimal (18, 2)) as [2nd Dep Left off Chargeable Wt Late],
CAST(ch.ChargeableWt AS DECIMAL(18, 2)) AS [Actual Chargeable Wt],
CASE
WHEN m.DestinationPoint IN ( 'AT', 'FR', 'PL', 'NL', 'BE', 'ES', 'IT', 'CH', 'CZ', 'TU', 'MT', 'DE' ) THEN
22000
ELSE
20000
END AS Target,
CASE
WHEN m.DestinationPoint IN ( 'AT', 'FR', 'PL', 'NL', 'BE', 'ES', 'IT', 'CH', 'CZ', 'TU', 'MT', 'DE' ) THEN
CAST(ch.ChargeableWt - 22000 AS DECIMAL(18, 2))
ELSE
CAST(ch.ChargeableWt - 20000 AS DECIMAL(18, 2))
END AS Variance
--ISNULL(
-- STUFF(
-- (
-- SELECT ', ' + COALESCE(R.Region, 'Region not found') + ': ' + SUBSTRING(cnor.PostCode,1,CHARINDEX( ' ',cnor.PostCode + ' ') - 1) +
-- ' (' + COALESCE(DC.Description, 'NA') + ')'
-- FROM dbo.whsConsignment wc
-- INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference AND c.Cancelled = 0
-- INNER JOIN dbo.genAddress cnor ON cnor.AddressID = c.Consignor
-- LEFT OUTER JOIN dbo.disConsignment AS DC ON DC.ConsignmentRef = wc.ConsignmentReference AND DC.WhsHeaderID = wc.whsHeaderID
-- LEFT JOIN dbo.lstPostcode P ON P.Postcode = cnor.PostcodePrefix
-- LEFT JOIN dbo.lstPostcodeRegion R ON R.RegionID = P.RegionID
-- WHERE wc.whsHeaderID = wh.WhsHeaderID AND wc.StatusCode = 'NL'
-- FOR XML PATH('')
-- ),
-- 1,1,''
-- )
-- ,'') AS [Postcodes & Discrepancies]
FROM dbo.movMovement m
inner join #1schargslack on #1schargslack.movementref = m.MovementRef
inner join #1chargns on #1chargns.movementref = m.MovementRef
inner join #2chargs on #2chargs.movementref = m.MovementRef
inner join #2chargns on #2chargns.MovementRef = m.MovementRef
inner join #2charglate on #2charglate.movementref = m.MovementRef
INNER JOIN dbo.movUnit mu ON mu.UnitID = m.TrailerID
INNER JOIN dbo.whsHeader wh ON wh.RunMovID = m.MovementID AND wh.LocalDepotCode = 'NOT' AND wh.WhsJobType = 'CL' AND wh.WhsJobStatus = 'COMP'
LEFT JOIN dbo.whsHeaderComments whc ON whc.ID = wh.WhsHeaderID
INNER JOIN (
SELECT MovementID,
SUM(c.[Chargeable Weight]) AS ChargeableWt
FROM dbo.movConLink mc
INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = mc.ConsignmentReference AND c.Cancelled = 0
GROUP BY mc.MovementID
) ch ON ch.MovementID = m.MovementID
LEFT JOIN (
SELECT wc.whsHeaderID,
SUM(c.[Chargeable Weight]) AS LeftOffWt
FROM dbo.whsConsignment wc
INNER JOIN dbo.cgtConsignment c ON c.[Consignment Reference] = wc.ConsignmentReference AND c.Cancelled = 0
INNER JOIN dbo.genAddress cnor ON cnor.AddressID = c.Consignor
WHERE StatusCode IN ( 'NL' )
GROUP BY wc.whsHeaderID
) LO ON LO.whsHeaderID = wh.WhsHeaderID
WHERE wh.ArrDepDate
BETWEEN @StartDate AND @EndDate
ORDER BY m.MovementRef;
答案 0 :(得分:0)
如果您正在填充临时表,然后只调用一次,您可以查看使用CTE。它将节省SQL Server填充表的大量时间。