我有一个Foo
表,具有以下属性:
FooID (PK) | Bar | Price
鉴于示例记录集为:
1 | 332 | 10
2 | 332 | 10
3 | 333 | 5
4 | 334 | 30
4 | 334 | 30
4 | 334 | 30
我希望查询的输出为
1 | 332 | 5
2 | 332 | 5
3 | 333 | 5
4 | 334 | 10
4 | 334 | 10
4 | 334 | 10
换句话说,Price
列应该是以下内容的结果:Price/N
,其中N是给定Bar
在记录集中存在的次数。
我设法使用以下方法来计算存在的Bars
的数量:
select Bar, count(*) as BarCount from Foo GROUP BY Bar
但是,我不知道如何将其应用于Price
的划分。
谢谢!
答案 0 :(得分:4)
您可以使用窗口功能和除法:
select fooid, bar,
price / count(*) over (partition by bar) as new_price
from t;
请注意,SQL Server执行整数除法。因此,如果price
是整数,则可能会得到更准确的结果:
select fooid, bar,
price * 1.0 / count(*) over (partition by bar) as new_price
from t;
编辑:
标题为“更新”,但问题仅提及获得结果。如果要更新,请使用可更新的CTE或子查询:
with toupdate as (
select t.*,
price / count(*) over (partition by bar) as new_price
from t
)
update toupdate
set new_price = price
where price <> new_price;
答案 1 :(得分:2)
您可以使用CTE查找每个Bar
组的计数,然后加入该组以查找商:
WITH cte AS (
SELECT Bar, COUNT(*) AS cnt
FROM yourTable
GROUP BY Bar
)
SELECT t1.FooID, t1.Bar,
1.0 * t1.Price / t2.cnt AS NewPrice
FROM yourTable t1
INNER JOIN cte t2
ON t1.Bar = t2.Bar
ORDER BY
t1.Bar, t1.FooID;
戈登的答案可能是最快的,因为它可以找到计数并在整个表格中一次计算商数。我给出的答案需要一个额外的子查询并加入。
答案 2 :(得分:1)
使用相关子查询来计算除法器:
WITH myTable AS ( SELECT 1 AS FooID, 332 AS Bar, 10 AS Price UNION ALL SELECT 2, 332, 10 UNION ALL SELECT 3, 333, 5 UNION ALL SELECT 4, 334, 30 UNION ALL SELECT 4, 334, 30 UNION ALL SELECT 4, 334, 30 ) SELECT t.fooid, t.bar, t.price / (SELECT COUNT(*) FROM myTable WHERE bar = t.bar) FROM myTable t GO
fooid | bar | (No column name) ----: | --: | ---------------: 1 | 332 | 5 2 | 332 | 5 3 | 333 | 5 4 | 334 | 10 4 | 334 | 10 4 | 334 | 10
db <>提琴here