By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,419 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,419 IT Pros & Developers. It's quick & easy.

group by clause query help

P: n/a
select ano,max(date),a_subject from MY_TAB where table_name='xyz' and
ano=877 group by a_subject,ano order by a_subject

id max(Date) A_Subject
877 2005-01-20 00:00:00.000 Subject_1
877 1900-01-01 00:00:00.000 Subject_2
877 2004-12-20 00:00:00.000 Subject_3
877 2005-01-19 00:00:00.000 Subject_4

--------------------------------------------------------------------------
When I put the status column in, it fetches all the rows.

select ano,max(date),a_subject,status from MY_TAB where
table_name='xyz' and ano=877 group by a_subject,ano,status order by
a_subject

ANO max(Date) A_Subject Status
877 2005-01-20 00:00:00.000 Subject_1 Not
Started
877 1900-01-01 00:00:00.000 Subject_2 Not
Started
877 2004-12-20 00:00:00.000 Subject_3 Completed
877 1900-01-01 00:00:00.000 Subject_3 Not
Started
877 1900-01-01 00:00:00.000 Subject_4 Not
Started
877 2005-01-19 00:00:00.000 Subject_4 Not
Started

-----------------------------------------------------------------------
now what i want is

ANO max(Date) A_Subject Status
877 2005-01-20 00:00:00.000 Subject_1 Not
Started
877 1900-01-01 00:00:00.000 Subject_2 Not
Started
877 2004-12-20 00:00:00.000 Subject_3 Completed
877 2005-01-19 00:00:00.000 Subject_4 Not
Started
-----------------------------------------------------------------------------
This is one of the suggestions by Erland

SELECT a.ano, a.a_subject, b.status
FROM (select ano, max(date), a_subject
from MY_TAB
where table_name='xyz' and ano=877
group by a_subject, ano) AS b
JOIN MY_TAB b ON a.ano = b.ano
AND a.maxdate = b.date
AND a.a_subject = b.a_subject

but I am getting this error

Server: Msg 1011, Level 15, State 1, Line 6
The correlation name 'b' is specified multiple times in a FROM clause.
Any ideas?

Thanks

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
This line:
group by a_subject, ano) AS b
should be:
group by a_subject, ano) AS a

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.