SQL语句:根据条件选择数据
我想将我的选择查询基于条件,因此类似于:
--THE LOGIC TO BE IMPLEMENTED
IF ( SELECT OrderPlacementDate from SellOrders WHERE SellOrders.ProductName = Products.ProductName UNION SELECT OrderPlacementDate from BuyOrders WHERE BuyOrders.ProductName = Products.ProductName is today then :
SELECT Products.ProductName, tblCommun.firstdate
FROM Products
OUTER APPLY (SELECT TOP 1 OrderPlacementDate as firstdate
FROM ( SELECT OrderPlacementDate from SellOrders WHERE SellOrders.ProductName = Products.ProductName UNION SELECT OrderPlacementDate from BuyOrders WHERE BuyOrders.ProductName = Products.ProductName) t
ORDER BY OrderPlacementDate ASC) tblCommun;
以下查询给所有产品列表的输出表(其中一些产品中的某些产品今天没有订单 +今天的订单)和日期时间戳为每个产品放置第一订单时:
SELECT Products.ProductName, tblCommun.firstdate
FROM Products
OUTER APPLY (SELECT TOP 1 OrderPlacementDate as firstdate
FROM ( SELECT OrderPlacementDate from SellOrders WHERE SellOrders.ProductName = Products.ProductName UNION SELECT OrderPlacementDate from BuyOrders WHERE BuyOrders.ProductName = Products.ProductName) t
ORDER BY OrderPlacementDate ASC) tblCommun;
ProductName | firstdate
---------------------------------------
Apple | 2021-10-12 13:55:50.100
Orange | 2021-10-15 08:40:14.670
Carrot | 2021-10-29 08:45:20.110
示例sellorders
和buyorders
:
sellorders
OrderID | ProductName | OrderPlacementDate
-------------------------------------------------
546 | Apple | 2022-04-20 13:55:30.100
547 | Apple | 2022-04-20 14:55:50.100
548 | Orange | 2022-04-20 15:40:50.100
340 | Carrot | 2022-04-19 13:55:50.100
230 | Carrot | 2022-04-18 16:50:50.100
buyorders
OrderID | ProductName | OrderPlacementDate
-------------------------------------------------
500 | Apple | 2022-04-20 10:55:30.100
540 | Apple | 2022-04-20 12:55:50.100
267 | Carrot | 2022-04-19 10:55:50.100
235 | Carrot | 2022-04-18 15:50:50.100
229 | Orange | 2022-04-18 13:40:50.100
目标>目标:我只想显示今天下达订单的产品列表。
今天是2022-04-20
,所以我希望拥有的输出表是:
ProductName | firstdate
---------------------------------------
Apple | 2021-10-12 13:55:50.100
Orange | 2021-10-15 08:40:14.670
最佳方法是什么?
I want to base my select query on a condition so it is something like:
--THE LOGIC TO BE IMPLEMENTED
IF ( SELECT OrderPlacementDate from SellOrders WHERE SellOrders.ProductName = Products.ProductName UNION SELECT OrderPlacementDate from BuyOrders WHERE BuyOrders.ProductName = Products.ProductName is today then :
SELECT Products.ProductName, tblCommun.firstdate
FROM Products
OUTER APPLY (SELECT TOP 1 OrderPlacementDate as firstdate
FROM ( SELECT OrderPlacementDate from SellOrders WHERE SellOrders.ProductName = Products.ProductName UNION SELECT OrderPlacementDate from BuyOrders WHERE BuyOrders.ProductName = Products.ProductName) t
ORDER BY OrderPlacementDate ASC) tblCommun;
The following query gives an output table for the list of all products (where some of these product have no orders for today + products have orders for today) and the date timestamp when a first order was placed for every product:
SELECT Products.ProductName, tblCommun.firstdate
FROM Products
OUTER APPLY (SELECT TOP 1 OrderPlacementDate as firstdate
FROM ( SELECT OrderPlacementDate from SellOrders WHERE SellOrders.ProductName = Products.ProductName UNION SELECT OrderPlacementDate from BuyOrders WHERE BuyOrders.ProductName = Products.ProductName) t
ORDER BY OrderPlacementDate ASC) tblCommun;
ProductName | firstdate
---------------------------------------
Apple | 2021-10-12 13:55:50.100
Orange | 2021-10-15 08:40:14.670
Carrot | 2021-10-29 08:45:20.110
Example from tables SellOrders
and BuyOrders
:
SellOrders
OrderID | ProductName | OrderPlacementDate
-------------------------------------------------
546 | Apple | 2022-04-20 13:55:30.100
547 | Apple | 2022-04-20 14:55:50.100
548 | Orange | 2022-04-20 15:40:50.100
340 | Carrot | 2022-04-19 13:55:50.100
230 | Carrot | 2022-04-18 16:50:50.100
BuyOrders
OrderID | ProductName | OrderPlacementDate
-------------------------------------------------
500 | Apple | 2022-04-20 10:55:30.100
540 | Apple | 2022-04-20 12:55:50.100
267 | Carrot | 2022-04-19 10:55:50.100
235 | Carrot | 2022-04-18 15:50:50.100
229 | Orange | 2022-04-18 13:40:50.100
The goal: I want to display only the list of products where there was orders placed today.
Today is 2022-04-20
, so the output table that I wish to have is:
ProductName | firstdate
---------------------------------------
Apple | 2021-10-12 13:55:50.100
Orange | 2021-10-15 08:40:14.670
What is the best way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎您可以在
中使用
中使用某些条件聚合来检查今天有一个顺序:或者,或者,您可以使用
存在
。我会让您测试在您的环境中更绩效的内容。Seems you could use some conditional aggregation in the
HAVING
to check there was an order today:Or, alternatively, you could use an
EXISTS
. I'll leave you to test what is more performant on your environment.db<>fiddle
另一种方法是使用外部申请来获取今天的卖出/购买
Another approach is using outer apply to get the sells/buys for today
DBFiddle