473,750 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(recordcreat eddatetime) AS
max_recordcreat eddatetime
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_recordcreat eddatetime but I needed to also have it display one
more field, current_status like this:
=============== =============== =============== ========
SELECT TOP 200 foreign_id, MAX(recordcreat eddatetime) AS
max_recordcreat eddatetime, 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 1475
> SELECT TOP 200 foreign_id, MAX(recordcreat eddatetime) AS
max_recordcreat eddatetime, current_status
FROM table1
GROUP BY foreign_id

select top 200
t1.foreign_id
,t2.max_recordc reateddatetime
,t1.urrent_stat us
from table1 t1
inner join (select
foreign_id
,max_recordcrea teddatetime = MAX(recordcreat eddatetime)
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, recordcreatedda tetime, current_status
from table1 T1A
where recordcreatedda tetime = (
select max(recordcreat eddatetime) from table1 T1B
where T1B.foreign_id = T1A.foreign_id
)

as does this:

select foreign_id, recordcreatedda tetime, current_status
from table1 T1A
where not exists (
select * from table1 T1B
where T1B.foreign_id = T1A.foreign_id
and T1B.recordcreat eddatetime > T1A.recordcreat eddatetime
)

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(recordcreat eddatetime) AS
max_recordcreat eddatetime
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_recordcreat eddatetime but I needed to also have it display one
more field, current_status like this:
=============== =============== =============== ========
SELECT TOP 200 foreign_id, MAX(recordcreat eddatetime) AS
max_recordcreat eddatetime, 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******* *******@newsrea d3.news.atl.ear thlink.net>...
Roumen,

This technique works:

select foreign_id, recordcreatedda tetime, current_status
from table1 T1A
where recordcreatedda tetime = (
select max(recordcreat eddatetime) from table1 T1B
where T1B.foreign_id = T1A.foreign_id
)

as does this:

select foreign_id, recordcreatedda tetime, current_status
from table1 T1A
where not exists (
select * from table1 T1B
where T1B.foreign_id = T1A.foreign_id
and T1B.recordcreat eddatetime > T1A.recordcreat eddatetime
)

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(recordcreat eddatetime) AS
max_recordcreat eddatetime
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_recordcreat eddatetime but I needed to also have it display one
more field, current_status like this:
=============== =============== =============== ========
SELECT TOP 200 foreign_id, MAX(recordcreat eddatetime) AS
max_recordcreat eddatetime, 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
1387
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 1/1/2004 4000.00 40.00 4040.00 1/2/2004 1000.00 10.00 1010.00 1/2/2204 2000.00 20.00 2020.00 1/3/2004 1500.00 15.00 1515.00 I want my resultant DataTable to show in DataGrid as
2
303
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 successfully? Thanks experts. CREATE Trigger AvailableLogFootage on tblLogs FOR INSERT As
1
8088
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 1/1/2004 4000.00 40.00 4040.00 1/2/2004 1000.00 10.00 1010.00 1/2/2204 2000.00 20.00 2020.00 1/3/2004 1500.00 15.00 1515.00 I want my resultant DataTable to show in DataGrid as
3
3116
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 successfully? Thanks experts. CREATE Trigger AvailableLogFootage on tblLogs FOR INSERT As
0
9000
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9339
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6804
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.