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.
Author |
Topic |
BlackIce662
Starting Member
11 Posts |
Posted - 2014-06-09 : 10:04:52
|
Good DayThis is my SQL Query. Everything after the 3rd line is required due to dependancies.[CODE]SELECT Customer.EMail, Amount=SUM(ABS((SELECT Top 1 Amount+VATAmount FROM Kitty WITH(NOLOCK) Where Kitty.ResID=FreeWinners.ResID AND TransType='PZ')))FROM FreeWinners INNER JOIN Reservations WITH(NOLOCK) ON (Reservations.ResID = FreeWinners.ResID) INNER JOIN Customer WITH(NOLOCK) ON (Reservations.EMail = Customer.EMail) WHERE FreeWinners.Comments_Approved!='No' AND FreeWinners.ResID IN (SELECT ResID FROM Kitty WITH(NOLOCK) Where Kitty.ResID=FreeWinners.ResID AND TransType='PZ' AND Year(TransDate)>1900 AND Approved='Yes') AND FreeWinners.ResID NOT IN (SELECT ResID FROM Kitty WITH(NOLOCK) Where Kitty.ResID=FreeWinners.ResID AND TransType='PC') GROUP BY Customer.EMail, FreeWinners.ResIDORDER BY Amount DESC[/CODE]Some sample dataEMail Amountbct@beyond.com 46269.75matomel@ag.co.za 29700.00yolan@jdo.co.za 26400.00sgre@nics.co.za 25200.00robgreish@hmail.com 22500.00bct@beyond.com 20400.00ghskhan@mail.com 17940.00carstens@mail.com 16220.00bishopk@sa.net 16100.00bishopk@sa.net 16100.00What I require is that the amounts for bct@beyond.com be added togetherand then bishopk@sa.net values added together giving me the first and second records I require fordisplay. This will filter all through the data so if there are 5 amounts for bct@beyond.com I willget a SUM returned for that email address based on the 5 amounts and then be sorted from highest to lowest.So my output would then be EMail Amountbct@beyond.com 66669.75bishopk@sa.net 32200.00matomel@ag.co.za 29700.00I realise I can't have Subquery for the SUM and I'm not sure if I'm using the Grouping correctly.Any help will be greatly appreciated. |
|
djj55
Constraint Violating Yak Guru
352 Posts |
Posted - 2014-06-09 : 13:35:03
|
Try using a couple of cte's to do your sub queriesdjj |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-06-09 : 13:43:45
|
[code]WITH SomeCTE (Email, Amount)AS( SELECT Customer.EMail, Amount=SUM(ABS((SELECT Top 1 Amount+VATAmount FROM Kitty WITH(NOLOCK) Where Kitty.ResID=FreeWinners.ResID AND TransType='PZ'))) FROM FreeWinners INNER JOIN Reservations WITH(NOLOCK) ON (Reservations.ResID = FreeWinners.ResID) INNER JOIN Customer WITH(NOLOCK) ON (Reservations.EMail = Customer.EMail) WHERE FreeWinners.Comments_Approved!='No' AND FreeWinners.ResID IN (SELECT ResID FROM Kitty WITH(NOLOCK) Where Kitty.ResID=FreeWinners.ResID AND TransType='PZ' AND Year(TransDate)>1900 AND Approved='Yes') AND FreeWinners.ResID NOT IN (SELECT ResID FROM Kitty WITH(NOLOCK) Where Kitty.ResID=FreeWinners.ResID AND TransType='PC') GROUP BY Customer.EMail, FreeWinners.ResID ORDER BY Amount DESC)SELECT Email, SUM(Amount) AS AmountFROM SomeCTEGROUP BY EmailORDER BY Amount DESC[/code]Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
|
|
|