I have Table with below data
Id date
-----------------------------------
1 2014-06-11 08:35:00.000
2 2014-06-11 08:35:00.000
3 2014-06-11 08:50:00.000
4 2014-06-11 08:55:00.000
5 2014-06-11 08:55:00.000
I want to query and group the row by date and get below result
date Ids
----------------------------------------
2014-06-11 08:35:00.000 1,2
2014-06-11 08:50:00.000 3
2014-06-11 08:55:00.000 4,5
for that I have wrote a query
SELECT dateadd(millisecond, - datepart(millisecond, EventDate) ,EventDate), COUNT(*),
STUFF((SELECT ',' + CAST(MetricEventId as varchar) FROM MetricEvents ME1 WHERE ME1.MetricEventId = ME.MetricEventId FOR XML PATH('')),1,1,'') FROM MetricEvents ME
GROUP BY dateadd(millisecond, - datepart(millisecond, EventDate) ,EventDate),EventUrl
but I am getting error message saying
Column 'MetricEvents.MetricEventId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I know it wants me to group my query by MetricEvents.MetricEventId as well but that's not my requirement as I want to group them only by date, how can I get my grouped data in my sub query something like ME1.MetricEventId IN (GROUPED IDS)
or is there any alternative
I guess this is what you are looking for:
SELECT t1.Date , (SELECT STUFF((SELECT ','+ cast(t2.id as nvarchar(5))
FROM DemoTable t2 WHERE t2.Date = t1.Date
FOR XML PATH('')), 1, 1, '')) as IDS
FROM DemoTable t1
GROUP By t1.Date
Depending on if you have milliseconds in your data, you can also subtract the milliseconds as in your own SQL query.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.