2017-10-18 66 views
0

我有一個查詢。這個查詢是每個產品的計算百分比。我在這個查詢中創建了一個虛擬列,這個列名是'yüzde'。之後,如果產品id相同,我希望將yüzde列轉移到另一個表中的另一列並更新查詢。 我想我需要編寫一個存儲過程。我怎樣才能做到這一點?如何將值傳遞到另一列與兩個查詢

SELECT [ProductVariantId] , 
     count([ProductVariantId]) as bedensayısı, 
     count([ProductVariantId]) * 100.0/(SELECT Top 1 Count(*) as Total  
    FROM [Live_ADL].[dbo].[_INV_ProductCombinationAttributes] 
    Where Size LIKE '%[^0-9]%' and [StockQuantity]>0 
    Group by [ProductVariantId] 
    order by Total Desc) as yüzde 
FROM [Live_ADL].[dbo].[_INV_ProductCombinationAttributes] 
Where Size LIKE '%[^0-9]%' and [StockQuantity]>0 
group by [ProductVariantId] 
order by yüzde desc 

enter image description here

回答

0

你並不真的需要一個SP,你可以做在線,使用CTE例如,東西沿着這些線路:

; with tabyuzde as 
(
SELECT [ProductVariantId] , 
     count([ProductVariantId]) as bedensayısı, 
     count([ProductVariantId]) * 100.0/(SELECT Top 1 Count(*) as Total  
    FROM [Live_ADL].[dbo].[_INV_ProductCombinationAttributes] 
    Where Size LIKE '%[^0-9]%' and [StockQuantity]>0 
    Group by [ProductVariantId] 
    order by Total Desc) as yüzde 
FROM [Live_ADL].[dbo].[_INV_ProductCombinationAttributes] 
Where Size LIKE '%[^0-9]%' and [StockQuantity]>0 
group by [ProductVariantId] 
) 

update x 
set othertablevalue=yüzde 
from 
othertable x 
join tabyuzde t on x.ProductVariantId=t.ProductVariantId 
+0

謝謝。這工作 –

相關問題