问题
i have a data in a table, column id and column date
id || datetime
1 || 2013-05-24 19:23:16
2 || 2013-05-28 19:24:20
3 || 2013-05-28 19:25:05
4 || 2013-05-30 19:25:39
5 || 2013-05-30 19:26:05
how query to show only one day with count?or one row with count. so the result is.
|| datetime || count
1 || 2013-05-24 19:23:16 || 1
2 || 2013-05-28 19:24:20 || 2
3 || 2013-05-30 19:25:39 || 2
i try in mysql and it works , there is my query.
SELECT id, GROUP_CONCAT( datetime ) AS date, COUNT( id ) AS count
FROM dataPetak
GROUP BY DATE( datetime )
but when i use sqlite the result is gives me all in one string
5 || 2013-05-24 19:23:16,2013-05-28 19:24:20,2013-05-28 19:25:05,2013-05-30 19:25:39,2013-05-30 19:26:05 || 5
how to fix that?
help me
EDIT
when i use query
SELECT [datetime], [count] FROM (SELECT MAX([datetime]) 'datetime', COUN
T(*) 'count' FROM dataPetak GROUP BY CAST ([datetime] AS DATE)) t;
the result is
2013-05-24 19:23:16 || 1
2013-05-28 19:24:20 || 2
2013-05-30 19:25:39 || 2
how to use row number in sqlite?
so the result is
1 || 2013-05-24 19:23:16 || 1
2 || 2013-05-28 19:24:20 || 2
3 || 2013-05-30 19:25:39 || 2
when i try this query ,
SELECT ROW_NUMBER () OVER (ORDER BY [datetime]) No, [datetime], [count] FROM (SELECT MAX([datetime]) 'datetime', COUN
T(*) 'count' FROM dataPetak GROUP BY CAST ([datetime] AS DATE)) t;
show the error
Error: near "(": syntax error
how to fix that?
回答1:
There is no need for GROUP_CONCAT()
functionality in your case. Your last query in the question is for SQL Server. Sqlite doesn't have implementation for ROW_NUMBER()
.
This being said, try
SELECT
(
SELECT COUNT(*)
FROM
( SELECT 1
FROM dataPetak
WHERE id <= t.id
GROUP BY DATE(datetime)
) q
) No, datetime, count
FROM
(
SELECT id, MIN(datetime) datetime, COUNT(*) count
FROM dataPetak
GROUP BY DATE(datetime)
) t
Output:
| No | datetime | count |
------------------------------------
| 1 | 2013-05-24 19:23:16 | 1 |
| 2 | 2013-05-28 19:24:20 | 2 |
| 3 | 2013-05-30 19:25:39 | 2 |
Here is SQLFiddle demo
回答2:
Try this query
select date(datetime) as date, count(*) as count from tbl
group by date(datetime)
FIDDLE
| date | count |
----------------------
| 2013-05-24 | 1 |
| 2013-05-28 | 2 |
| 2013-05-30 | 2 |
来源:https://stackoverflow.com/questions/16837958/group-concat-and-how-to-use-row-number-in-sqlite