gpt4 book ai didi

sql - 操作数类型冲突 : date is incompatible with smallint error in sql server

转载 作者:行者123 更新时间:2023-12-05 00:59:16 25 4
gpt4 key购买 nike

我编写了一个 sql 查询来获取 2006 年到 2008 年之间的员工雇佣人数。这是我来自 Adventurework2014.dimemployee 表的代码

SELECT YEAR(cast('HireDate' as int)), DepartmentName,
count(ParentEmployeeKey) AS 'total emplyee join'
FROM DimEmployee
where HireDate between 2006 and 2008
group by DepartmentName, HireDate,FirstName,ParentEmployeeKey
ORDER BY YEAR(HireDate)

我上面的代码显示错误

Operand type clash: date is incompatible with smallint

请帮我解决一下。

最佳答案

你错过了在 where 子句中使用 year() :

where year(HireDate) >= 2006 and year(HireDate) <= 2008 

此外,您也不需要将 cast() 函数与 year() 一起使用,因为它会返回数字类型。

你的 SELECT 语句对我来说很奇怪,当你包含 GROUP BY 时它应该有聚合列:

SELECT YEAR(HireDate), DepartmentName,
count(ParentEmployeeKey) AS 'total emplyee join'
FROM DimEmployee
WHERE year(HireDate) >= 2006 and year(HireDate) <= 2008
GROUP BY DepartmentName, YEAR(HireDate), FirstName, ParentEmployeeKey
ORDER BY YEAR(HireDate);

关于sql - 操作数类型冲突 : date is incompatible with smallint error in sql server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54325847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com