Nu har jag hamnat i SQL-problem igen. Kör MS SQLSrvr 2000 och försöker räkna med följande frågor.
Denna fråga:
DECLARE @ImportSession uniqueidentifier
DECLARE @AccountNo nvarchar(10)
SET @Importsession = '8A82E52D-E42E-41D5-9941-989C544A0234'
SET @AccountNo = '3010' -- Fakturerade larm
SELECT COUNT(tAccounts.sAccountNo) AS accountnumbaz, SUBSTRING(sVerificationDate, 1, 6) AS sDate
FROM tVerTransactions INNER JOIN
tVerifications ON tVerTransactions.tVerificationId = tVerifications.tVerificationId INNER JOIN
tAccounts ON tVerTransactions.tAccountId = tAccounts.tAccountId
WHERE (tVerifications.tImportSessionId = @ImportSession)
AND (tAccounts.sAccountNo = @AccountNo)
AND (tVerifications.sVerificationName = 'B')
AND ([B]tVerTransactions.mTransSize < 0[/B])
GROUP BY SUBSTRING(sVerificationDate, 1, 6)
ORDER BY sDate
Minus denna fråga:
SELECT COUNT(tAccounts.sAccountNo) AS accountnumbaz, SUBSTRING(sVerificationDate, 1, 6) AS sDate
FROM tVerTransactions INNER JOIN
tVerifications ON tVerTransactions.tVerificationId = tVerifications.tVerificationId INNER JOIN
tAccounts ON tVerTransactions.tAccountId = tAccounts.tAccountId
WHERE (tVerifications.tImportSessionId = @ImportSession)
AND (tAccounts.sAccountNo = @AccountNo)
AND (tVerifications.sVerificationName = 'B')
AND ([B]tVerTransactions.mTransSize > 0[/B])
GROUP BY SUBSTRING(sVerificationDate, 1, 6)
ORDER BY sDate
Frågorna är identiska, bortsett från större/mindre än tecknet.
Alltså, antal transaktioner som är mindre än 0 subtraherat med antal transaktioner som är större än 0 uppdelat månadsvis. Frågorna fungerar bra när man kör dom ensamma, men när jag skall subtrahera tar det stopp i skallen på mig!
DECLARE @ImportSession uniqueidentifier
DECLARE @AccountNo nvarchar(10)
SET @Importsession = '8A82E52D-E42E-41D5-9941-989C544A0234'
SET @AccountNo = '3010' -- Fakturerade larm
SELECT
COUNT(tAccountsLess.sAccountNo)-COUNT(tAccountsGreater.sAccountNo) AS accountnumbaz,
SUBSTRING(sVerificationDate, 1, 6) AS sDate
FROM tVerifications
INNER JOIN tVerTransactions tVerTransactionsGreater ON
tVerTransactionsGreater.tVerificationId = tVerifications.tVerificationId
AND (tVerTransactionsGreater.mTransSize > 0)
INNER JOIN tAccounts tAccountsGreater ON
tVerTransactionsGreater.tAccountId = tAccountsGreater.tAccountId
INNER JOIN tVerTransactions tVerTransactionsLess ON
tVerTransactionsLess.tVerificationId = tVerifications.tVerificationId
AND (tVerTransactionsLess.mTransSize < 0)
INNER JOIN tAccounts tAccountsLess ON
tVerTransactionsLess.tAccountId = tAccountsLess.tAccountId
WHERE (tVerifications.tImportSessionId = @ImportSession)
AND (
(tAccountsGreater.sAccountNo = @AccountNo)
OR (tAccountsLess.sAccountNo = @AccountNo)
)
AND (tVerifications.sVerificationName = 'B')
GROUP BY SUBSTRING(sVerificationDate, 1, 6)
ORDER BY sDate
Eftersom jag uppenbarligen inte kan testa koden är den nog inte 100%-ig i sitt nuvarande utförande. Men huvudidén är att använda FROM tVerifications och sedan JOINa tVerTransactions och tAccounts två gånger och sedan subtrahera.
SELECT sum(case when tVerTransactions.mTransSize > 0 then 1 else -1 end) as accountnumbaz,
SUBSTRING(sVerificationDate, 1, 6) AS sDate
FROM tVerTransactions INNER JOIN
tVerifications ON tVerTransactions.tVerificationId = tVerifications.tVerificationId INNER JOIN
tAccounts ON tVerTransactions.tAccountId = tAccounts.tAccountId
WHERE (tVerifications.tImportSessionId = @ImportSession)
AND (tAccounts.sAccountNo = @AccountNo)
AND (tVerifications.sVerificationName = 'B')
AND (tVerTransactions.mTransSize <> 0)
GROUP BY SUBSTRING(sVerificationDate, 1, 6)
ORDER BY sDate