高级查询盘点日期
Advanced Query Counting Dates
我正在尝试编写一个查询,该查询将 return 我的活动 Table 中每个活动的销售日期与非销售日期的订单数量。
订单Table
OrderID (PK, INT)
OrderDate (DATE)
EventID (INT,FK)
1
2022-01-01
1
2
2022-01-12
1
3
2022-01-01
1
4
2022-03-10
2
5
2022-03-15
2
事件Table
EventID (PK, FK, INT)
NameEvent (VARCHAR)
OnSalesDate (DATE)
1
The Rolling Stones
2022-01-01
2
The Who Live in Concert
2022-03-10
输出 Table 应该是这样的
Name_Event
BoughtOnSalesDate
BoughtAfter
The Rolling Stones
2
1
The Who Live in Concert
1
1
我目前的查询是:
SELECT OrderDate , EventName
FROM Orders
Left JOIN Event on Orders.EventID = Event.EventID
WHERE Orders.OrderDate = Event.OnSaleDate
ORDER BY OrderDate;
哪些输出:
OrderDate
EventName
2022-01-01
The Rolling Stones
2022-01-01
The Rolling Stones
2022-03-10
The Who Live in Concert
您可以对事件的数据进行分组以获得您想要的结果
CREATE TABLE Orders (
`OrderID` INTEGER,
`OrderDate` VARCHAR(10),
`EventID` INTEGER
);
INSERT INTO Orders
(`OrderID`, `OrderDate`, `EventID`)
VALUES
('1', '2022-01-01', '1'),
('2', '2022-01-12', '1'),
('3', '2022-01-01', '1'),
('4', '2022-03-10', '2'),
('5', '2022-03-15', '2');
CREATE TABLE Event (
`EventID` INTEGER,
`NameEvent` VARCHAR(23),
`OnSalesDate` VARCHAR(10)
);
INSERT INTO Event
(`EventID`, `NameEvent`, `OnSalesDate`)
VALUES
('1', 'The Rolling Stones', '2022-01-01'),
('2', 'The Who Live in Concert', '2022-03-10');
SELECT `NameEvent`,`OnSalesDate`
, SUM(`OnSalesDate` = `OrderDate`) BoughtOnSalesDate
, SUM(`OnSalesDate` < `OrderDate`) BoughtAfter
FROM Orders
Left JOIN Event on Orders.EventID = Event.EventID
GROUP BY Orders.`EventID`, `NameEvent`,`OnSalesDate`
ORDER BY `OnSalesDate`;
NameEvent | OnSalesDate | BoughtOnSalesDate | BoughtAfter
:---------------------- | :---------- | ----------------: | ----------:
The Rolling Stones | 2022-01-01 | 2 | 1
The Who Live in Concert | 2022-03-10 | 1 | 1
db<>fiddle here
我正在尝试编写一个查询,该查询将 return 我的活动 Table 中每个活动的销售日期与非销售日期的订单数量。
订单Table
OrderID (PK, INT) | OrderDate (DATE) | EventID (INT,FK) |
---|---|---|
1 | 2022-01-01 | 1 |
2 | 2022-01-12 | 1 |
3 | 2022-01-01 | 1 |
4 | 2022-03-10 | 2 |
5 | 2022-03-15 | 2 |
事件Table
EventID (PK, FK, INT) | NameEvent (VARCHAR) | OnSalesDate (DATE) |
---|---|---|
1 | The Rolling Stones | 2022-01-01 |
2 | The Who Live in Concert | 2022-03-10 |
输出 Table 应该是这样的
Name_Event | BoughtOnSalesDate | BoughtAfter |
---|---|---|
The Rolling Stones | 2 | 1 |
The Who Live in Concert | 1 | 1 |
我目前的查询是:
SELECT OrderDate , EventName
FROM Orders
Left JOIN Event on Orders.EventID = Event.EventID
WHERE Orders.OrderDate = Event.OnSaleDate
ORDER BY OrderDate;
哪些输出:
OrderDate | EventName |
---|---|
2022-01-01 | The Rolling Stones |
2022-01-01 | The Rolling Stones |
2022-03-10 | The Who Live in Concert |
您可以对事件的数据进行分组以获得您想要的结果
CREATE TABLE Orders ( `OrderID` INTEGER, `OrderDate` VARCHAR(10), `EventID` INTEGER ); INSERT INTO Orders (`OrderID`, `OrderDate`, `EventID`) VALUES ('1', '2022-01-01', '1'), ('2', '2022-01-12', '1'), ('3', '2022-01-01', '1'), ('4', '2022-03-10', '2'), ('5', '2022-03-15', '2');
CREATE TABLE Event ( `EventID` INTEGER, `NameEvent` VARCHAR(23), `OnSalesDate` VARCHAR(10) ); INSERT INTO Event (`EventID`, `NameEvent`, `OnSalesDate`) VALUES ('1', 'The Rolling Stones', '2022-01-01'), ('2', 'The Who Live in Concert', '2022-03-10');
SELECT `NameEvent`,`OnSalesDate` , SUM(`OnSalesDate` = `OrderDate`) BoughtOnSalesDate , SUM(`OnSalesDate` < `OrderDate`) BoughtAfter FROM Orders Left JOIN Event on Orders.EventID = Event.EventID GROUP BY Orders.`EventID`, `NameEvent`,`OnSalesDate` ORDER BY `OnSalesDate`;
NameEvent | OnSalesDate | BoughtOnSalesDate | BoughtAfter :---------------------- | :---------- | ----------------: | ----------: The Rolling Stones | 2022-01-01 | 2 | 1 The Who Live in Concert | 2022-03-10 | 1 | 1
db<>fiddle here