Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
The above code actually worked but I wanted to make it one query, does anyone have any ideas.Thanks much
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts
Posted - 2009-03-16 : 10:02:03
This is to find the cardno which has most amount spent on it,
select top 1 cardno,sum(spend)from transactionsgroup by cardnoorder by sum(spend) desc
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts
Posted - 2009-03-16 : 10:05:14
quote:The above code actually worked ...
Not sure how that worked since you don't have a group by clause anywhere there. Even if you had it, your first query and second query will fetch you the same result. It'll list the total sum against all card numbers.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-03-16 : 10:17:22
if sql 2005
select top 1 cardno,... other columnsfrom(select *,sum(spend) over (partition by cardno) as cardsumfrom transactions)torder by cardsum desc