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 |
|
Johnyalm
Starting Member
49 Posts |
Posted - 2005-12-04 : 02:55:55
|
| I hav e a query like this:SELECT tblACTORS.name, a_tblTIMESERIES_TYPE.type, AVG(CAST(a_tblTIMESERIES.decimalvalue AS int)) AS averageFROM tblACTORS_KITCODES_GROUPS INNER JOIN tblACTORS ON tblACTORS_KITCODES_GROUPS.id_actors = tblACTORS.id_actors INNER JOIN a_tblTIMESERIES INNER JOIN a_tblTIMESERIES_TYPE ON a_tblTIMESERIES.id_timeseries_type = a_tblTIMESERIES_TYPE.id_timeseries_type ON tblACTORS.id_actors = a_tblTIMESERIES.id_actorsWHERE (a_tblTIMESERIES_TYPE.type = 'steps')GROUP BY tblACTORS.name, a_tblTIMESERIES_TYPE.typeORDER BY average DESCIt works OK, but I would like to get a NEW column where incremented rownumber is genereated.Is there a way to do that?The result of the above query is:NAME TYPE AVERAGECharles steps 12312Curt steps 11257Lisa steps 10287I would like to get# NAME TYPE AVERAGE1 Charles steps 123122 Curt steps 112573 Lisa steps 10287Johny Almwww.mirrorgate.com |
|
|
svicky9
Posting Yak Master
232 Posts |
Posted - 2005-12-04 : 03:09:11
|
| If you are using T-sql 2005 you can generate row numbers likeselect row_number() over(order by average desc) as rownumber ,tblACTORS.name, a_tblTIMESERIES_TYPE.type, AVG(CAST(a_tblTIMESERIES.decimalvalue AS int)) AS averageFROM tblACTORS_KITCODES_GROUPS INNER JOINtblACTORS ON tblACTORS_KITCODES_GROUPS.id_actors = tblACTORS.id_actors INNER JOINa_tblTIMESERIES INNER JOINa_tblTIMESERIES_TYPE ON a_tblTIMESERIES.id_timeseries_type = a_tblTIMESERIES_TYPE.id_timeseries_type ONtblACTORS.id_actors = a_tblTIMESERIES.id_actorsWHERE (a_tblTIMESERIES_TYPE.type = 'steps')GROUP BY tblACTORS.name, a_tblTIMESERIES_TYPE.typethanksvic |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2005-12-04 : 03:29:36
|
| also see here[url]http://www.sqlteam.com/item.asp?ItemID=1491[/url]-----------------[KH] |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2005-12-04 : 06:16:57
|
| and well you can try out the following query .. SELECT (Select Count(1) From tblACTORS t1 Where t1.Name < tblACTORS.Name),tblACTORS.name, a_tblTIMESERIES_TYPE.type, AVG(CAST(a_tblTIMESERIES.decimalvalue AS int)) AS averageFROM tblACTORS_KITCODES_GROUPS INNER JOINtblACTORS ON tblACTORS_KITCODES_GROUPS.id_actors = tblACTORS.id_actors INNER JOINa_tblTIMESERIES INNER JOINa_tblTIMESERIES_TYPE ON a_tblTIMESERIES.id_timeseries_type = a_tblTIMESERIES_TYPE.id_timeseries_type ON tblACTORS.id_actors = a_tblTIMESERIES.id_actorsWHERE (a_tblTIMESERIES_TYPE.type = 'steps')GROUP BY tblACTORS.name, a_tblTIMESERIES_TYPE.typeORDER BY average DESCif there is any unique columns like DateTime ..etc.. then replace it in the where cluase of the first line of query .. If there is no unique columns then following query will fail.. then i guess.. you need to create the temp table.. with the identity column ... and pull out the query from that temp table.. Somthing like this .. SELECT IDENTITY(int, 1,1) Rownum,tblACTORS.name, a_tblTIMESERIES_TYPE.type, AVG(CAST(a_tblTIMESERIES.decimalvalue AS int)) AS average Into #TempFROM tblACTORS_KITCODES_GROUPS INNER JOINtblACTORS ON tblACTORS_KITCODES_GROUPS.id_actors = tblACTORS.id_actors INNER JOINa_tblTIMESERIES INNER JOINa_tblTIMESERIES_TYPE ON a_tblTIMESERIES.id_timeseries_type = a_tblTIMESERIES_TYPE.id_timeseries_type ON tblACTORS.id_actors = a_tblTIMESERIES.id_actorsWHERE (a_tblTIMESERIES_TYPE.type = 'steps')GROUP BY tblACTORS.name, a_tblTIMESERIES_TYPE.typeSelect * From #Temp Hope this works for you.. :-)Sucess Comes to those who Believe in Beauty of their Dream.. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-05 : 00:27:05
|
| Where do you want to show these data?If you use Front End Application use for or While loop to genereate row numbers.If you use Reports make use of RecordNumber feature. If you try to do this in SQL it will be time consuming if there are thousands of dataMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|