此代码适用于Oracle,但它在SQL Server中失败。我是SQL服务器的新手,我不确定解决这个问题的正确方法是什么。
SELECT CASE GROUPING(HireDate)
when 0 then HireDate
else 'TOTAL'
end HireDate,
count(HireDate) as count_hire_date
FROM Faculty
GROUP BY ROLLUP(HireDate);
试图得到这样的东西:
hire_date count_hire_date
1995 10
1996 20
1997 30
TOTAL 60
我是否在SQL服务器中使用某种类型的强制转换,或者我要编写什么来修复此语法错误并使其在SQL Server中运行?
答案 0 :(得分:1)
我认为你的问题是在case表达式和ROLLUP中,它应该是这样的:
SELECT (CASE when GROUPING(HireDate) = 0 then CONVERT(varchar(50),HireDate) else 'TOTAL' end) HireDate,
count(HireDate) as count_hire_date
FROM Faculty
GROUP BY HireDate WITH ROLLUP
请参阅此链接:http://msdn.microsoft.com/en-us/library/ms178544.aspx