我坚持按id/rev对表中的项目求和。
我需要从内容字段中减去值来计算总和。传递的是加号,返回的是减号,记录有时间戳,我需要表中不同id的最后一次重置之前的总和。例如,id1的最后一次重置时间是2018-01-12 14:10,我只需要这个日期之后的3个sysx和。有可能在一个查询中得到9个和吗?
我需要以下结果:
ID1 SYS1 +/-x items (delivered items - returned items until the most reset for this ID)
ID1 SYS2 +/-x items
ID1 SYS3 +/-x items
ID2 SYS1 +/-x items
ID2 SYS2 +/-x items
ID2 SYS3 +/-x items
ID3 SYS1 +/-x items
ID3 SYS2 +/-x items
ID3 SYS3 +/-x items
CREATE TABLE docs (
tDate DATETIME NULL,
id varchar(200) NOT NULL,
rev varchar(200)NOT NULL,
content varchar(200) NOT NULL
);
INSERT INTO docs (tDate, id, rev, content) VALUES
('2018-01-13 12:10','ID1', 'SYS1', 'returned 3 items'),
('2018-01-13 12:05','ID1', 'SYS2', 'delivered items: 4'),
('2018-01-13 12:00','ID2', 'SYS3', 'returned 2 items'),
('2018-01-12 14:10','ID1', 'RESET', ''),
('2018-01-12 12:50','ID2', 'SYS1', 'delivered items: 1'),
('2018-01-12 12:40','ID3', 'SYS2', 'returned 1 item'),
('2018-01-12 12:30','ID3', 'SYS3', 'delivered items: 1'),
('2018-01-12 12:20','ID2', 'SYS1', 'delivered items: 1'),
('2018-01-11 12:00','ID3', 'SYS2', 'returned 1 item'),
('2018-01-11 12:00','ID1', 'SYS2', 'delivered items: 1'),
('2018-01-11 12:00','ID3', 'SYS3', 'returned 3 items'),
('2018-01-10 12:10','ID1', 'RESET', ''),
('2018-01-10 12:00','ID2', 'SYS3', 'delivered items: 1'),
('2018-01-10 12:00','ID3', 'SYS1', 'delivered items: 1'),
('2018-01-09 13:20','ID2', 'SYS2', 'delivered items: 3'),
('2018-01-08 14:00','ID3', 'SYS1', 'delivered items: 1'),
('2018-01-07 14:10','ID3', 'RESET', ''),
('2018-01-07 12:00','ID1', 'SYS2', 'returned 2 items'),
('2018-01-06 13:00','ID1', 'SYS1', 'delivered items: 1'),
('2018-01-05 14:00','ID2', 'SYS2', 'delivered items: 3'),
('2018-01-05 13:00','ID2', 'SYS3', 'delivered items: 2'),
('2018-01-05 12:00','ID3', 'SYS2', 'returned 1 item'),
('2018-01-04 17:00','ID3', 'SYS1', 'delivered items: 2'),
('2018-01-03 17:10','ID2', 'RESET', ''),
('2018-01-02 18:00','ID2', 'SYS3', 'delivered items: 2'),
('2018-01-01 19:00','ID3', 'SYS2', 'returned 1 item');
1条答案
按热度按时间3okqufwl1#