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 |
|
nivaskhan
Starting Member
17 Posts |
Posted - 2002-07-10 : 01:17:30
|
| Dear all,I have a table as follows.I want to get the sum of points for each login after giving some discount of say 500 points for each.But the discount will be given based on the rank of individual Login.Login Rank PointsAA 1 50AA 3 150AA 5 25BB 1 50BB 2 150BB 3 550BB 4 325BB 5 275CC 3 250CC 4 500CC 5 350Suppose BB has a total of 1350 points i have to give him a discount of 500 points,it will be given based on the rank he has obtainedBB 1 50 - fully discountedBB 2 150 - fully discountedBB 3 550 - partially discounted(300 points disconnted & 250 points kept for accounting)BB 4 325 - kept for accountingBB 5 275 - kept for accountingLogin TotalPoints(Accounting) BB 850So,in total BB has 850 points to be kept for accounting.And i want this points(850) to be split based on rank .Such asLogin TotalPoints(Accounting) Rank < 3 Rank >= 4BB 850 250 600I need to get the individual points and their split up grouped by rank for all the logins asLogin TOTAL POINTS Rank < 3 Rank >=4AA 0 0 0BB 850 250 600CC 600 0 600Thanks in advanceNivasRegards,Nivas |
|
|
YellowBug
Aged Yak Warrior
616 Posts |
Posted - 2002-07-10 : 05:58:16
|
| This may help:declare @tblA table(Login varchar(10), Rank int, Points int) insert @tblA values('AA', 1, 50 )insert @tblA values('AA', 3 ,150 )insert @tblA values('AA', 5 ,25 )insert @tblA values('BB', 1 ,50 )insert @tblA values('BB', 2 ,150 )insert @tblA values('BB', 3 ,550 )insert @tblA values('BB', 4 ,325 )insert @tblA values('BB', 5 ,275 )insert @tblA values('CC', 3 ,250 )insert @tblA values('CC', 4 ,500 )insert @tblA values('CC', 5, 350 )select Login, TotalPoints = sum(RankedPoints) , RankLessThan3 = (SELECT sum(points) FROM @tblA WHERE RANK <3 AND Login = tA.Login), RankGreatThanEqual4 = (SELECT sum(points) FROM @tblA WHERE Login = tA.Login AND RANK >=4)from(select Login, rank, RankedPoints = CASE Rank WHEN 1 THEN 0 WHEN 2 THEN 0 WHEN 3 THEN sum(points) - 300 WHEN 4 THEN sum(Points) WHEN 5 THEN sum(Points) end from @tblA group by Login , rank) as tAgroup by Login |
 |
|
|
nivaskhan
Starting Member
17 Posts |
Posted - 2002-07-10 : 06:56:08
|
| Thanks for your response.But your output isLogin TotalPoints RankLessThan3 RankGreatThanEqual4 ---------- ----------- ------------- ------------------- AA -125 50 25BB 850 200 600CC 800 NULL 850But What i need isLogin TotalPoints RankLessThan3 RankGreatThanEqual4 ---------- ----------- ------------- ------------------- AA 0 0 0BB 850 250 600CC 600 0 600And moreover ur substracting 300 points againt rank 3.I have to subtract 300 points when the total sum of points reaches 300 and have the remaining points grouped into rank.I dont know when the sum of points reaches 300 mark and then get the remaining sum.Regards,Nivas |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2002-07-10 : 07:55:15
|
| How about breaking it into CASE statement with 2 parts?CASE (total-discount)CASE > 0, then = total-discountCASE <= 0, then = 0END CASEThe format of the statement isn't right...(I'm rushing here)....but you should be able to see what I'm at....the sample given by yellowbug also is hard-coded for a particular score (300) and rank of 3 / 4.....whereas the solution i think you are after is something more flexible....one that would cope with any discount amount and any rank.... |
 |
|
|
|
|
|
|
|