我有一張包含樣本資料的表格
Inventory
shop_code product_id date QTY
10 20 20210101 5
**EDIT: Please add additional data above too**
我想選擇過去 60 天內所有日期的 QTY 不為 0 的 shop_codes 和 product_ids。
編輯: OP把這個放在一個答案中,它應該在這里
我想要的是:
select top 1000
SHOPCODE,
ProductId,
COUNT(DateKey) as dayNumber
from
Inventory
where
DateKey > (the date of 60 days ago)
and QTY > 0
group by
SHOPCODE,
ProductID
having
COUNT(DateKey) = ( select COUNT(distinct DateKey)
from Inv where DateKey > (the date of 60 days ago)
)
我不確定這是一種合適的方式,并想知道是否有辦法而不是計算日期
uj5u.com熱心網友回復:
首先,我們計算前 60 天的日期并將其以數字形式放入變數中。然后我們提取所有產品,只要它們的日期是60天并且它們的數量在0以上,我們根據商店和產品ID對它們進行分組。
使用這個片段:
declare @dateInt int
set @dateInt=cast(Convert(CHAR(8),DATEADD(day,-60, getdate()) 1,112) as int)
select product_id
,shop_code
,sum(QTY) as sumQTY
from yourTable
where date>=@dateInt and QTY>0
group by product_id,shop_code
uj5u.com熱心網友回復:
您的問題不是很清楚,但是根據我對它的理解以及對 Sayeed 回答的評論,我認為這可能是您所追求的
select top 1000
i.SHOPCODE,
i.ProductId
from Inventory i
where not exists ( select 1
from inventory i2
where i2.shopcode = i.shopcode
and i2.productid = i.productid
and i2.DateKey > datepart(day, -60, getdate())
and i2.QTY = 0
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/416752.html
標籤:
