Я выполняю запрос с использованием Management Studio 18 к базе данных SQL сервера, и для его запуска на сервере требуется 3-4 минуты. Если я удаляю функции дат, то она запускается на моей машине за 1 минуту, но мне нужна функция дат. Кто-нибудь сможет помочь? Заранее спасибо!
Вот оригинальный запрос, который занимает 3-4 на сервере:
With A AS
(
Select DSS.transactionSynchronizationId, DSS.transactionDate, DSS.transactionTime,
convert (varchar, CAST(DateAdd(minute, DateDiff(minute, 0, DSS.transactionTime), 0) AS smalldatetime), 24) As DSS_TransactionTime_EST, DSS.processedBy,
DSS.transactionType, DSS.country, DSS.state, DSS.partner,
DSS.originUnitPrice, DSS.discount, DSS.taxAmount,
Case When transactionType In ('REFUND', 'CHARGEBACK', 'CREDIT') Then (DSS.taxAmount*-1) Else DSS.taxAmount End As DSS_Tax_Amount,
Case When DSS.country In ('US', 'PR', 'CA') And transactionType In ('REFUND', 'CHARGEBACK', 'CREDIT') Then -DSS.originUnitPrice - DSS.discount
When DSS.country In ('US', 'PR', 'CA') And transactionType Not In ('REFUND', 'CHARGEBACK', 'CREDIT') Then DSS.originUnitPrice - DSS.discount
When DSS.country Not In ('US', 'PR', 'CA') And transactionType In ('REFUND', 'CHARGEBACK', 'CREDIT') Then (-DSS.originUnitPrice - DSS.discount - DSS.taxAmount)
Else DSS.originUnitPrice - DSS.discount - DSS.taxAmount End AS DSS_Gross_Sales
From TableA As DSS
),
C As
(
Select Distinct VTX.[Transaction Synchronization ID], VTX.[Transaction Process Date], VTX.[Transaction Process Time],
convert(varchar, DATEADD (HOUR , -5 , [Transaction Process Time]) , 24) As Vertex_TransactionTime_EST, VTX.[Situs Country Code 2], VTX.[Situs Main Division],
VTX.[Taxpayer Division Code], VTX.[Gross Amount] As VTX_Gross_Amount, Sum (VTX.[Tax Amount]) As VTX_Tax_Amount
From TableB As VTX
Group By VTX.[Transaction Synchronization ID], VTX.[Transaction Process Date], VTX.[Transaction Process Time], VTX.[Situs Country Code 2], VTX.[Situs Main Division],
VTX.[Taxpayer Division Code], VTX.[Gross Amount]
),
B As
(
Select C.[Transaction Synchronization ID], C.[Transaction Process Date], C.[Transaction Process Time],
convert(varchar, DATEADD (HOUR , -5 , C.[Transaction Process Time]) , 24) As Vertex_TransactionTime_EST, C.[Situs Country Code 2], C.[Situs Main Division],
C.[Taxpayer Division Code], Sum (C.VTX_Gross_Amount) As VTX_Gross_Amount, Sum (C.VTX_Tax_Amount) As VTX_Tax_Amount
From C
Group By C.[Transaction Synchronization ID], C.[Transaction Process Date], C.[Transaction Process Time],
convert(varchar, DATEADD (HOUR , -5 , C.[Transaction Process Time]) , 24), C.[Situs Country Code 2], C.[Situs Main Division],
C.[Taxpayer Division Code]
)
Select A.transactionSynchronizationId, B.[Transaction Process Date], B.Vertex_TransactionTime_EST, A.transactionDate, A.DSS_TransactionTime_EST, A.processedBy, A.transactionType,
B.[Situs Country Code 2], A.country, B.[Situs Main Division], A.state, A.partner, B.[Taxpayer Division Code], B.VTX_Gross_Amount, B.VTX_Tax_Amount, A.originUnitPrice,
A.discount, A.DSS_Tax_Amount, A.DSS_Gross_Sales, Cast ((A.DSS_Gross_Sales - B.VTX_Gross_Amount) As decimal (10,2)) As 'Gross Sales Diff',
A.DSS_Tax_Amount, Cast (A.DSS_Tax_Amount - B.VTX_Tax_Amount As decimal (10,2)) As 'Tax Diff'
From A Join B
On A.transactionSynchronizationId = B.[Transaction Synchronization ID]
Вот запрос, который выполняется быстрее:
With A AS
(
Select DSS.transactionSynchronizationId, DSS.transactionDate, DSS.transactionTime,
convert (varchar, CAST(DateAdd(minute, DateDiff(minute, 0, DSS.transactionTime), 0) AS smalldatetime), 24) As DSS_TransactionTime_EST, DSS.processedBy,
DSS.transactionType, DSS.country, DSS.state, DSS.partner,
DSS.originUnitPrice, DSS.discount, DSS.taxAmount,
Case When transactionType In ('REFUND', 'CHARGEBACK', 'CREDIT') Then (DSS.taxAmount*-1) Else DSS.taxAmount End As DSS_Tax_Amount,
Case When DSS.country In ('US', 'PR', 'CA') And transactionType In ('REFUND', 'CHARGEBACK', 'CREDIT') Then -DSS.originUnitPrice - DSS.discount
When DSS.country In ('US', 'PR', 'CA') And transactionType Not In ('REFUND', 'CHARGEBACK', 'CREDIT') Then DSS.originUnitPrice - DSS.discount
When DSS.country Not In ('US', 'PR', 'CA') And transactionType In ('REFUND', 'CHARGEBACK', 'CREDIT') Then (-DSS.originUnitPrice - DSS.discount - DSS.taxAmount)
Else DSS.originUnitPrice - DSS.discount - DSS.taxAmount End AS DSS_Gross_Sales
From TableA As DSS
),
C As
(
Select Distinct VTX.[Transaction Synchronization ID], VTX.[Transaction Process Date], VTX.[Transaction Process Time],
convert(varchar, DATEADD (HOUR , -5 , [Transaction Process Time]) , 24) As Vertex_TransactionTime_EST, VTX.[Situs Country Code 2], VTX.[Situs Main Division],
VTX.[Taxpayer Division Code], VTX.[Gross Amount] As VTX_Gross_Amount, Sum (VTX.[Tax Amount]) As VTX_Tax_Amount
From TableB As VTX
Group By VTX.[Transaction Synchronization ID], VTX.[Transaction Process Date], VTX.[Transaction Process Time], VTX.[Situs Country Code 2], VTX.[Situs Main Division],
VTX.[Taxpayer Division Code], VTX.[Gross Amount]
),
B As
(
Select C.[Transaction Synchronization ID], C.[Transaction Process Date], C.[Transaction Process Time],
C.[Situs Country Code 2], C.[Situs Main Division],
C.[Taxpayer Division Code], Sum (C.VTX_Gross_Amount) As VTX_Gross_Amount, Sum (C.VTX_Tax_Amount) As VTX_Tax_Amount
From C
Group By C.[Transaction Synchronization ID], C.[Transaction Process Date], C.[Transaction Process Time],
C.[Situs Country Code 2], C.[Situs Main Division],
C.[Taxpayer Division Code]
)
Select A.transactionSynchronizationId, B.[Transaction Process Date], A.transactionDate, A.DSS_TransactionTime_EST, A.processedBy, A.transactionType,
B.[Situs Country Code 2], A.country, B.[Situs Main Division], A.state, A.partner, B.[Taxpayer Division Code], B.VTX_Gross_Amount, B.VTX_Tax_Amount, A.originUnitPrice,
A.discount, A.DSS_Tax_Amount, A.DSS_Gross_Sales,
A.DSS_Tax_Amount, Cast (A.DSS_Tax_Amount - B.VTX_Tax_Amount As decimal (10,2)) As 'Tax Diff'
From A Join B
On A.transactionSynchronizationId = B.[Transaction Synchronization ID]