Código SQL:
Ver originalCREATE TABLE #temp
(
id INT,
fiesta VARCHAR(20),
consumo VARCHAR(50)
)
INSERT INTO #temp VALUES (1, 'BODA', 'BUFFET')
INSERT INTO #temp VALUES (33, 'BODA', 'BUFFET P.')
INSERT INTO #temp VALUES (20, 'BODA', '2 CERVEZAS')
INSERT INTO #temp VALUES (1, 'BODA2', 'BUFFET')
INSERT INTO #temp VALUES (33, 'BODA2', 'BUFFET P.')
INSERT INTO #temp VALUES (20, 'BODA2', '2 CERVEZAS')
INSERT INTO #temp VALUES (21, 'BODA2', 'BUFFET')
INSERT INTO #temp VALUES (22, 'BODA2', 'BUFFET P.')
INSERT INTO #temp VALUES (34, 'BODA2', '2 CERVEZAS')
INSERT INTO #temp VALUES (21, 'BODA2', 'BUFFET')
INSERT INTO #temp VALUES (22, 'BODA2', 'BUFFET P.')
INSERT INTO #temp VALUES (34, 'BODA2', '2 CERVEZAS')
SELECT *,
CASE WHEN (SELECT top 1 id FROM #temp GROUP BY id,fiesta)=id THEN
(
SELECT total FROM (SELECT COUNT(id) total,fiesta FROM #temp GROUP BY fiesta) AS t1 WHERE t1.fiesta=#temp.fiesta
) ELSE 0 END AS total FROM #temp GROUP BY id,fiesta,consumo
saludos!