| Author |
Topic |
|
axisperfect
Starting Member
21 Posts |
Posted - 2004-09-21 : 23:29:29
|
Help! Need a little bit of creativity here. My users have requested I export some data in a rather arbitrary format in one single CSV file--one criteria is how they want to auto-number some of the rows:LineNumber,Type,Date,SomeData1,SomeData2,SomeData21,TypeA,20-Sept-2004,ABC,XYZ,1232,TypeA,20-Sept-2004,ABC,XYZ,1233,TypeA,20-Sept-2004,ABC,XYZ,1234,TypeA,20-Sept-2004,ABC,XYZ,1231,TypeB,20-Sept-2004,ABC,XYZ,1232,TypeB,20-Sept-2004,ABC,XYZ,1231,TypeC,20-Sept-2004,ABC,XYZ,1232,TypeC,20-Sept-2004,ABC,XYZ,1233,TypeC,20-Sept-2004,ABC,XYZ,1234,TypeC,20-Sept-2004,ABC,XYZ,123Note that the line number "resets" itself every time the type of row is different... I'm thinking if I can get a select statement for each type (with autoincrement), I can union the select statements and get what I want.So... any ideas for a way to generate a SQL statement that will do something like:select (auto_increment_in_this_statement_only), (my_other_fields) from my_table where my_criteriaSample Table NAMES, with field FNameJohn DoeJane DoeBilly BobRay BobSelect auto_number, NAMES from FName order by FName would result in1, Billy Bob2, Jane Doe3, John Doe4, Ray BobSelect auto_number, NAMES from FName order by FName DESC would result in1, Ray Bob2, John Doe3, Jane Doe4, Billy Bob |
|
|
ffoiii
Starting Member
12 Posts |
Posted - 2004-09-22 : 00:00:55
|
| If you put all records into a table with a single ident column and then sort by an alternate criteria...i.e.create table #asdf (id int identity (1,1), name sysname, type varchar(10))insert into #asdfselect name, typefrom sysobjectsorder by type, nameselect (select min(id) from #asdf where type = a.type) + (select count(*) from #asdf where type = a.type and name < a.name) - (select count(*) from #asdf where type < a.type), name, typefrom #asdf aorder by name, typenot a great solution, and definitely not tested against a large data-set, but maybe it will give you or someone else a better idea.ffoiii |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-09-22 : 08:28:27
|
this should help. play with it. select autonumber = count(*), t1.* from FName t1, FName t2where t1.Type + t1.Date + t1.SomeData1 + t1.SomeData2 + t1.SomeData2 > t2.Type + t2.Date + t2.SomeData1 + t2.SomeData2 + t2.SomeData2group by t1.Type, t1.Date, t1.SomeData1, t1.SomeData2, t1.SomeData2order by 1Go with the flow & have fun! Else fight the flow |
 |
|
|
|
|
|