致结束问题的主持人为什么!!!厌倦了这个网站上的小希特勒
我有以下类型的数据 我想获得每个 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 的内容
客户编号 | 订单号 | 订单类型 | 年月 | 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
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;
输出:
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;
输出:
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;
输出: