在没有匹配项的列中返回计数

时间:2017-01-09 10:34:54

标签: sqlite

我有这两张桌子。

调度

CREATE TABLE dispatch (
    ID INTEGER PRIMARY KEY NOT NULL,
    FDID INTEGER,
    COUNTYNUM TEXT,
    TRANS1 TEXT
);

有了这些数据......

INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470638','28','201612310026','2016-12-31T00:26:41.123-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470640','26','201612310031','2016-12-31T00:31:34.747-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470670','26','201612311136','2016-12-31T11:36:33.323-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470688','26','201612311332','2016-12-31T13:32:09.940-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470719','26','201612311929','2016-12-31T19:29:23.487-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470749','28','201612312301','2016-12-31T23:02:06.607-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470769','26','201701010033','2017-01-01T00:33:46.750-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470849','28','201701012101','2017-01-01T21:01:38.073-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470880','28','201701020640','2017-01-02T06:41:10.087-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470893','28','201701021110','2017-01-02T11:10:28.280-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470912','28','201701021333','2017-01-02T13:33:31.247-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470915','26','201701021350','2017-01-02T13:50:48.440-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470918','28','201701021416','2017-01-02T14:16:37.833-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470935','26','201701021546','2017-01-02T15:46:07.347-05:00');
INSERT INTO "dispatch" ("ID","FDID","COUNTYNUM","TRANS1") VALUES ('470960','28','201701022009','2017-01-02T20:09:33.177-05:00');

CREATE TABLE points (
    rowid INTEGER PRIMARY KEY,
    powId INTEGER,
    userId INTEGER,
    timeStart DATETIME,
    timeEnd DATETIME,
    points NUMERIC,
    COUNTYNUM NUMERIC,
--  FOREIGN KEY(powId) REFERENCES points_power(powId)
--  FOREIGN KEY(userId) REFERENCES users(userId)
    UNIQUE(userId, timeStart, COUNTYNUM) ON CONFLICT IGNORE
);

有了这些数据......

INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('1','7','2','2016-12-31T19:29:23-0500','2016-12-31T20:29:23-0500','2','201612311929');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('2','7','4','2016-12-31T19:29:23-0500','2016-12-31T20:29:23-0500','2','201612311929');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('3','7','12','2016-12-31T19:29:23-0500','2016-12-31T20:29:23-0500','2','201612311929');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('4','7','16','2016-12-31T19:29:23-0500','2016-12-31T20:29:23-0500','2','201612311929');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('5','7','585','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('6','7','2','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('7','7','12','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('9','7','11','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('10','7','593','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('11','7','14','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('12','7','13','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('13','7','11','2017-01-02T13:50:48-0500','2017-01-02T14:50:48-0500','2','201701021350');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('14','7','16','2017-01-02T15:46:07-0500','2017-01-02T16:46:07-0500','2','201701021546');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('15','7','16','2017-01-01T00:33:46-0500','2017-01-01T01:33:46-0500','2','201701010033');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('16','7','2','2017-01-02T13:50:48-0500','2017-01-02T14:50:48-0500','2','201701021350');
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('17','13','1','2017-01-02T17:00:00-0500','2017-01-02T21:00:00-0500','1',NULL);
INSERT INTO "points" ("rowid","powId","userId","timeStart","timeEnd","points","COUNTYNUM") VALUES ('18','13','2','2017-01-02T17:00:00-0500','2017-01-02T21:00:00-0500','1',NULL);

我正在尝试根据这两个表生成报告。所以我使用了这个查询,但它并没有给我提供我想要的输出。

SELECT strftime('%Y', TRANS1) AS Year, strftime('%m', TRANS1) AS Month, COUNT(*) AS Incidents, (SELECT COUNT(COUNTYNUM) FROM points WHERE dispatch.COUNTYNUM = points.COUNTYNUM) AS Zero FROM dispatch WHERE FDID = 26 GROUP BY Year, Month;

我正在......

Year  Month  Incidents  Zero
2016  12     3          0
2017  01     4          8

当我进行此查询时...

SELECT ID, FDID, COUNTYNUM, TRANS1, (SELECT COUNT(*) FROM points WHERE points.COUNTYNUM = dispatch.COUNTYNUM) AS Zero FROM dispatch WHERE FDID = 26 AND Zero = 0;

我得到了正确的结果......

ID      FDID  COUNTYNUM     TRANS1                         Zero
470640  26    201612310031  2016-12-31T00:31:34.747-05:00  0
470670  26    201612311136  2016-12-31T11:36:33.323-05:00  0
470688  26    201612311332  2016-12-31T13:32:09.940-05:00  0

这是正确的,但这意味着之前的查询应该返回此表。

Year  Month  Incidents  Zero
2016  12     4          1
2017  01     3          0

因为COUNTYNUM 201612311929不在结果集中。

我不确定我现在是否对此有任何意义。 Rubby ducky调试到这个盒子实际上让我更加困惑。主要是因为我提供的示例数据集产生的结果与我的实际生产数据集不同。如果有人对我正在尝试做什么有任何想法,我会很感激帮助。

1 个答案:

答案 0 :(得分:0)

这些是您的查询的相关部分:

SELECT ...,
       (SELECT ... WHERE ... = dispatch.COUNTYNUM)
FROM dispatch
GROUP BY Year, Month;

相关子查询引用COUNTYNUM列,但这不是表格分组的列之一。这意味着对于每个结果行,组中有多个可能的COUNTYNUM值。你得到的是随机的。 (这是为了与MySQL兼容;其他数据库报告错误。)