| Author |
Topic |
|
jonathans
Starting Member
40 Posts |
Posted - 2006-09-18 : 08:02:51
|
| Hey allI have a bit of a problem that i cant get my head around. I have to do a report with "Rankings", no the data for these reports are collaborated into a single table with a report id (report id is the identifing id in a relationship of 1 to many). Now the problem comes in that I dont want to use a cursor to do this, any ideas on how i can have a "1..2..3" ranking per report for certain criteria?Any and all idea's welcome :)WARNING: Running on cold coffee! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-18 : 08:05:12
|
| Give us DDL!Otherwise, try something like select mt1.*, (select count(*) from mytable mt2 where mt2.pk <= mt1.pk)from mytable mt1Peter LarssonHelsingborg, Sweden |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-18 : 08:06:33
|
| Can you post the table structure and some sample data with expected output?Chirag |
 |
|
|
jonathans
Starting Member
40 Posts |
Posted - 2006-09-18 : 08:27:21
|
| Table StructureTheIncrementedNumber|ReportID|JoinCodeStatus|JoinCode|MTDSales1|1|0|'N00001'|3000002|1|0|'N00002'|2500003|1|0|'N00003'|2450001|2|0|'N00001'|9005002|2|0|'N00005'|9000003|2|0|'N00002'|850000TheIncrementedNumber is the one i want to create, it is done in order of the MTDSales DESC.... Hope that helps, and thanks for the quick replies :)WARNING: Running on cold coffee! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-18 : 08:51:47
|
| [code]-- Prepare test dataDECLARE @Test TABLE (ReportID INT, JoinCodeStatus INT, JoinCode VARCHAR(6), MTDSales INT)INSERT @TestSELECT 1, 0, 'N00001', 300000 UNION ALLSELECT 1, 0, 'N00002', 250000 UNION ALLSELECT 1, 0, 'N00003', 245000 UNION ALLSELECT 2, 0, 'N00001', 900500 UNION ALLSELECT 2, 0, 'N00005', 900000 UNION ALLSELECT 2, 0, 'N00002', 850000-- Do the workSELECT (SELECT COUNT(DISTINCT MTDSales) FROM @Test t2 WHERE t2.ReportID = t1.ReportID AND t2.MTDSales >= t1.MTDSales) TheIncrementedNumber, t1.*FROM @Test t1ORDER BY 2, 1[/code]How do you treat ties? Ie, there are two MTDSales with same value?Peter LarssonHelsingborg, Sweden |
 |
|
|
jonathans
Starting Member
40 Posts |
Posted - 2006-09-18 : 09:07:13
|
| Thanks for the code...now ties are a completely different issueIf there are 2 with the same MTD Sales then the count would be as follows1|1|0|'N00001'|3000002|1|0|'N00002'|2500002|1|0|'N00003'|2500004|1|0|'N00004'|245000Nice and complex hehe :)Trying your code out, thanks for the help so farWARNING: Running on cold coffee! |
 |
|
|
jonathans
Starting Member
40 Posts |
Posted - 2006-09-18 : 09:20:59
|
| Awesome code, thank you, I never would have thought of doing it that way... thanks so much. Now onto the 'tied' valuesWARNING: Running on cold coffee! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-18 : 09:25:13
|
| [code]-- Prepare test dataDECLARE @Test TABLE (ReportID INT, JoinCodeStatus INT, JoinCode VARCHAR(6), MTDSales INT)INSERT @TestSELECT 1, 0, 'N00001', 300000 UNION ALLSELECT 1, 0, 'N00002', 250000 UNION ALLSELECT 1, 0, 'N00004', 250000 UNION ALLSELECT 1, 0, 'N00003', 245000-- Do the workSELECT (SELECT 1 + COUNT(*) FROM @Test t2 WHERE t2.ReportID = t1.ReportID AND t2.MTDSales > t1.MTDSales) TheIncrementedNumber, t1.*FROM @Test t1ORDER BY 2, 1[/code] |
 |
|
|
jonathans
Starting Member
40 Posts |
Posted - 2006-09-18 : 09:27:03
|
| A 1B 2B 2D 4 (occupy ranks)WARNING: Running on cold coffee! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-18 : 09:30:32
|
| See previous post.Peter LarssonHelsingborg, Sweden |
 |
|
|
jonathans
Starting Member
40 Posts |
Posted - 2006-09-18 : 10:03:36
|
| Thanks for the code, may i ask what were the other forms of ranking that you showed earlier?Thanks again, you have made my life happy today (jobs of been a simpleton)WARNING: Running on cold coffee! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-18 : 10:07:53
|
| Sports results often ties withA - 1B - 2B - 2C - 4Economic ranking often ties withA - 1B - 2B - 2C - 3and I have only come across "late ranking" once in my careerA - 1B - 3B - 3C - 4Peter LarssonHelsingborg, Sweden |
 |
|
|
jonathans
Starting Member
40 Posts |
Posted - 2006-09-18 : 10:13:57
|
| Thanks :)This code still stuffs with my head at its simplicity. I commend you on your knowledge.WARNING: Running on cold coffee! |
 |
|
|
|