Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Group by without an aggregate function Expand / Collapse
Author
Message
Posted Tuesday, October 14, 2008 2:20 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 3, 2011 11:02 AM
Points: 27, Visits: 50
Hi, I need to write a query which selects the most recent record entered from among multiple records which have the same column value, except for one single column, date entered.

this query returns all the values, but I only need the most recent one.

Select table1.column1, table2.column2, table3.column3,...
from table1,
join table2
on something = something else
join table3
on something = something else
where there is a condition

I am thinking I need to group these records based on the value of the date column, and then somehow select the most recent one.
Please help me.

Post #585778
Posted Tuesday, October 14, 2008 2:26 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
You could possibly use TOP 1 with an order by date desc.

Select Top 1
Column List
From
Table List
Where
Criteria
Order By
Date Column desc


Without more details this is the best I can do.




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #585788
Posted Tuesday, October 14, 2008 2:45 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 3, 2011 11:02 AM
Points: 27, Visits: 50
No, that wouldn't work. I am returning a table, and only some rows in this table might have multiple records, from which i need to select the most recent one.
I am thinking I need to use having clause after my group by, but not sure how. something like

having max(purchaseDate)
Post #585806
Posted Tuesday, October 14, 2008 2:48 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 12:05 PM
Points: 42,462, Visits: 35,522
Please post the table structure, some sample data and your desired output.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #585808
Posted Tuesday, October 14, 2008 2:53 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 3, 2011 11:02 AM
Points: 27, Visits: 50
DATA:

product Id Product Name purchase DATE
11111 Book 2002
22222 Vase 2004
33333 PC 2006
33333 PC 2007

I'd like my query to return

product Id Product Name purchase DATE
11111 Book 2002
22222 Vase 2004
33333 PC 2007
Post #585816
Posted Tuesday, October 14, 2008 2:56 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
Okay, you should take a look at the links in my signature line as posting a more complete question helps you get better answers.

When you say you get multiple rows for some does that mean that you are getting the same data except for the date? Something like this:

FirstName   LastName     ChangeDate
--------- --------- ------------
Jack Corbett 01/27/1998
Jack Corbett 07/01/2008
Steve Jones 06/25/2007

Or are there other differences in the data as well?

If you post some schemas and some sample data it would be a lot easier to answer your question.




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #585820
Posted Tuesday, October 14, 2008 3:01 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
Okay with that data all you need is a Group By and a Max(PurchaseDate)

Select
product_id,
product_name,
Max(purchase_date) as last_purchase_date
From
table
Group By
product_id,
product_name





Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #585827
Posted Wednesday, October 15, 2008 7:33 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 3, 2011 11:02 AM
Points: 27, Visits: 50
Thanks, sorry man bad. I should have mentioned that each record has a unique key, and this doesn't work.

DATA:

unique key product Id Product Name purchase DATE
1 11111 Book 2002
2 22222 Vase 2004
3 33333 PC 2006
4 33333 PC 2007

I'd like my query to return

Unique key product Id Product Name purchase DATE
1 11111 Book 2002
2 22222 Vase 2004
4 33333 PC 2007
Post #586189
Posted Wednesday, October 15, 2008 7:59 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:07 AM
Points: 11,157, Visits: 12,899
Don't you mean for the last record you would want the record with unique Id 4?



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #586214
Posted Wednesday, October 15, 2008 8:01 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 12:05 PM
Points: 42,462, Visits: 35,522
That's why we ask for the table schema.

Assuming that the relationship between product_id and product_name is a 1-to-1, this should work

SELECT table.uniquekey, table.product_id, table.product_name, sub.last_purchase_date 
FROM table INNER JOIN
(Select
product_id,
Max(purchase_date) as last_purchase_date
From
table
Group By
product_id) sub on table.product_id = sub.product_id AND table.purchase_date = sub.last_purchase_date




Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #586216
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse