ID Offered SalesID ItemID DateTime
------------------------------------------
123456 1 36 1 2015-08-17
123456 1 36 2 2015-08-17
123456 1 36 3 2015-08-17
如何计算值为1的Offered
列在另一列中出现3次?
答案 0 :(得分:0)
将表连接到子查询,该子查询查找Offered
值的频率:
select s.*, OfferedCount
from (select Offered, count(*) OfferedCount
from Sales group by Offered) c
join Sales s on s.Offered = c.Offered
请参阅live demo。
答案 1 :(得分:0)
WITH cte AS (
SELECT * FROM (VALUES
(123456, 1, 36, 1, '2015-08-17'),
(123456, 1, 36, 2, '2015-08-17'),
(123456, 1, 36, 3, '2015-08-17')
) AS x(ID, Offered, SalesID, ItemID, DateTime)
)
SELECT *,
SUM(CASE WHEN Offered = 1 THEN 1 ELSE 0 END) OVER (PARTITION BY ID) AS OfferedCount
FROM cte
如果您的Offered
列只是一个列,您只需将其汇总而不是我已获得的CASE表达式来简化它。