2017-10-18 62 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

谢谢。这工作 –

相关问题