Distinct where statement getting clobbered

时间:2017-01-03 00:05:50

标签: sqlite distinct where

From this database stucture ...

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
);

With this data ...

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);

And using this query

SELECT DISTINCT powId, timeStart, timeEnd FROM points WHERE userId != 1;

I'm not getting the result that I want.

The last two entries in the database what I'm looking at right now. userId is set for the first of those two entries, but the second entry masks the userId and it still returns that distinct row now. Is there anything I can do to that query to make it not return ANY distinct row where that userId is within the DISTINCT set?

I basiclly want to get all points that the userId is not assigned. How do I do that? I figured it would be this simple, but it's not.

Please, only SQLite answers.

1 个答案:

答案 0 :(得分:1)

您必须检查具有相同列值的所有(其他)行:

SELECT DISTINCT powId,
                timeStart,
                timeEnd
FROM points
WHERE NOT EXISTS (SELECT 1
                  FROM points AS p2
                  WHERE p2.powId     = points.powId
                    AND p2.timeStart = points.timeStart
                    AND p2.timeEnd   = points.timeEnd
                    AND p2.userId = 1);