私は過去6ヶ月間に販売されていないアイテムのリストを取得したいと考えています。私はアプローチのカップルを試みたが、どれも正しく動作していません。 私を案内してください。 商品番号がある販売データマート用のテーブルが1つあり、アイテムテーブルはすべてのアイテムのリストの別のテーブルからアイテムを取得しています。 ここに2つのアプローチがあります: これはビューなので一時テーブルを作成できませんか?最後の6ヶ月間に販売されていないアイテムのSQLビュー
select distinct a.ItemID, a.TranDate Into #Itemslast6months
from SalesDataMart a
where TranDate >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0)
Select distinct m.ITMNO_0 into #ItemsNotSoldLast6Months
from ITEMMASTER as m
Where not exists (select ItemID
from #Itemslast6months as B
where m.ITMNO_0 = B.ItemID)
Select n.ITMNO_0, s.[Description], max(s.TranDate) last_Transaction_Date
from #temsNotSoldLast6Months n
join SalesDataMart s on n.ITMNO_0 = s.ItemID
group by n.ITMNO_0, s.[Description]
第二のアプローチ:
WITH ItemsSoldLast6Months (ItemID, TranDate)
AS
(
select distinct a.ItemID, a.TranDate
from SalesDataMart a
where TranDate >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp)), 0)
)
WITH ItemsNotSoldLast6Months (ItemNO)
AS
(
Select distinct m.ITMNO_0
from ITEMMASTER as m
Where not exists (select ItemID
from ItemsSoldLast6Months as B
where m.ITMNO_0 = B.ItemID)
)
Select n.ITMNO_0, s.[Description], max(s.TranDate) last_Transaction_Date
from #temsNotSoldLast6Months n
join SalesDataMart s on n.ITMNO_0 = s.ItemID
group by n.ITMNO_0, s.[Description]
おかげからフィルタリングされるだろうテーブルを作成するには、ステートメントを実行する前にドロップテーブルステートメントを追加する必要がありますか? – user3347312