SQL 中每组的第一行[已关闭]

问题描述 投票:0回答:1

致结束问题的主持人为什么!!!厌倦了这个网站上的小希特勒

我有以下类型的数据 我想获得每个 orderNo 有多少个“Y”或“N”的总和,然后将每个 OrderType 和客户的答案相加。 如果我进行正常计数,我会得到 4 'N' 我希望这是 1 'N' 他们可以是 'Y's

客户编号 订单号 订单类型 交货日期 年月 请求日期 TF
cc001 0123456 A 2024/10/01 2024/03 2024/09/13 N
cc001 0123456 A 2024/10/01 2024/03 2034/09/13 N
cc001 0123456 A 2024/10/01 2024/03 2024/09/13 N
cc001 0123456 A 2024/10/01 2024/03 2034/09/13 N
cc001 0123459 A 2024/10/01 2024/03 2024/09/13 N
cc001 0123459 A 2024/10/01 2024/03 2034/09/13 N
cc001 0123459 A 2024/10/03 2024/03 2024/09/16 N
cc001 0123459 A 2024/10/03 2024/03 2034/09/16 N
cc001 0123457 B 2024/09/13 2024/03 2024/09/13
cc001 0123457 B 2024/03/13 2024/03 2034/09/13
cc001 9879234 A 2024/03
cc001 9879234 A 2024/03
cc001 9879234 B 2024/03
cc001 9879234 B 2024/03 2034/09/13
cc002 0123467 A 2023/10/01 2023/10 2023/10/13 N
cc002 0123467 A 2023/10/01 2023/10 2023/10/13 N
cc002 0123468 B 2023/09/13 2023/09 2023/09/13
cc002 0123468 B 2023/09/13 2023/09 2023/09/13

TF 由以下推导得出,不存储

    CASE 
        WHEN [Delivered Date]<>'1753-01-01' AND RequestedDate <>'1753-01-01' 
        THEN 
            IIF([Delivered Date] <= RequestedDate,'Y','N' )
        ELSE
            NULL
                    
    END) AS YN,

我需要一些能够总结三个级别的 Y 和 N 的内容

  1. 在客户级别 cc001 2 N AND 1 Y
  2. A 的订单类型级别 cc001 将有 2 N 和 0 Y B 的 cc001 将有 0 N 和 1 Y
  3. 在订单号 0123456 的订单号级别 cc001 处,将有 2 N
    在订单号 0123457 的订单号级别 cc001 处,将有 1 Y

客户级别:

客户编号 订单号 订单类型 年月 TFCountY TFCountN 行号
cc001 0 2 1

客户订单类型级别:

客户编号 订单号 订单类型 年月 TFCountY TFCountN 行号
cc001 A 0 2 1

客户订单类型 订单号

客户编号 订单号 订单类型 年月 TFCountY TFCountN 行号
cc001 0123456 A 24/03 0 2 1

我已经尝试过 Row_Number、group by 和 CrossApply 都无法正常工作,下面是我尝试过的众多尝试中的其中两个,我们将不胜感激。

;WITH cte AS ( Select CustomerNo, OrderType, YearMonth, SUM(CASE WHEN [Delivery Date]<>'1753-01-01' AND RequestedDate <>'1753-01-01' THEN IIF([Delivery Date] <= RequestedDate,1, 0) ELSE 0 END) OVER (PARTITION BY CustomerNo,OrderType,YearMonth)AS OTIFY, ROW_NUMBER() OVER (PARTITION By CustomerNo,orderType,YearMonth ORDER BY CustomerNo,OrderType,YearMonth) as RowNum From @SaleData --Cross apply SELECT SUM(CASE WHEN sd.Delivered Date<>'1753-01-01' AND sd.CustomerRequestedDate <>'1753-01-01' THEN IIF(sd.[Delivered Date] <=sd.CustomerRequestedDate,1, 0) ELSE NULL END) AS OTIFY, c.OrderType, c.YearMonth, c.OrderNo, c.CustomerNo FROM @SaleData sd CROSS APPLY (SELECT TOP 1 * FROM @SaleData sd2 WHERE sd.CustomerNo = sd2.CustomerNo AND sd2.OrderType= sd.OrderType AND sd.YearMonth= sd2.YearMonth AND sd.OrderNo=sd2.OrderNo) c GROUP BY c.CustomerNo, c.OrderType, c.YearMonth, c.OrderNo
    
sql sql-server t-sql common-table-expression row-number
1个回答
0
投票

CTE 获得 TF

WITH OrdersWithTF AS ( SELECT CustomerNo, OrderNo, OrderType, DeliveredDate, YearMonth, RequestedDate, CASE WHEN DeliveredDate IS NOT NULL AND RequestedDate IS NOT NULL THEN IIF(DeliveredDate <= RequestedDate, 'Y', 'N') ELSE NULL END AS TF FROM Orders )

1 级(客户):

SELECT CustomerNo, MAX(CASE WHEN TF = 'Y' THEN 1 ELSE 0 END) AS Y_Count, MAX(CASE WHEN TF = 'N' THEN 1 ELSE 0 END) AS N_Count FROM OrdersWithTF GROUP BY CustomerNo;
输出:

客户编号Y_计数N_Countcc00111cc00211

2 级(订单类型):

SELECT CustomerNo, OrderType, MAX(CASE WHEN TF = 'Y' THEN 1 ELSE 0 END) AS Y_Count, MAX(CASE WHEN TF = 'N' THEN 1 ELSE 0 END) AS N_Count FROM OrdersWithTF GROUP BY CustomerNo, OrderType;
输出:

客户编号订单类型Y_计数N_Countcc001A01cc001B10cc002A01cc002B10

3级(订单号):

SELECT CustomerNo, OrderNo, MAX(CASE WHEN TF = 'Y' THEN 1 ELSE 0 END) AS Y_Count, MAX(CASE WHEN TF = 'N' THEN 1 ELSE 0 END) AS N_Count FROM OrdersWithTF GROUP BY CustomerNo, OrderNo;
输出:

客户编号订单号Y_计数N_Countcc001012345601cc001012345710cc002012346701cc002012346810
© www.soinside.com 2019 - 2024. All rights reserved.