OK!
Got something here!
If is use a with index to the tbltrans_inventory table everything works in 2 seconds and I get indexded execution plan !! ( FROM VISTA.dbo.tblTrans_Inventory WITH (INDEX = indCollectedTranDate) )
!I think this is it!
P.S. If i use it on the view it slows down significantly!
declare @DateFrom DATETIME
declare @DateTo DATETIME
declare @CollectedUserCodeV nvarchar(50)
set @CollectedUserCodeV = '-1'
declare @CollectedWorkstationCodeV nvarchar(50)
set @CollectedWorkstationCodeV = '-1'
set @DateFrom = '20170101' --- add extra dates so we do not loose some
set @DateTo = '20170105'
declare @DateFromX as datetime
set @dateFromx = DATEADD(d, -1, @DateFrom)
declare @DateToX as datetime
set @dateTox = DATEADD(d, +2, @DateTo)
--AS
--BEGIN
--TRUNCATE TABLE dbo.ZZ_tblRPTConcessionEOrders
-- SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @CollectedUserCode int
declare @CollectedWorkstationCode nvarchar(50)
if (@CollectedUserCodeV = '-1' or @CollectedUserCodeV = 'UserNo')
Begin
set @CollectedUserCode = -1
end
else
begin
set @CollectedUserCode = cast(@CollectedUserCodeV as int)
end
if (@CollectedWorkstationCodeV = '-1' or @CollectedWorkstationCodeV = 'WrkstnCode')
begin
set @CollectedWorkstationCode ='-1'
end
else
begin
set @CollectedWorkstationCode = @CollectedWorkstationCodeV
end
SELECT CASE WHEN ISNULL(TransI_decActualNoOfItems,0) < 0 THEN TransI_dtmRealTransTime --Refund
ELSE CASE WHEN tblBooking_Header.BookingH_intNextBookingNo IS NULL THEN TransI_dtmRealTransTime ELSE TransI_dtmDateCollected END
END AS CollectedDate,
CASE WHEN ISNULL(TransI_decActualNoOfItems,0) < 0 THEN User_intUserNo --Refund
ELSE CASE WHEN tblBooking_Header.BookingH_intNextBookingNo IS NULL THEN User_intUserNo ELSE ZZ_tblTrans_Inventory_EXT.TransI_intPickupUser END
END AS CollectedUserCode,
CASE WHEN ISNULL(TransI_decActualNoOfItems,0) < 0 THEN Workstation_strCode --Refund
ELSE CASE WHEN tblBooking_Header.BookingH_intNextBookingNo IS NULL THEN Workstation_strCode ELSE ZZ_tblTrans_Inventory_EXT.TransI_strPickupWorkstn END
END AS CollectedWorkStationCode,
tblItem.HOPK AS ItemCode,
tblItem.Item_strItemDescription AS ItemDescription,
ISNULL(TransI_decActualNoOfItems,0) AS Quantity,
ISNULL(TransI_curValueEach,0) AS GrossUP,
CAST(ROUND(ISNULL(TransI_curValueEach,0)*ISNULL(TransI_decActualNoOfItems,0),2) AS MONEY) AS GrossValue,
CAST(ROUND((ISNULL(TransI_curSTaxEach,0)+
ISNULL(TransI_curSTaxEach2,0)+
ISNULL(TransI_curSTaxEach3,0)+
ISNULL(TransI_curSTaxEach4,0))*ISNULL(TransI_decActualNoOfItems,0),2) AS MONEY) AS VATValue,
--NET = GROSS - TAX
CAST(ROUND(ISNULL(TransI_curValueEach,0)*ISNULL(TransI_decActualNoOfItems,0),2)-
(ROUND((ISNULL(TransI_curSTaxEach,0)+
ISNULL(TransI_curSTaxEach2,0)+
ISNULL(TransI_curSTaxEach3,0)+
ISNULL(TransI_curSTaxEach4,0))*ISNULL(TransI_decActualNoOfItems,0),2)) AS MONEY) AS NetValue
Into #TempEorder
FROM VISTA.dbo.tblTrans_Inventory WITH (INDEX = indCollectedTranDate)
INNER JOIN VISTA.dbo.tblItem
ON tblItem.Item_strItemId = tblTrans_Inventory.Item_strItemId
LEFT OUTER JOIN VISTA.dbo.ZZ_tblTrans_Inventory_EXT
ON tblTrans_Inventory.TransI_lgnNumber = ZZ_tblTrans_Inventory_EXT.TransI_lgnNumber
AND tblTrans_Inventory.TransI_intSequence = ZZ_tblTrans_Inventory_EXT.TransI_intSequence
LEFT JOIN VISTA.DBO.tblBooking_Header
ON tblBooking_Header.TransC_lgnNumber = tblTrans_Inventory.TransI_lgnNumber
WHERE tblItem.Item_strBookingFee <> 'Y'
AND TransI_dtmDateCollected < '9999-01-01 00:00:00.000'
AND TransI_dtmDateCollected between @DateFromX and @DateToX
-- select * from #TempEorder
CREATE CLUSTERED INDEX Tmp_EticketRPT_IDX_C_Users_UserID ON #TempEorder(CollectedDate)
SELECT CAST(CONVERT(VARCHAR, CASE WHEN Datepart(hh, CollectedDate) < 6 THEN DATEADD(d, -1, CollectedDate) ELSE CollectedDate END, 112) AS DATETIME) AS CollectedDate,
CollectedUserCode, ISNULL(User_strLastName,'') + ' ' + ISNULL(User_strFirstName,'') AS CollectedUserName,
CollectedWorkStationCode, tblWorkstation.Workstation_strName AS CollectedWorkStationName,
ItemCode, ItemDescription,
SUM(Quantity) AS Quantity,
SUM(GrossValue) AS GrossValue,
SUM(VATValue) AS VATValue,
SUM(NetValue) AS NetValue
FROM #TempEorder
LEFT OUTER JOIN dbo.tblWorkstation WITH (NOLOCK) ON tblWorkstation.Workstation_strCode = CollectedWorkStationCode
LEFT OUTER JOIN dbo.tblUser WITH (NOLOCK) ON tblUser.User_intUserNo = CollectedUserCode
WHERE CAST(CONVERT(VARCHAR, CASE WHEN Datepart(hh, CollectedDate) < 6 THEN DATEADD(d, -1, CollectedDate) ELSE CollectedDate END, 112) AS DATETIME) between @dateFrom and @DateTo
and ( ( @CollectedUserCode = -1 ) OR ( #TempEorder.CollectedUserCode = @CollectedUserCode))
and ( ( @CollectedWorkstationCode = '-1' ) OR ( #TempEorder.CollectedWorkStationCode = @CollectedWorkstationCode))
GROUP BY CAST(CONVERT(VARCHAR, CASE WHEN Datepart(hh, CollectedDate) < 6 THEN DATEADD(d, -1, CollectedDate) ELSE CollectedDate END, 112) AS DATETIME),
CollectedUserCode,
ISNULL(User_strLastName,'') + ' ' + ISNULL(User_strFirstName,''),
CollectedWorkStationCode, tblWorkstation.Workstation_strName,
ItemCode, ItemDescription
drop table #TempEorder