SQL lite如何获取按日期(日,而不是白天)和ENUM分组的列数

时间:2014-09-03 17:35:15

标签: sql sqlite

我想问:我怎样才能得到这个:

ID | DATE | NUMBER| RESULT | TIME| SYNCED
----------------------------------------------------------
1|2014-09-02 15:54:34+0200|800123456|NOT_INTERESTED|10|0
2|2014-09-02 15:56:30+0200|800123456|NO_APPOINTMENT|10|0
3|2014-09-02 16:01:49+0200|800123456|DONE|9|0
4|2014-09-02 16:03:03+0200|800123456|NO_APPOINTMENT|69|0
5|2014-09-02 18:09:34+0200|800123456|NO_APPOINTMENT|3|0
6|2014-09-02 18:54:02+0200|123456789|NO_APPOINTMENT|89|0
7|2014-09-02 18:55:25+0200|123456789|NOT_INTERESTED|89|0
8|2014-09-03 18:36:58+0200|123456789|DONE|185|0
// TABLE STRUCTURE

对此:

     ID | DATE | NUMBER_OF_ALL_CALLS| RESULT_DONE | RESULT_NOT_INTERESTED|RESULT_NO_APP| 
    ----------------------------------------------------------
    1|2014-09-02|6|1|1|4
    2|2014-09-03|2|1|1|0

我尝试使用计数和分组,但没有正确的结果。

此示例的SQLite查询是什么?

编辑:

添加了原始表格结构的图像:

enter image description here

2 个答案:

答案 0 :(得分:2)

编辑:考虑到时区问题,此查询将有效,但不会考虑时区:

SELECT substr(m.date, 1, 10) as my_date, COUNT(m.ID) AS 'NUMBER_OF_ALL_CALLS',
(SELECT COUNT(*) FROM dialed_calls subq WHERE subq.call_result = 'DONE'
AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) as 'RESULT_DONE',
(SELECT COUNT(*) FROM dialed_calls subq WHERE subq.call_result = 'NOT_INTERESTED' 
AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) as 'RESULT_NOT_INTERESTED',
(SELECT COUNT(*) FROM dialed_calls subq WHERE subq.call_result = 'NO_APPOINTMENT'
AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) as 'RESULT_NO_APP'
FROM dialed_calls m
GROUP BY my_date;

我在在线SQLLite引擎中验证了上述内容,似乎可以完成这项工作。

不确定为什么要包含' id'列中的所需结果?你想要将几个ID(具有相同的一天)组合在一起,因此在那里选择是不正确的。

---编辑--- 这适用于我online SQLLite environment。请验证并保留我的信息。

BEGIN TRANSACTION;

CREATE TABLE dialed_calls(Id integer PRIMARY KEY,
'date' datetime,
'called_number' VARCHAR(45),
'call_result' VARCHAR(45),
'call_duration' INT,
'synced' BOOL);

/* Create few records in this table */
INSERT INTO dialed_calls VALUES(1,'2014-09-02 15:54:34+0200',
'800123456', 'NOT_INTERESTED', 10, 0);
INSERT INTO dialed_calls VALUES(2,'2014-09-02 15:56:30+0200',
'800123456', 'NO_APPOINTMENT', 10, 0);
INSERT INTO dialed_calls VALUES(3,'2014-09-02 16:01:49+0200',
'800123456', 'DONE', 9, 0);
INSERT INTO dialed_calls VALUES(4,'2014-09-02 16:03:03+0200',
'800123456', 'NO_APPOINTMENT', 69, 0);
INSERT INTO dialed_calls VALUES(5,'2014-09-02 18:09:34+0200',
'800123456', 'NO_APPOINTMENT', 3, 0);
INSERT INTO dialed_calls VALUES(6,'2014-09-02 18:54:02+0200',
'123456789', 'NO_APPOINTMENT', 89, 0);
INSERT INTO dialed_calls VALUES(7,'2014-09-02 18:55:25+0200',
'123456789', 'NOT_INTERESTED', 89, 0);
INSERT INTO dialed_calls VALUES(8,'2014-09-03 18:36:58+0200',
'123456789', 'DONE', 185, 0);
COMMIT;


SELECT substr(m.date, 1, 10) as my_date, COUNT(m.ID) AS 'NUMBER_OF_ALL_CALLS',
(SELECT COUNT(*) FROM dialed_calls subq WHERE subq.call_result = 'DONE'
AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) as 'RESULT_DONE',
(SELECT COUNT(*) FROM dialed_calls subq WHERE subq.call_result = 'NOT_INTERESTED' 
AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) as 'RESULT_NOT_INTERESTED',
(SELECT COUNT(*) FROM dialed_calls subq WHERE subq.call_result = 'NO_APPOINTMENT'
AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) as 'RESULT_NO_APP'
FROM dialed_calls m
GROUP BY my_date;

答案 1 :(得分:0)

尝试分组

 convert(varchar(10), @date, 111) // 2014/09/03

在转换为varchar时,时间相关信息被剥离,为您留下纯粹的日期。如果您希望再次将其用作DateTime数据类型,只需将其转换回DateTime。

如果输出很重要,请参阅here了解不同的格式选项。