---
title: "Räkna med två frågor och subtrahera"
type: "forum-thread"
url: "https://www.webforum.nu/amne/databaser-sql/178960-räkna-med-två-frågor-och-subtrahera"
topic: "Databaser & SQL"
topic_url: "https://www.webforum.nu/amne/databaser-sql"
author: "KrpSvedberg"
published: "2009-06-11T13:40:01.000Z"
updated: "2009-06-12T12:47:02.000Z"
replies: 6
views: 630
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/databaser-sql/178960-räkna-med-två-frågor-och-subtrahera"
---

# Räkna med två frågor och subtrahera

## #1 — KrpSvedberg, 2009-06-11T13:40Z

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!

Permalänk: https://www.webforum.nu/p/178960

## #2 — nitro2k01, 2009-06-11T14:32Z

Vilken tabell tillhör fältet sVerificationDate?

Permalänk: https://www.webforum.nu/p/2190354

## #3 — KrpSvedberg, 2009-06-11T14:33Z

> **[nitro2k01 skrev:](https://www.webforum.nu/p/1470694)**
>
> Vilken tabell tillhör fältet sVerificationDate?

Tabellen 'tVerifications'

Permalänk: https://www.webforum.nu/p/2190355

## #4 — nitro2k01, 2009-06-11T14:50Z

```
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.

Permalänk: https://www.webforum.nu/p/2190356

## #5 — LarsG, 2009-06-12T07:44Z

```
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
```

Permalänk: https://www.webforum.nu/p/2190403

## #6 — KrpSvedberg, 2009-06-12T11:29Z

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

Permalänk: https://www.webforum.nu/p/2190415

## #7 — nitro2k01, 2009-06-12T12:47Z

> **[KrpSvedberg skrev:](https://www.webforum.nu/p/1470761)**
>
> 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.

Permalänk: https://www.webforum.nu/p/2190421

---

Tråden på webben: https://www.webforum.nu/amne/databaser-sql/178960-räkna-med-två-frågor-och-subtrahera
