473,395 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

SQL aggregate fuction and GROUP BY

Hello, everyone!
Does anyone know how I can pull additional field in a database when
the max() of one field is pulled. For example:
================================================== ===
SELECT TOP 200 foreign_id, MAX(recordcreateddatetime) AS
max_recordcreateddatetime
FROM table1
GROUP BY foreign_id
================================================== ===
Here I am trying to pull the records that have the latest date for
each foreign_id. The result set above will pull foreign_id and
max_recordcreateddatetime but I needed to also have it display one
more field, current_status like this:
================================================== ===
SELECT TOP 200 foreign_id, MAX(recordcreateddatetime) AS
max_recordcreateddatetime, current_status
FROM table1
GROUP BY foreign_id
================================================== ===
The problem however is that SQL wants me to add this field to GROUP BY
or use an aggregate function with it and I don't want any aggregate
function processing - I just want current_status to show up there for
me to see. How do I do this?
Thank you for any input in advance!
Roumen.
Jul 20 '05 #1
3 1456
> SELECT TOP 200 foreign_id, MAX(recordcreateddatetime) AS
max_recordcreateddatetime, current_status
FROM table1
GROUP BY foreign_id

select top 200
t1.foreign_id
,t2.max_recordcreateddatetime
,t1.urrent_status
from table1 t1
inner join (select
foreign_id
,max_recordcreateddatetime = MAX(recordcreateddatetime)
FROM table1
GROUP BY foreign_id) t2 on t1.foreign_id = t2.foreign_id

Jul 20 '05 #2
Roumen,

This technique works:

select foreign_id, recordcreateddatetime, current_status
from table1 T1A
where recordcreateddatetime = (
select max(recordcreateddatetime) from table1 T1B
where T1B.foreign_id = T1A.foreign_id
)

as does this:

select foreign_id, recordcreateddatetime, current_status
from table1 T1A
where not exists (
select * from table1 T1B
where T1B.foreign_id = T1A.foreign_id
and T1B.recordcreateddatetime > T1A.recordcreateddatetime
)

I've left out TOP 200, since without an ORDER BY clause, the result is indeterminate.

Steve Kass
Drew University


Roumen Semov wrote:
Hello, everyone!
Does anyone know how I can pull additional field in a database when
the max() of one field is pulled. For example:
================================================== ===
SELECT TOP 200 foreign_id, MAX(recordcreateddatetime) AS
max_recordcreateddatetime
FROM table1
GROUP BY foreign_id
================================================== ===
Here I am trying to pull the records that have the latest date for
each foreign_id. The result set above will pull foreign_id and
max_recordcreateddatetime but I needed to also have it display one
more field, current_status like this:
================================================== ===
SELECT TOP 200 foreign_id, MAX(recordcreateddatetime) AS
max_recordcreateddatetime, current_status
FROM table1
GROUP BY foreign_id
================================================== ===
The problem however is that SQL wants me to add this field to GROUP BY
or use an aggregate function with it and I don't want any aggregate
function processing - I just want current_status to show up there for
me to see. How do I do this?
Thank you for any input in advance!
Roumen.

Jul 20 '05 #3
Thank you guys,
I was never aware that I could refer to elements in the main query from subqueries.
Roumen.
Steve Kass <sk***@drew.edu> wrote in message news:<uf**************@newsread3.news.atl.earthlin k.net>...
Roumen,

This technique works:

select foreign_id, recordcreateddatetime, current_status
from table1 T1A
where recordcreateddatetime = (
select max(recordcreateddatetime) from table1 T1B
where T1B.foreign_id = T1A.foreign_id
)

as does this:

select foreign_id, recordcreateddatetime, current_status
from table1 T1A
where not exists (
select * from table1 T1B
where T1B.foreign_id = T1A.foreign_id
and T1B.recordcreateddatetime > T1A.recordcreateddatetime
)

I've left out TOP 200, since without an ORDER BY clause, the result is indeterminate.

Steve Kass
Drew University


Roumen Semov wrote:
Hello, everyone!
Does anyone know how I can pull additional field in a database when
the max() of one field is pulled. For example:
================================================== ===
SELECT TOP 200 foreign_id, MAX(recordcreateddatetime) AS
max_recordcreateddatetime
FROM table1
GROUP BY foreign_id
================================================== ===
Here I am trying to pull the records that have the latest date for
each foreign_id. The result set above will pull foreign_id and
max_recordcreateddatetime but I needed to also have it display one
more field, current_status like this:
================================================== ===
SELECT TOP 200 foreign_id, MAX(recordcreateddatetime) AS
max_recordcreateddatetime, current_status
FROM table1
GROUP BY foreign_id
================================================== ===
The problem however is that SQL wants me to add this field to GROUP BY
or use an aggregate function with it and I don't want any aggregate
function processing - I just want current_status to show up there for
me to see. How do I do this?
Thank you for any input in advance!
Roumen.

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Job Lot | last post by:
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values: Date Amount Int Balance 1/1/2004 5000.00 50.00 5050.00...
2
by: MrMike | last post by:
I'm having an issue with an aggregate function in line 6 of the code below. The error is: "An aggregate may not appear in the set list of an UPDATE statement." How can i SUM this value...
1
by: Job Lot | last post by:
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values: Date Amount Int Balance 1/1/2004 5000.00 50.00 5050.00...
3
by: MrMike | last post by:
I'm having an issue with an aggregate function in line 6 of the code below. The error is: "An aggregate may not appear in the set list of an UPDATE statement." How can i SUM this value...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.