I have the following table
date_time, data_value
date_time data_value
----------------------- ----------------------
2011-03-28 15开发者_Python百科:05:29.000 20
2011-03-28 15:55:39.000 40
2011-03-28 16:25:45.000 30
2011-03-28 23:37:11.000 10
2011-03-29 04:43:12.000 44
2011-03-29 05:13:18.000 55
2011-03-29 05:28:21.000 52
2011-03-29 05:48:25.000 23
2011-03-29 06:13:30.000 8
2011-03-29 07:03:40.000 34
2011-03-29 08:34:01.000 25
2011-03-29 09:34:20.000 35
2011-03-29 09:49:23.000 23
2011-03-30 16:00:55.000 20
I would like to get the minimum value of each date in sql:
2011-03-29 06:13:30.000 8
2011-03-28 15:05:29.000 20
any suggestions?
In SQL Server 2005+:
WITH ranked AS (
SELECT
date_time,
data_value,
rownum = ROW_NUMBER() OVER (
PARTITION BY DATEADD(day, DATEDIFF(day, 0, date_time), 0)
ORDER BY data_value
)
FROM atable
)
SELECT
date_time,
data_value
FROM ranked
WHERE rownum = 1
Something like
select TO_DAYS(date_time) as dt, min(date_time) from xxx group by dt;
Try those :
SELECT FROM_DAYS(TO_DAYS(data_time)) AS dt, min(data_value) FROM table GROUP BY TO_DAYS(NOW())
SELECT DATE_FORMAT(data_time,'YYY-MM-01') AS dt, min(data_value) FROM table GROUP BY dt
Does this work?
SELECT date_time, MIN(data_value)
FROM TABLE
GROUP BY date_time
Oh sorry now you've formatted the data that clearly isn't going to work! You'll need to apply a function to date_time, to cut out the just the Date and ignore the time portion.
In MySQL (and others e.g. DB2) you would do:
SELECT DATE(date_time), MIN(data_value)
FROM TABLE
GROUP BY DATE(date_time)
For SQL Server this looks like it should work, please can someone verify:
SELECT CONVERT(varchar(10), date_time), MIN(data_value)
FROM TABLE
GROUP BY CONVERT(varchar(10), date_time)
Try this
select min(val),substring(date,1,10) from table group by substring(date,1,10)
SELECT (ord_date), min([qty])
FROM [pubs].[dbo].[sales]
group by [ord_date]
order by ord_date
select d(date_time), min(data_value)
from table
group by d(date_time)
where d is a function that extracts the date part from a date_time. (check your SQL reference manual for the function name).
In Oracle SQL that function is trunc.
精彩评论