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 |
gypsymami
Starting Member
3 Posts |
Posted - 2007-12-19 : 17:32:45
|
I'm not sure if this is even possible but if so, please help!I need a counter on a table that will reset based on another field. Basically, I would like the row count (id field, whatever the name) to return to 1 when the ID field changes. What I am trying to do is count the number of dates that is associated with each ID.Right now, I have this:Row Date ID 1 2007-09-01 00:00:00.000 2772 2007-10-01 00:00:00.000 2773 2007-11-01 00:00:00.000 2774 2007-12-01 00:00:00.000 2775 2008-01-01 00:00:00.000 2776 2007-10-01 00:00:00.000 2937 2007-11-01 00:00:00.000 2938 2007-12-01 00:00:00.000 2939 2008-01-01 00:00:00.000 293What i would LIKE is this:Row Date ID 1 2007-09-01 00:00:00.000 2772 2007-10-01 00:00:00.000 2773 2007-11-01 00:00:00.000 2774 2007-12-01 00:00:00.000 2775 2008-01-01 00:00:00.000 2771 2007-10-01 00:00:00.000 2932 2007-11-01 00:00:00.000 2933 2007-12-01 00:00:00.000 2934 2008-01-01 00:00:00.000 293... and so onI'm using the SQL below because that's the only way i could figure to get the counter in there. Of course, it doesn't give me what a want drop table #tableselect identity (int,1,1) as row , date,id into #table from [TableList] where date is not null and ID in ( 277, 293) order by dateselect * from #table group by id, ratingdate, row order by ratingdate Any assistance would be greatly appreciated |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 17:44:01
|
Fourth time today!-- Prepare sample dataDECLARE @Sample TABLE (Row INT, Date DATETIME, ID INT)INSERT @SampleSELECT 1, '2007-09-01', 277 UNION ALLSELECT 2, '2007-10-01', 277 UNION ALLSELECT 3, '2007-11-01', 277 UNION ALLSELECT 4, '2007-12-01', 277 UNION ALLSELECT 5, '2008-01-01', 277 UNION ALLSELECT 6, '2007-10-01', 293 UNION ALLSELECT 7, '2007-11-01', 293 UNION ALLSELECT 8, '2007-12-01', 293 UNION ALLSELECT 9, '2008-01-01', 293-- Initialize staging areaDECLARE @Stage TABLE (RowID INT IDENTITY(1, 1), Date DATETIME, ID INT)INSERT @StageSELECT Date, IDFROM @SampleORDER BY ID, Date-- Show the expected outputSELECT s.RowID - e.MinID + 1 AS Row, s.Date, s.IDFROM ( SELECT ID, MIN(RowID) AS MinID FROM @Stage GROUP BY ID ) AS eINNER JOIN @Stage AS s ON s.ID = e.IDORDER BY s.ID, s.RowID E 12°55'05.25"N 56°04'39.16" |
|
|
gypsymami
Starting Member
3 Posts |
Posted - 2007-12-19 : 18:12:08
|
I may be misunderstanding something - or maybe not explaining correctly. But, the table has THOUSANDS of IDs and Thousands of dates. From the looks of this, i'd basically be manually writing the table. Correct?I don't want to do this. I am hoping to find a way to change the counter based on the ID field. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 18:15:15
|
I don't think I have access to your database. Am I right?If I don't have access to your database, I must somehow have some sample data, right?So I wrote the "Prepare sample data" part in the code above since I do not have access to your database.What you need, is the "Initialize staging area" and "Show the expected output" parts.Copy those two parts, change the table names and column names to the one used in your environment. E 12°55'05.25"N 56°04'39.16" |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 18:16:27
|
[code]-- Initialize staging areaDECLARE @Stage TABLE (RowID INT IDENTITY(1, 1), Date DATETIME, ID INT)INSERT @StageSELECT Date, IDFROM {Your table name here}ORDER BY ID, Date-- Show the expected outputSELECT s.RowID - e.MinID + 1 AS Row, s.Date, s.IDFROM ( SELECT ID, MIN(RowID) AS MinID FROM @Stage GROUP BY ID ) AS eINNER JOIN @Stage AS s ON s.ID = e.IDORDER BY s.ID, s.RowID[/code] E 12°55'05.25"N 56°04'39.16" |
|
|
gypsymami
Starting Member
3 Posts |
Posted - 2007-12-20 : 11:33:13
|
beautiful. Thanks a million! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-20 : 11:54:14
|
You're welcome.Please come back when you experience next difficulty. E 12°55'05.25"N 56°04'39.16" |
|
|
|
|
|
|
|