0
這是我的sql查詢和結果。我得到了我期望的結果。但它在我的結果中添加了額外的行。如何在我的方案SQL Server中避免重複的行?
我查詢
Declare @store1001 varchar(10)='1001'
Declare @storeAll varchar(10)='StoreAll'
select ID,StoreID,TotalQtyParent into #tempQuantity
From
(
select ID,@store1001 StoreID,Sum([TotalQtyParent]) TotalQtyParent
FROM [HQMatajer].[dbo].[ItemRelationQty]
where StoreID='1001' and TotalQtyParent is not null
group by ID
Union
select ID,@storeAll StoreID,sum([TotalQtyParent]) TotalQtyParent
FROM [HQMatajer].[dbo].[ItemRelationQty]
where StoreID<>'1001' and TotalQtyParent is not null
group by ID
) totalquantity
輸出查詢
select TotalQty.ID, item.ItemLookupCode,item.Description,
(
case when TotalQty.StoreID = '1001' then TotalQty.TotalQtyParent
else 0 end
) WHS,
(
case when TotalQty1.StoreID = 'StoreAll' then TotalQty1.TotalQtyParent
else 0 end
) Store
FROM
#tempQuantity TotalQty
INNER JOIN #tempQuantity TotalQty1 on TotalQty.ID=TotalQty1.ID
LEFT JOIN HQMatajer.dbo.Item item on item.id=TotalQty.ID
order by WHS desc,Store desc
最終輸出
可以看到ID的每一秒記錄並存儲的值是零(0)。我不想顯示那一行。
請不要建議我在where子句中添加條件,其中商店<> 0.它顯示的結果是因爲我的第一個查詢結果,每個ID都有兩次可用。 感謝
修訂 我刪除從我的輸出查詢一些列理解目的
目前尚不清楚你想要什麼,因爲你明顯地有兩個** ** STOREID每個** ** ID在** [HQMatajer]。[ DBO]。[ItemRelationQty] **。您必須決定如何彙總/漫遊此項。例如,你可以嘗試聚合(SUM)ID – jean