| Author |
Topic |
|
Lopaka
Starting Member
48 Posts |
Posted - 2006-03-16 : 17:11:32
|
| I am trying to assign values to a unique record set(s). All of the records are in one table, for example:Table:PK, NAME, BASE, RANK1, AAA, 1.50, NULL2, AAA, 2.25, NULL3, AAA, 1.55, NULL4, AAA, 1.00, NULL5, AAA, 1.99, NULL6, AAA, 2.10, NULL7, BBB, 1.00, NULL8, BBB, 1.50, NULL9, CCC, 1.01, NULL10, CCC, 1.03, NULL11, CCC, 1.02, NULLMy current query pulls all of the ‘AAA’ values. Sorts by base descending order. Writes the data to a temporary table with an identity column defined. Then I join the table and the temp table based on pk and name, then update Table.Rank with the temp table values. Then I repeat the process.END Result:PK, NAME, BASE, RANK1, AAA, 1.50, 52, AAA, 2.25, 13, AAA, 1.55, 44, AAA, 1.00, 65, AAA, 1.99, 36, AAA, 2.10, 27, BBB, 1.00, 28, BBB, 1.50, 19, CCC, 1.01, 310, CCC, 1.03, 111, CCC, 1.02, 2The problem with my query is the amount of data, 500k Unique Names. Each name can consist of up to 10 base values. Is there a fast way of assigning the RANK?Thank you.Robert R. Barnes |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-03-16 : 17:23:00
|
Here is one way:set nocount oncreate table #table (PK int, NAME varchar(3), BASE money, RANK int null)insert #table select 1, 'AAA', 1.50, null union allselect 2, 'AAA', 2.25, null union allselect 3, 'AAA', 1.55, null union allselect 4, 'AAA', 1.00, null union allselect 5, 'AAA', 1.99, null union allselect 6, 'AAA', 2.10, null union allselect 7, 'BBB', 1.00, null union allselect 8, 'BBB', 1.50, null union allselect 9, 'CCC', 1.01, null union allselect 10, 'CCC', 1.03, null union allselect 11, 'CCC', 1.02, null update a set a.rank = (select count(*) from #table where [name] = a.[name] and base <= a.base)from #table aselect * from #table order by name, rank --and serial number :)output:PK NAME BASE RANK ----------- ---- --------------------- ----------- 4 AAA 1.0000 11 AAA 1.5000 23 AAA 1.5500 35 AAA 1.9900 46 AAA 2.1000 52 AAA 2.2500 67 BBB 1.0000 18 BBB 1.5000 29 CCC 1.0100 111 CCC 1.0200 210 CCC 1.0300 3 EDIT:How do you want to rank ties?Be One with the OptimizerTG |
 |
|
|
Lopaka
Starting Member
48 Posts |
Posted - 2006-03-16 : 17:38:53
|
| It worked great, thank you...:)Robert R. Barnes |
 |
|
|
Lopaka
Starting Member
48 Posts |
Posted - 2006-03-16 : 18:11:37
|
| This only works if you have values in the base field. when there are duplicate or null values it repeats...Any ideas?Robert R. Barnes |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-16 : 20:28:35
|
"This only works if you have values in the base field. when there are duplicate or null values it repeats..."quote: Originally posted by TGHow do you want to rank ties?
You did not answer TG's question.And also How do you want to treat null values as ? KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-03-17 : 00:27:10
|
| Where do you want to show the data?If you use Reports, then Group it by Keycolumn then make use of Running Total with Count and reset it to group level. Trying to do this from SQL will become ineffecient if there are millions of rows on that tableMadhivananFailing to plan is Planning to fail |
 |
|
|
Lopaka
Starting Member
48 Posts |
Posted - 2006-03-17 : 01:23:08
|
| Ties and nulls are to be treated secquencially as well. The base is the primary sort and if null does not matter what number comes next.Robert R. Barnes |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-17 : 02:06:01
|
[code]set nocount oncreate table #table (PK int, [NAME] varchar(3), BASE money, RANK int null)insert #table select 1, 'AAA', 1.50, null union allselect 2, 'AAA', 2.25, null union allselect 3, 'AAA', 1.55, null union allselect 4, 'AAA', 1.00, null union allselect 5, 'AAA', 1.99, null union allselect 6, 'AAA', 2.10, null union allselect 7, 'BBB', 1.00, null union allselect 8, 'BBB', 1.50, null union allselect 9, 'CCC', 1.01, null union allselect 10, 'CCC', 1.03, null union allselect 11, 'CCC', 1.02, null union allselect 12, 'AAA', 1.55, null union allselect 13, 'AAA', null, null union allselect 14, 'CCC', 1.02, null union allselect 15, 'CCC', 1.02, null union allselect 16, 'CCC', 1.02, null union allselect 17, 'BBB', 1.00, null union allselect 18, 'BBB', 1.00, nullupdate a set a.RANK = (select count(*) from #table where [NAME] = a.[NAME] and isnull(BASE, 0) <= isnull(a.BASE, 0))from #table aupdate tset t.RANK = t.RANK - (select count(*) from #table x where x.[NAME] = t.[NAME] and x.RANK = t.RANK and x.PK < t.PK)from #table tinner join( select [NAME], RANK from #table group by [NAME], RANK having count(*) > 1) mon t.[NAME] = m.[NAME]and t.RANK = m.RANKselect * from #table order by [NAME], RANKdrop table #table[/code]Result :[code]PK NAME BASE RANK ----------- ---- --------------------- ----------- 13 AAA NULL 14 AAA 1.0000 21 AAA 1.5000 312 AAA 1.5500 43 AAA 1.5500 55 AAA 1.9900 66 AAA 2.1000 72 AAA 2.2500 818 BBB 1.0000 117 BBB 1.0000 27 BBB 1.0000 38 BBB 1.5000 49 CCC 1.0100 116 CCC 1.0200 215 CCC 1.0200 314 CCC 1.0200 411 CCC 1.0200 510 CCC 1.0300 6[/code] KHChoice is an illusion, created between those with power, and those without.Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant |
 |
|
|
|
|
|