选择最大日期和分组 [英] Selecting the max date and group by
问题描述
嗨
我目前正在尝试做报告并从SQL中提取数据
问题我是我似乎无法得到我需要的信息
我有一个名为listStage的视图。
此视图代表我的数据库中的5阶段(表格)
我的陈述是
选择 max( date ),产品,阶段来自 liststage < span class =code-keyword> group by product,stage
问题是不是只返回视图中的最大日期,我只需要它来显示最大日期,产品和阶段。
查询返回每个日期的最大日期阶段。
我怎样才能让它显示产品和阶段的最大日期?
< blockquote>尝试
SELECT 日期,产品,阶段 FROM liststage
WHERE 日期 =( SELECT MAX(日期) FROM liststage)
- 或
SELECT TOP 1 日期,产品,阶段 FROM liststage
ORDER BY 日期 DESC
SELECT TOP 1日期,产品,阶段FROM liststage ORDER BY日期DESC
是'' date''列数据类型是DATETIME,如果没有尝试这个
SELECT MAX(CAST([date] AS DATETIME))AS MaxDate,
产品,
阶段
FROM liststage
GROUP BY
产品,
阶段
ORDER BY
MAX(CAST([date] AS DATETIME))DESC
希望这样可行。
Hi
I am currently trying to do a report and pull the data from SQL
The problem i am having is that i cant seem to get the information i require
I have a view called listStage.
this view represents 5stages(tables) in my db
my statement is
Select max(date),Product,stage from liststage group by product,stage
Problem with this is it doesnt just return the maximum date in the view, i just need it to show the maximum date,product and stage.
The query returns the maximum date at each stage.
How can i get it to show the just maximum date,with product and stage?
Try
SELECT Date, Product,stage FROM liststage WHERE Date = (SELECT MAX(Date) FROM liststage) --OR SELECT TOP 1 Date, Product,stage FROM liststage ORDER BY Date DESC
SELECT TOP 1 Date, Product,stage FROM liststage ORDER BY Date DESC
Is the ''date'' column datatype is DATETIME if not try this one
SELECT MAX(CAST([date] AS DATETIME)) AS MaxDate, Product, stage FROM liststage GROUP BY product, stage ORDER BY MAX(CAST([date] AS DATETIME)) DESC
Hope this will work.
这篇关于选择最大日期和分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!