百分比细分
Percentage breakdown
我想知道是否有人可以帮助我解决这个产品百分比细分挑战。
我有以下代码将数据插入 table。
DROP TABLE IF EXISTS #ProductBreakdown
CREATE TABLE #ProductBreakdown
(
Product VARCHAR(20)
, [TotalSales] decimal(20, 2)
, Percentage decimal(20, 2)
)
;
INSERT INTO #ProductBreakdown VALUES('Product A', 596.42, 0)
INSERT INTO #ProductBreakdown VALUES('Product A', 127.55, 0)
INSERT INTO #ProductBreakdown VALUES('Product A', 736.83, 0)
INSERT INTO #ProductBreakdown VALUES('Product B', 379.51, 0)
INSERT INTO #ProductBreakdown VALUES('Product B', 205.95, 0)
INSERT INTO #ProductBreakdown VALUES('Product C', 905.99, 0)
本SELECT查询returns正确的百分比结果,即
SELECT PB.Product, PB.TotalSales,
[TotalSales] / SUM([TotalSales]) OVER(PARTITION BY Product) * 100 AS PercentageSales
FROM #ProductBreakdown PB
Product
TotalSales
PercentageSales
Product A
596.42
40.83
Product A
127.55
8.73
Product A
736.83
50.44
Product B
379.51
64.82
Product B
205.95
35.18
Product C
905.99
100.00
我现在想使用 UPDATE 语句将这些百分比提交到 table 中。我正在使用以下语句
UPDATE #ProductBreakdown SET [Percentage] =
(SELECT
[TotalSales] / SUM([TotalSales]) OVER(PARTITION BY Product) * 100 AS PercentageSales
FROM #ProductBreakdown PB2 WHERE PB1.Product = PB2.Product AND PB1.TotalSales = PB2.TotalSales)
FROM #ProductBreakdown PB1
SELECT * FROM #ProductBreakdown
但是,这 returns 每行 100%。
有没有人对what/where我出错有什么建议?
您可以将您的计算包含在 CTE 中并简单地更新它:
with updateme as (
select *,
TotalSales / Sum(TotalSales) over(partition by Product) * 100 as PercentageSales
from #ProductBreakdown
)
update updateme set Percentage = PercentageSales;
您犯的错误是您还关联了子查询中的 TotalSales
列,而不仅仅是 Product
.
但是您可以更新 CTE 或派生 table 而无需连接或重新扫描基础 table
UPDATE pb
SET Percentage = pb.PercentageSales
FROM (
SELECT *,
pb.TotalSales / SUM(pb.TotalSales) OVER (PARTITION BY pb.Product) * 100 AS PercentageSales
FROM #ProductBreakdown pb
) pb;
但我还是建议您为此使用视图,否则您将不得不使其与触发器保持同步。
我想知道是否有人可以帮助我解决这个产品百分比细分挑战。
我有以下代码将数据插入 table。
DROP TABLE IF EXISTS #ProductBreakdown
CREATE TABLE #ProductBreakdown
(
Product VARCHAR(20)
, [TotalSales] decimal(20, 2)
, Percentage decimal(20, 2)
)
;
INSERT INTO #ProductBreakdown VALUES('Product A', 596.42, 0)
INSERT INTO #ProductBreakdown VALUES('Product A', 127.55, 0)
INSERT INTO #ProductBreakdown VALUES('Product A', 736.83, 0)
INSERT INTO #ProductBreakdown VALUES('Product B', 379.51, 0)
INSERT INTO #ProductBreakdown VALUES('Product B', 205.95, 0)
INSERT INTO #ProductBreakdown VALUES('Product C', 905.99, 0)
本SELECT查询returns正确的百分比结果,即
SELECT PB.Product, PB.TotalSales,
[TotalSales] / SUM([TotalSales]) OVER(PARTITION BY Product) * 100 AS PercentageSales
FROM #ProductBreakdown PB
Product | TotalSales | PercentageSales |
---|---|---|
Product A | 596.42 | 40.83 |
Product A | 127.55 | 8.73 |
Product A | 736.83 | 50.44 |
Product B | 379.51 | 64.82 |
Product B | 205.95 | 35.18 |
Product C | 905.99 | 100.00 |
我现在想使用 UPDATE 语句将这些百分比提交到 table 中。我正在使用以下语句
UPDATE #ProductBreakdown SET [Percentage] =
(SELECT
[TotalSales] / SUM([TotalSales]) OVER(PARTITION BY Product) * 100 AS PercentageSales
FROM #ProductBreakdown PB2 WHERE PB1.Product = PB2.Product AND PB1.TotalSales = PB2.TotalSales)
FROM #ProductBreakdown PB1
SELECT * FROM #ProductBreakdown
但是,这 returns 每行 100%。
有没有人对what/where我出错有什么建议?
您可以将您的计算包含在 CTE 中并简单地更新它:
with updateme as (
select *,
TotalSales / Sum(TotalSales) over(partition by Product) * 100 as PercentageSales
from #ProductBreakdown
)
update updateme set Percentage = PercentageSales;
您犯的错误是您还关联了子查询中的 TotalSales
列,而不仅仅是 Product
.
但是您可以更新 CTE 或派生 table 而无需连接或重新扫描基础 table
UPDATE pb
SET Percentage = pb.PercentageSales
FROM (
SELECT *,
pb.TotalSales / SUM(pb.TotalSales) OVER (PARTITION BY pb.Product) * 100 AS PercentageSales
FROM #ProductBreakdown pb
) pb;
但我还是建议您为此使用视图,否则您将不得不使其与触发器保持同步。