高级查询计数日期
我正在尝试编写一个查询,该查询将返回事件表中每个事件的销售日期与非销售日期下的订单数量。
订单表
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 |
事件表
EventID (PK, FK, INT) | NameEvent (VARCHAR) | OnSalesDate (DATE) |
---|---|---|
1 | The Rolling Stones | 2022-01-01 |
2 | The Who Live in Concert | 2022-03-10 |
输出表应该类似于
Name_Event | BoughtOnSalesDate | BoughtAfter |
---|---|---|
The Rolling Stones | 2 | 1 |
The Who Live in Concert | 1 | 1 |
My到目前为止我的当前查询是:
SELECT OrderDate , EventName
FROM Orders
Left JOIN Event on Orders.EventID = Event.EventID
WHERE Orders.OrderDate = Event.OnSaleDate
ORDER BY OrderDate;
Which Outputs:
OrderDate | EventName |
---|---|
2022-01-01 | 滚石乐队 |
2022-01-01 | 滚石乐队 |
2022-03-10 | The Who Live in 演唱会 |
I am trying to write a query that will return the number of orders placed during on sales-date vs on non-on sales date for each event in my Event Table.
Orders 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 |
Event 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 |
The Ouput Table should be Something like
Name_Event | BoughtOnSalesDate | BoughtAfter |
---|---|---|
The Rolling Stones | 2 | 1 |
The Who Live in Concert | 1 | 1 |
My current Query that I have so far is:
SELECT OrderDate , EventName
FROM Orders
Left JOIN Event on Orders.EventID = Event.EventID
WHERE Orders.OrderDate = Event.OnSaleDate
ORDER BY OrderDate;
Which Outputs:
OrderDate | EventName |
---|---|
2022-01-01 | The Rolling Stones |
2022-01-01 | The Rolling Stones |
2022-03-10 | The Who Live in Concert |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以按事件数据分组以获得您想要的结果
db<>fiddle 此处
You can Group BY the data of event to get your wanted result
db<>fiddle here