-1

我想在表格末尾添加一个总目标列,但 CUBE 无法做到这一点,因为它说 bx 无效。我该怎么做?

 DECLARE @Weeks VARCHAR(MAX) = ''
    SELECT @Weeks += (QUOTENAME([Week]) + ',') 
FROM
(Select Distinct [Week] from Fixture
) as z
SET @Weeks = LEFT(@Weeks, LEN(@Weeks) - 1)
print @Weeks

DECLARE @SQL NVARCHAR(MAX) = ''
SET @SQL = '

    select *
    from
    (
    Select Home as Team, [Week] as Weeks, [Home Score] as Goals From Fixture
    group by Home,Week, [Home Score]
    UNION
    Select Away as Team, [Week] as Weeks, [Away Score] as Goals From Fixture
    group by Away,Week, [Away Score]
    ) as A
PIVOT
(
    MAX(Goals)
For Weeks
    IN(' + @Weeks + ')
    )b


'
exec (@SQL)
4

1 回答 1

0

您可以像这样更改内部查询:-

Select Home as Team, [Week] as Weeks, [Home Score] as Goals From Fixture
group by Home,Week, [Home Score]
UNION ALL
Select Away as Team, [Week] as Weeks, [Away Score] as Goals From Fixture
group by Away,Week, [Away Score]
UNION ALL
Select 'Total', NULL, SUM([Home Score]) + SUM ([Away Score]) as 'Total Goals' From Fixture

这个逻辑可能会对你有所帮助。

于 2015-08-26T15:02:13.860 回答