Omit Aggregate Column in SQL Query
Is it possible to omit an aggregate column in a query? As an example:
SELECT Id, Descri开发者_StackOverflow中文版ption, MAX(Created)
FROM Record
GROUP BY Id, Description
How do I omit the MAX(Created) column from my result set? This query is being used in a sub-query so I can join to the most recent record and omit any older records. I know it won't make a large difference, but in general my practice has been to only bring back data you need, and in this case I just want to join to the most recent record, and pull out the description. I don't actually care what the date is.
Any thoughts? Am I being too picky?
If you only want the Description (1 record) of the most recent record (MAX(Created)), then
SELECT TOP 1 Id, Description
FROM Record
ORDER BY CREATED DESC
select TOP 1 ID, Description from Record
group by ID, Description order by MAX(Created) DESC
You cannot use an aggregate function without grouping columns.
And like mentioned above you may not even need an aggregate function
select TOP 1 ID, Description from Record order by Created DESC
Unless I'm misunderstanding the question, can't you just remove the column you don't want from the select?
SELECT Id, Description
FROM Record
ORDER BY Created DESC LIMIT 1
SELECT DISTINCT Id, Description
FROM Record
精彩评论