Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need help in a Select Query

Author  Topic 

matutano
Starting Member

3 Posts

Posted - 2009-05-06 : 11:31:59
Hi there!

I'm developing a small report program, and I'm starting to be confused in a small small detail...
table:
Transactions -> CardID(Nvarchar), transactionType(int), value(int)

I want to sum all values having transactiontype =1 , and then subtract a Sum of all values, where transactionsType=2 . The problem is that... i can't have this done.... still.. wanna count all transactions having transactiontype = 2.

The problem is that... when i try to count, tables, and once it's same table, if Card= 'A' have 2 transactions (one of each type), i'm going to end having 4 rows... Can anyone help out here?? please :S

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2009-05-06 : 11:43:43
select (Select Sum(value) from Transactions a where a.TransactionType = 1) - (Select Sum(value) from Transactions a where a.TransactionType = 2)


Success is 10% Intelligence, 70% Determination, and 22% Stupidity.
\_/ _/ _/\_/ _/\_/ _/ _/- 881
Go to Top of Page

matutano
Starting Member

3 Posts

Posted - 2009-05-06 : 11:58:41
hey thanks!!

Bu i want to apply it to all Cards in table, so I don't think I can calculate that on my select query :S
Go to Top of Page

matutano
Starting Member

3 Posts

Posted - 2009-05-06 : 12:12:27
quote:
Originally posted by matutano

hey thanks!!

Bu i want to apply it to all Cards in table, so I don't think I can calculate that on my select query :S



Ok i've just found it out:


select (Select Sum(value) from transactions As A where A.CardID = C.CardID and (A.transtype = 4 or A.transtype = 5)) -
(Select Sum(value) from transactions As B where B.CardID = C.CardID and (b.transtype = 1 or b.transtype = 2)), CardID from Transactions AS C group by CardId;

Thanks for your help and quick answer ;)
Go to Top of Page
   

- Advertisement -