[英]How to find the lowest value and the corresponding date SQL-Server 2014
我有示例數據, 請參見SQL Fiddle 。 我想從值列中找到最低值,並從上載日期列中找到最低值的日期。 我嘗試了以下查詢,但它無法正常工作並顯示所有值。
SELECT
username,
MIN(Value1) AS MinValue1,
CASE
WHEN value1 = MIN(value1) THEN uploaded_date
END
AS MinDate
FROM
test
GROUP BY
username
SELECT test. username,value1,MIN(uploaded_date )FROM
(Select MIN(value1) MinValue1,username from test group by username ) A
INNER JOIN
test
ON A.MinValue1 = test.Value1
AND A.username = test.username
GROUP BY
test. username,value1
您的查詢無效,因為您正在使用選擇列表中的列,因此Group by子句會將所有不同的值分組到每一行中
我希望這能幫到您
;with CTE as(
SELECT username,
MIN(Value1) AS MinValue1
FROM
test
GROUP BY
username
)
select c.username,MinValue1,dt.uploaded_date from CTE c
cross apply(select top 1 uploaded_date from test where value1=c.MinValue1 and username=c.username order by uploaded_date desc) dt
select Value1, update_date from test order by Value1 limit 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.