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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Group by complications

Author  Topic 

thedude813
Starting Member

3 Posts

Posted - 2008-08-28 : 10:21:28
Hey all, I'm having some issues with an aggregate function question. Basically I have the following in two separate tables - student information and tests they have taken. I need to get information from both of them based on the latest test they have taken. What I have is the following:

Select s.std_pk, t.test_pk, max(t.test_dt)
from student s, test t, stdtstFK st
where s.std_pk = st.std_fk and t.test_pk = st.test_fk
group by s.std_pk, t.test_pk

I know this isn't right. What I want is to be able to get the student pk, and the test pk of the latest date. Whenever I write something up, it either gives me all entries of tests taken (as in here, because the test_pk's are different, so grouping by them will give me distinct values for each std_pk/test_pk pair).

The other option is that I can get all the student_pk's with their max test_dt's, but I don't like this answer because I am concerned that when I link that date back I could get multiple rows if on some rare case their two latest tests had the same date. This shouldn't happen, but I want to be able to rely on the query, not on the data. I would much rather be able to immediately grab the test_pk that is linked to the max(test_dt) as I select it, as opposed to trying to link a date back to a pk, it just seems unwieldy. Is there any other way to do this?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-28 : 10:25:38
may be this

SELECT *
FROM
(SELECT ROW_NUMBER() OVER (PARTITION BY s.std_pk, t.test_pk ORDER BY t.test_dt DESC) AS Seq,*
FROM student s
INNER JOIN stdtstFK st
ON s.std_pk = st.std_fk
INNER JOIN test t
ON t.test_pk = st.test_fk
)r
WHERE r.Seq=1
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-08-28 : 10:28:46
Also replace * with actual names

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

thedude813
Starting Member

3 Posts

Posted - 2008-08-28 : 10:45:32
Hey, thanks for the quick and helpful responses! I thought I should just post again to let everyone know what was up. I tried your code, but it was giving me only 1's for the whole seq row, so I decided to look into how you were doing it.

The OVER( PARTITION BY... statement is something I never even knew existed, but is apparently very important for the issue I was having. Your code was very close to what I needed, and all I had to do was remove t.test_pk from the partition by clause. It should be making separate partitions for each employee, not for each employee/test set.

Thanks again for all your (very fast) help!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-28 : 10:50:46
quote:
Originally posted by thedude813

Hey, thanks for the quick and helpful responses! I thought I should just post again to let everyone know what was up. I tried your code, but it was giving me only 1's for the whole seq row, so I decided to look into how you were doing it.

The OVER( PARTITION BY... statement is something I never even knew existed, but is apparently very important for the issue I was having. Your code was very close to what I needed, and all I had to do was remove t.test_pk from the partition by clause. It should be making separate partitions for each employee, not for each employee/test set.

Thanks again for all your (very fast) help!


ok...i just made the query from what you posted so couldnt guess you were looking for distinct result per employee. It would be best if you can give some sample data & output in future so that we can know what you're trying to get else you will keep us guessing abt what you really want. Glad that you could sort it out.
Go to Top of Page

thedude813
Starting Member

3 Posts

Posted - 2008-08-28 : 10:56:10
Yeah, I knew I wasn't being clear when I wrote it, but I tried the best I could. I said that "What I want is to be able to get the student pk, and the test pk of the latest date." This was a little ambiguous. Anyways, it doesn't matter, you got me close enough to the answer to get it all figured out. Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-28 : 10:59:02
quote:
Originally posted by thedude813

Yeah, I knew I wasn't being clear when I wrote it, but I tried the best I could. I said that "What I want is to be able to get the student pk, and the test pk of the latest date." This was a little ambiguous. Anyways, it doesn't matter, you got me close enough to the answer to get it all figured out. Thanks!


you're Welcome
Go to Top of Page
   

- Advertisement -