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 |
|
tinkerman
Starting Member
13 Posts |
Posted - 2005-06-01 : 12:05:23
|
| SamplesSampleId intsamplename varchar(25)ReadingId sampleId Reading date1 2 43 03/01/20051 5 3 03/01/20051 2 12 02/24/20051 2 7 03/05/2005....I have a transaction table with a listing of readings. I want to get the last 5 readings per sample. Any thoughts? |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2005-06-01 : 12:09:47
|
| select top 5 sampleid, samplename from samples order by sampleid descor use SET ROWCOUNT |
 |
|
|
tinkerman
Starting Member
13 Posts |
Posted - 2005-06-01 : 12:47:42
|
| I want the most recent 5 readings from the readings tbl per sampleid.Ex. sampleid reading date2 4 03/05/20052 8 03/04/20052 2 03/02/20052 15 02/26/20052 0 02/22/20055 13 03/01/20055 18 02/04/20055 12 02/02/20055 31 01/31/20055 13 01/30/2005... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-06-02 : 03:16:03
|
| Select top 5 reading from readingstbl order by reading DescMadhivananFailing to plan is Planning to fail |
 |
|
|
tinkerman
Starting Member
13 Posts |
Posted - 2005-06-02 : 09:23:35
|
| I am not looking for the top 5 rows from either table. I want 5 rows per sample. So with 5 samples, thats 25 rows, with 10 samples thats 50 rows, not just the top 5. The output should look like my last post above. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-02 : 09:25:00
|
can a single sample have more than 1 reading on a given day?Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
tinkerman
Starting Member
13 Posts |
Posted - 2005-06-02 : 09:29:03
|
| nope, one reading per sample, per day. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-02 : 10:04:06
|
By the way, when asking a question... you should include the part in blue.Create Table #myTable (sampleId int, reading int, date datetime)Insert Into #myTableSelect 1, 21, '3/1/2005' UnionSelect 1, 22, '3/2/2005' UnionSelect 1, 23, '3/3/2005' UnionSelect 1, 24, '3/4/2005' UnionSelect 1, 25, '3/5/2005' UnionSelect 1, 26, '3/6/2005' UnionSelect 2, 27, '3/1/2005' UnionSelect 2, 28, '3/2/2005' UnionSelect 2, 29, '3/3/2005' UnionSelect 2, 20, '3/4/2005' UnionSelect 2, 21, '3/5/2005' UnionSelect 2, 22, '3/6/2005' UnionSelect 3, 23, '3/1/2005' UnionSelect 3, 24, '3/2/2005' UnionSelect 3, 25, '3/3/2005' UnionSelect 3, 26, '3/4/2005' UnionSelect 3, 27, '3/5/2005' UnionSelect 3, 28, '3/6/2005' Declare @recordsPerSample intSet @recordsPerSample = 3Select A.* From #myTable AInner Join #myTable BOn A.sampleId = B.sampleIdand A.date <= B.dateGroup By A.sampleId, A.reading, A.dateHaving count(*)<=@recordsPerSampleDrop Table #myTable Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
tinkerman
Starting Member
13 Posts |
Posted - 2005-06-02 : 10:38:14
|
| Self join with having...Figures, I was messing with a tally and a cross join. Thanks Corey. |
 |
|
|
tinkerman
Starting Member
13 Posts |
Posted - 2005-06-02 : 12:05:34
|
| Is there a faster way to do this? The self join is 5 seconds on 32K rows and this is a test tbl. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-02 : 13:16:33
|
The second method is more expensive on the smaller data set, but it might run faster on 32k rows self-joined. Not sure though.Create Table #myTable (sampleId int, reading int, date datetime)Create index ix_main On #myTable (sampleId, date)Insert Into #myTableSelect 1, 21, '3/1/2005' UnionSelect 1, 22, '3/2/2005' UnionSelect 1, 23, '3/3/2005' UnionSelect 1, 24, '3/4/2005' UnionSelect 1, 25, '3/5/2005' UnionSelect 1, 26, '3/6/2005' UnionSelect 2, 27, '3/1/2005' UnionSelect 2, 28, '3/2/2005' UnionSelect 2, 29, '3/3/2005' UnionSelect 2, 20, '3/4/2005' UnionSelect 2, 21, '3/5/2005' UnionSelect 2, 22, '3/6/2005' UnionSelect 3, 23, '3/1/2005' UnionSelect 3, 24, '3/2/2005' UnionSelect 3, 25, '3/3/2005' UnionSelect 3, 26, '3/4/2005' UnionSelect 3, 27, '3/5/2005' UnionSelect 3, 28, '3/6/2005' Declare @recordsPerSample intSet @recordsPerSample = 3Select A.* From #myTable AInner Join #myTable BOn A.sampleId = B.sampleIdand A.date <= B.dateGroup By A.sampleId, A.reading, A.dateHaving count(*)<=@recordsPerSampleCreate Table #results (sampleId int, reading int, date datetime)Create index ix_main On #results (sampleId, date)While (@recordsPerSample>0)Begin Insert Into #results Select Z.* From #myTable Z Inner Join (Select A.sampleId, Date=max(A.Date) From #myTable A Left Join #results B On A.sampleId = B.sampleId and A.date = B.date Where B.sampleId is null Group By A.sampleId) Y On Z.sampleId = Y.sampleId and Z.date = Y.date Set @recordsPerSample = @recordsPerSample-1EndSelect * From #resultsDrop Table #resultsDrop Table #myTable Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-02 : 13:31:06
|
brett - are you trying to boost your post count?? b/c you've already got me squashed Corey Secret Service Agent: Mr. President, you're urinating on me.President Lyndon Johnson: I know I am. It's my prerogative. |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-06-02 : 14:01:56
|
Old standard answer:"Search this forum for ..."New and improved answer:"Look in Brett's weblog" rockmoose |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-06-03 : 00:44:42
|
quote: Originally posted by rockmoose Old standard answer:"Search this forum for ..."New and improved answer:"Look in Brett's weblog" rockmoose
Thats true.MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|