webForumDet fria alternativet

Räkna med två frågor och subtrahera

Databaser & SQL

6 svar · 611 visningar · startad av KrpSvedberg

Medlem sedan okt. 200558 inlägg
Frågan#1

God dagens!

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!

Medlem sedan aug. 20039 340 inlägg
#2

Vilken tabell tillhör fältet sVerificationDate?

Medlem sedan okt. 200558 inlägg
#3

nitro2k01 skrev:

Vilken tabell tillhör fältet sVerificationDate?

Tabellen 'tVerifications'

Medlem sedan aug. 20039 340 inlägg
#4
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.

Medlem sedan dec. 200012 464 inlägg
#5
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
Medlem sedan okt. 200558 inlägg
#6

Vackert som få. Det är som poesi i mina ögon... Tack!!!! :birp

Medlem sedan aug. 20039 340 inlägg
#7

KrpSvedberg skrev:

Vackert som få. Det är som poesi i mina ögon... Tack!!!! :birp

Jag måste hålla med. Det märks att LarsG kan sin SQL.

261 ms totalt · 4 externa anrop · v20260731065814-full.a51de22e
125 ms — deklarationer (db)
0 ms — hämta statistik (cache)
134 ms — hämta tråd, inlägg och bilagor (db)
123 ms — ändringar (db)