SQL语句:根据条件选择数据

发布于 2025-01-22 17:27:10 字数 2529 浏览 3 评论 0原文

我想将我的选择查询基于条件,因此类似于:

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

示例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,所以我希望拥有的输出表是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟若柳尘 2025-01-29 17:27:10

似乎您可以在中使用中使用某些条件聚合来检查今天有一个顺序:

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;

或者,或者,您可以使用存在。我会让您测试在您的环境中更绩效的内容。

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;

Seems you could use some conditional aggregation in the HAVING to check there was an order today:

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;

Or, alternatively, you could use an EXISTS. I'll leave you to test what is more performant on your environment.

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

埖埖迣鎅 2025-01-29 17:27:10
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
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
本宫微胖 2025-01-29 17:27:10

另一种方法是使用外部申请来获取今天的卖出/购买

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

Another approach is using outer apply to get the sells/buys for today

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文