如何更新SQL查询以按月分组数据?

时间:2014-10-01 14:40:06

标签: sql sqlite

我有以下SQL查询,它按天计算分组数据并给出以下结果:

Result of the query

现在我想转换这个SQL查询来计算按月分组的数据,这意味着在列日期应该是:

2014-01
2014-02
2014-03
...

使用DIALS_CNT和其他值的总和(参见下面的示例)

2014-01 | 120 | 56 | 26 | 40

有人可以告诉我如何按价值和按月分组获得列数总和?

非常感谢您的帮助。

SQL查询:

   var DIALS_CNT = "DIALS_CNT";
            var APPT_CNT = "APPT_CNT";
            var CONVERS_CNT = "CONVERS_CNT";
            var CANNOT_REACH_CNT = "CANNOT_REACH_CNT";
            var DIALED_CALLS_TABLE = "dialed_calls";
            var CALL_RESULT_STATE_APPT = "APPT";
            var CALL_RESULT_STATE_CONV_NO_APPT = "CONV_NO_APPT";
            var CALL_RESULT_STATE_CANNOT_REACH = "CANNOT_REACH";

            var DATE_FROM =  $scope.dateFrom;
            var DATE_TO = $scope.dateTo;

            var sqlQuery =
                "SELECT d.date AS DATE, "+
                "IFNULL(DIALS_CNT, 0) AS "+DIALS_CNT+", "+
                "IFNULL(APPT_CNT, 0) AS "+APPT_CNT+", "+
                "IFNULL(CONVERS_CNT, 0) AS "+CONVERS_CNT+", "+
                "IFNULL(CANNOT_REACH_CNT, 0) AS "+CANNOT_REACH_CNT+" "+
                "FROM "+
                "(SELECT DATE('2014-01-01', '+' || (t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) || ' days') date FROM "+
                "(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0, "+
                "(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1, "+
                "(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2, "+
                "(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3, "+
                "(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4) d "+
                "LEFT JOIN "+
                "("+
                "SELECT substr(m.date, 1, 10) as my_date, COUNT(m.ID) AS '"+DIALS_CNT+"', "+
                "(SELECT COUNT(*) FROM "+DIALED_CALLS_TABLE+" subq WHERE subq.call_result = '"+CALL_RESULT_STATE_APPT+"' "+
                "AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) AS '"+APPT_CNT+"', "+
                "    (SELECT COUNT(*) FROM "+DIALED_CALLS_TABLE+" subq WHERE subq.call_result = '"+CALL_RESULT_STATE_CONV_NO_APPT+"' "+
                "AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) AS  '"+CONVERS_CNT+"', "+
                "    (SELECT COUNT(*) FROM "+DIALED_CALLS_TABLE+" subq WHERE subq.call_result = '"+CALL_RESULT_STATE_CANNOT_REACH+"' "+
                "AND substr(m.date, 1, 10) = substr(subq.DATE, 1, 10)) AS '"+CANNOT_REACH_CNT+"' "+
                "FROM "+DIALED_CALLS_TABLE+" m "+
                "GROUP BY my_date "+
                ") t "+
                "ON d.date = t.my_date "+
                "WHERE d.date BETWEEN '"+DATE_FROM+"' AND '"+DATE_TO+"' "+
                "ORDER BY d.date DESC; ";

1 个答案:

答案 0 :(得分:1)

使用the SQLite Date and Time Functions中的strftime

示例:

sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE t (
title text,
date datetime);
INSERT INTO "t" VALUES('foo','2014-09-30');
INSERT INTO "t" VALUES('bar','2014-09-28');
INSERT INTO "t" VALUES('baz','2014-08-27');
COMMIT;
sqlite> select strftime("%Y-%m", date) as d,
               count(*) as c
          from t
      group by strftime("%Y-%m", date);
2014-08|1
2014-09|2

使其适应您的表结构和查询应该很容易。