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 2000 Forums
 SQL Server Development (2000)
 Retrieving column not part of Group By

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-01-28 : 08:44:36
John writes "I need to retrive the primary key for the rows returned from the following query.

Here is my Table A {MyId (integer), Column1(String), Date1(date-time)}

select max(Date1),column1
from A
where Date1 = today
group by column1

I want to know if I can write a SQL query which can basically get me the MyId column for every row returned by the above query without having to use any programming logic. Can it be written in one single SQL query.

Thanks

John"

Nazim
A custom title

1408 Posts

Posted - 2002-01-28 : 08:56:19
select distinct myid from a
inner join
(
select max(Date1) as Mdate,column1
from A
where Date1 = today
group by column1
) b
on a.date1=b.mdate and a.column1=b.column1

HTH

--------------------------------------------------------------
Dont Tell God how big your Problem is , Tell the Problem how Big your God is
Go to Top of Page
   

- Advertisement -