SQL 语句:SELECT 数据基于条件

SQL statement: SELECT data based on a condition

我想将我的 select 查询基于一个条件,因此它类似于:

--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;

以下查询给出所有产品列表的输出 table(其中一些产品今天没有订单 + 产品今天有订单)和第一个订单下达的日期时间戳对于每个产品:

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

示例来自 tables SellOrdersBuyOrders :

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,所以我想要的输出table是:

ProductName   | firstdate
---------------------------------------
Apple         | 2021-10-12 13:55:50.100
Orange        | 2021-10-15 08:40:14.670

最好的方法是什么?

WITH CTE (
SELECT * FROM SellOrders WHERE CONVERT(date, OrderPlacementDate) = 
CONVERT(date, GETDATE())
UNION
SELECT * FROM BuyOrders WHERE CONVERT(date, OrderPlacementDate) = 
CONVERT(date, GETDATE())
) todaysOrders

SELECT DISTINCT p.ProductNmae, p.firstdate FROM Products p 
INNER JOIN todaysOrders t ON t.ProductName = p.ProductName

您似乎可以在 HAVING 中使用一些条件聚合来检查今天是否有订单:

WITH Orders AS(
    SELECT SO.ProductID,
           SO.OrderPlacementDate
    FROM dbo.SellOrders SO
    UNION ALL
    SELECT BO.ProductID,
           BO.OrderPlacementDate
    FROM dbo.BuyOrders BO)
SELECT P.ProductName,
       MIN(O.OrderPlacementDate) AS FirstDate
FROM dbo.Product P
     JOIN Orders O ON P.ProductID = O.ProductID
GROUP BY P.ProductName
HAVING COUNT(CASE WHEN O.OrderPlacementDate >= CONVERT(date,GETDATE()) AND O.OrderPlacementDate < DATEADD(DAY,1,CONVERT(date,GETDATE())) THEN 1 END) > 0;

或者,您也可以使用 EXISTS。我会让你去测试什么对你的环境性能更高。

WITH Orders AS(
    SELECT SO.ProductID,
           SO.OrderPlacementDate
    FROM dbo.SellOrders SO
    UNION ALL
    SELECT BO.ProductID,
           BO.OrderPlacementDate
    FROM dbo.BuyOrders BO)
SELECT P.ProductName,
       MIN(O.OrderPlacementDate) AS FirstDate
FROM dbo.Product P
     JOIN Orders O ON P.ProductID = O.ProductID
WHERE EXISTS (SELECT 1
              FROM Orders e
              WHERE e.ProductID = O.ProductID
                AND e.OrderPlacementDate >= CONVERT(date,GETDATE())
                AND e.OrderPlacementDate < DATEADD(DAY,1,CONVERT(date,GETDATE())))
GROUP BY P.ProductName;

db<>fiddle

另一种方法是使用外部应用来获取今天

的sells/buys
select p.ProductName,
       p.FirstDate
from   Products p
  outer apply (select top 1 s.OrderPlacementDate 
               from SellOrders s 
               where s.ProductName = p.ProductName
               and   convert(date, s.OrderPlacementDate) = convert(date, getdate())
              ) sell
  outer apply (select top 1 b.OrderPlacementDate 
               from BuyOrders b
               where b.ProductName = p.ProductName
               and   convert(date, b.OrderPlacementDate) = convert(date, getdate())
              ) buy
where sell.OrderPlacementDate is not null
or buy.OrderPlacementDate is not null

DBFiddle