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 |
velvettiger
Posting Yak Master
115 Posts |
Posted - 2009-12-29 : 15:53:14
|
Hi guys,Out of 2 stores(1432,1404) I would like to output, the store each customer spent the most money at. Below is what I have so far. Right now the query produces the following results. The query can be seen below the results.cardno subid totalspend84 1432 624.3203 1404 1070.8503 1404 50.0040 1432 149.6652 1432 117.5936 1404 40.0051 1404 120.0051 1432 110.0027 1404 369.60SELECt distinct cardnumber,subsidiaryid,t.spendfrom (select distinct cardnumber,subsidiaryid,sum(DailyTransactionValue)spendfrom SubsidiaryByCustomerByClassTransactionDetailwhere TransactionDate >= '20090101' and TransactionDate < '20100101'and subsidiaryid in (1404,1432)group by SubsidiaryId,cardnumber)tgroup by cardnumber,t.spend,subsidiaryidorder by cardnumber So basically the end result should look like this.Any ideas on how to accomplish this?cardno subid totalspend84 1432 624.3203 1404 1070.8540 1432 149.6652 1432 117.5936 1404 40.0051 1404 120.0027 1404 369.60[/code] |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-29 : 16:05:37
|
Just this should do ...no?select cardnumber,subsidiaryid,sum(DailyTransactionValue) as spendfrom SubsidiaryByCustomerByClassTransactionDetailwhere TransactionDate >= '20090101' and TransactionDate < '20100101'and subsidiaryid in (1404,1432)group by cardnumber,SubsidiaryId Also why did you leave out the other "03 1404" in your result...(without summing up its value). |
|
|
divya.ce
Starting Member
16 Posts |
Posted - 2009-12-30 : 05:10:05
|
Just do max instead of sum in the inner loop ..SELECt distinct cardnumber,subsidiaryid,t.spend as TotalSpendfrom (select distinct cardnumber,subsidiaryid,max(DailyTransactionValue)spendfrom SubsidiaryByCustomerByClassTransactionDetailwhere TransactionDate >= '20090101' and TransactionDate < '20100101'and subsidiaryid in (1404,1432)group by SubsidiaryId,cardnumber)tgroup by cardnumber,subsidiaryid,t.spendorder by cardnumber |
|
|
velvettiger
Posting Yak Master
115 Posts |
Posted - 2009-12-30 : 09:18:12
|
Hi everyone,Thanks for you responses but this doesn't give me the desired results. In the example i gave in my initial explanation showed that some cardnumbers appear twice but the subids are different e.gcardno subid totalspend51 1404 120.0051 1432 110.00 In this example I would like only the row cardno subid totalspend51 1404 120.00 to be outputted as this person has the highest spend at this store. |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-30 : 09:30:56
|
Try this..select cardnumber,subsidiaryid,DailyTransactionValue from (select row_number() over(partition by cardnumber order by DailyTransactionValue desc) as seq, *from SubsidiaryByCustomerByClassTransactionDetailwhere TransactionDate >= '20090101' and TransactionDate < '20100101'and subsidiaryid in (1404,1432)) twhere t.seq = 1 |
|
|
velvettiger
Posting Yak Master
115 Posts |
Posted - 2009-12-30 : 10:06:53
|
Hi,I am using sql server 2000. So row_number etc won't be recognized. |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-12-30 : 10:27:26
|
Well..you should have mentioned it...Try this..select a.cardnumber,a.subsidiaryid,b.DailyTransactionValuefrom SubsidiaryByCustomerByClassTransactionDetail ainner join (select cardnumber,max(DailyTransactionValue) as DailyTransactionValue from SubsidiaryByCustomerByClassTransactionDetail group by cardnumber) b on a.cardnumber = b.cardnumber and a.DailyTransactionValue = b.DailyTransactionValuewhere a.TransactionDate >= '20090101' and a.TransactionDate < '20100101'and a.subsidiaryid in (1404,1432) |
|
|
|
|
|
|
|