以下は日常の在庫レベルです。曜日のレコードがない場合は、その日に在庫がゼロだったことを意味します。在庫レベルの計算 - SQL
Week | Product | Inventory on first day of the week | Inventory on last day of the week | No of days product was available
1 A 10 0 4
2 A 0 2 4
の下に、私はインタビューの中で、この質問をしたし、これは例えばのために、この場合、インベントリに第三、第六及び第一週の7日目にゼロであった
Week | Day of the Week | Product | Inventory
1 1 A 10
1 2 A 20
1 4 A 5
1 5 A 5
2 2 A 20
2 3 A 15
2 4 A 5
2 7 A 2
私が探していますが出力されます私が思いついたものインタビュアーは私に、より良いアプローチがあると言っていました。そう。任意の提案は歓迎
WITH CTE_Inventory_Rank
AS (SELECT Week,
[Day of the Week],
Product,
Inventory,
RANK() OVER(PARTITION BY WEEK ORDER BY [Day of the Week]) RankAsc,
RANK() OVER(PARTITION BY WEEK ORDER BY [Day of the Week] DESC) RankDesc
FROM Inventory),
CTE_Inventory
AS (SELECT WEEK,
Product,
COUNT(*) [No of days product was available]
FROM Inventory
GROUP BY WEEK,
PRODUCT)
SELECT I.Week,
I.Product,
CASE
WHEN first.[Day of the Week] = 1
THEN first.Inventory
ELSE 0
END [Inventory on first day of the week],
CASE
WHEN last.[Day of the Week] = 7
THEN last.Inventory
ELSE 0
END [Inventory on first day of the week],
i.[No of days product was available]
FROM CTE_INVENTORY I
JOIN CTE_Inventory_Rank first ON I.week = first.week
AND I.Product = first.Product
AND first.RankAsc = 1
JOIN CTE_Inventory_Rank last ON I.week = last.week
AND I.Product = last.Product
AND last.RankDesc = 1;
? –