worksite
出现多少用户的总和。由于两个表的格式相同,是否可以将每个工作地点和日期的#1值和每个工作地点和日期的#2值合并起来,以便它们出现在同一表的另一列上?
看起来像这样:
| Worksite | Date | Users On Site | Completed |
| ---------- | ---------- | ----------- | --------- |
| worksite_1 | 2019-01-01 | 2 | 2
| worksite_2 | 2019-01-01 | 0 | 2
| worksite_1 | 2019-01-02 | 1 | 1
| worksite_2 | 2019-01-02 | 0 | 2
| worksite_1 | 2019-01-03 | 2 | 2
| worksite_1 | 2019-01-04 | 1 | 1
| worksite_2 | 2019-01-04 | 0 | 1
| worksite_1 | 2019-01-05 | 1 | 1
| worksite_2 | 2019-01-05 | 0 | 1
| worksite_1 | 2019-01-06 | 1 | 1
| worksite_2 | 2019-01-06 | 0 | 1
模式(MySQL v5.7)
CREATE TABLE OnSite
(`uid` varchar(55), `worksite_id` varchar(55), `timestamp` datetime)
;
INSERT INTO OnSite
(`uid`, `worksite_id`, `timestamp`)
VALUES
("u12345", "worksite_1", '2019-01-01'),
("u12345", "worksite_1", '2019-01-02'),
("u12345", "worksite_1", '2019-01-03'),
("u12345", "worksite_1", '2019-01-04'),
("u12345", "worksite_1", '2019-01-05'),
("u12345", "worksite_1", '2019-01-06'),
("u1", "worksite_1", '2019-01-01'),
("u1", "worksite_1", '2019-01-02'),
("u1", "worksite_1", '2019-01-05'),
("u1", "worksite_1", '2019-01-06')
;
查询#1
SELECT worksite_id, DATE(timestamp) Date, COUNT(DISTINCT uid) `Users On Site`
FROM OnSite
GROUP BY DATE(timestamp), worksite_id;
| worksite_id | Date | Users On Site |
| ----------- | ---------- | ------------- |
| worksite_1 | 2019-01-01 | 2 |
| worksite_1 | 2019-01-02 | 2 |
| worksite_1 | 2019-01-03 | 1 |
| worksite_1 | 2019-01-04 | 1 |
| worksite_1 | 2019-01-05 | 2 |
| worksite_1 | 2019-01-06 | 2 |
模式(MySQL v5.7)
CREATE TABLE Documents
(`document_id` varchar(55), `uid` varchar(55), `worksite_id` varchar(55), `type` varchar(55), `timestamp` datetime)
;
INSERT INTO Documents
(`document_id`, `uid`, `worksite_id`, `type`, `timestamp`)
VALUES
("1", "u12345", "worksite_1", 'work_permit', '2019-01-01 00:00:00'),
("2", "u12345", "worksite_2", 'job', '2019-01-01 00:00:00'),
("3", "u12345", "worksite_1", 'work_permit', '2019-01-02 00:00:00'),
("4", "u12345", "worksite_2", 'job', '2019-01-02 00:00:00'),
("5", "u12345", "worksite_1", 'work_permit', '2019-01-03 00:00:00'),
("6", "u12345", "worksite_2", 'job', '2019-01-04 00:00:00'),
("7", "u12345", "worksite_1", 'work_permit', '2019-01-04 00:00:00'),
("8", "u12345", "worksite_2", 'work_permit', '2019-01-05 00:00:00'),
("9", "u12345", "worksite_1", 'job', '2019-01-05 00:00:00'),
("10", "u12345", "worksite_2", 'work_permit', '2019-01-06 00:00:00'),
("11", "u12345", "worksite_1", 'work_permit', '2019-01-06 00:00:00'),
("12", "u12345", "worksite_2", 'work_permit', '2019-01-01 00:00:00'),
("13", "u12345", "worksite_1", 'job', '2019-01-01 00:00:00'),
("14", "u12345", "worksite_2", 'work_permit', '2019-01-02 00:00:00'),
("15", "u12345", "worksite_1", 'work_permit', '2019-01-03 00:00:00')
;
查询#2
SELECT worksite_id 'Worksite',
Date(timestamp) Date,
COUNT(worksite_id) `Completed`
FROM Documents
GROUP BY Date(timestamp), worksite_id;
| Worksite | Date | Completed |
| ---------- | ---------- | --------- |
| worksite_1 | 2019-01-01 | 2 |
| worksite_2 | 2019-01-01 | 2 |
| worksite_1 | 2019-01-02 | 1 |
| worksite_2 | 2019-01-02 | 2 |
| worksite_1 | 2019-01-03 | 2 |
| worksite_1 | 2019-01-04 | 1 |
| worksite_2 | 2019-01-04 | 1 |
| worksite_1 | 2019-01-05 | 1 |
| worksite_2 | 2019-01-05 | 1 |
| worksite_1 | 2019-01-06 | 1 |
| worksite_2 | 2019-01-06 | 1 |
答案 0 :(得分:1)
您可以使用JOIN。例如:
SELECT
IFNULL(OnSite.worksite_id, Documents.worksite_id) as `Worksite`,
DATE(IFNULL(OnSite.timestamp, Documents.timestamp)) as `Date`,
COUNT(DISTINCT OnSite.uid) as `Users_On_Site`,
COUNT(Documents.worksite_id) as `Completed`
FROM OnSite
LEFT JOIN Documents ON OnSite.worksite_id = Documents.worksite_id AND DATE(OnSite.timestamp) = DATE(Documents.timestamp)
GROUP BY `Date`, `Worksite`;