我的SQL查询的形成出错

时间:2010-09-30 11:46:26

标签: sql

以下是我的查询,它得到一个编队错误,查询将描述它的结构.....任何人都可以为此提供帮助

SELECT (CAST(Empid AS VARCHAR)+' '+EmployeeName) AS Employee
       ,COUNT(ActualDate)Total_No_Days 
       ,(SELECT COUNT(ActualDate)
            from BufferListforBilling 
            where BufferEmpName IS NOT NULL 
            GROUP BY EmpId) as BillDays
       ,(SELECT COUNT(ActualDate)
            from BufferListforBilling 
            where BufferEmpName IS NULL 
            GROUP BY EmpId) as NonBillDays
FROM BufferListforBilling 
WHERE Team = 'ABC'
GROUP BY Empid ,EmployeeName


Empid   ActualDate    EmpName   BuffEmpNames
===========================================
1       5/6/10        Roy       NULL
1       6/6/10        Roy       NULL
1       7/6/10        Roy       Assigned
1       8/6/10        Roy       Assigned
2       5/6/10        Deb       Assigned
2       6/6/10        Deb       NULL
2       7/6/10        Deb       NULL
2       8/6/10        Deb       NULL

以上是我的表结构,我需要得到像

这样的输出
Employee  Total_No_of_Days   Bill_Days  Non_Bill_Days
===============================================================
1-Roy     4                  2          2
2-Deb     4                  1          3

2 个答案:

答案 0 :(得分:4)

您在select部分中使用子查询,只有当它们返回标量(单列和单行)值时才允许使用子查询。

似乎

SELECT COUNT(ActualDate)
from BufferListforBilling 
where BufferEmpName IS NOT NULL 
GROUP BY EmpId) as BillDay

返回多行(由于分组依据)。

目的还不完全清楚。如果你准确解释你想要什么,你会得到更好的帮助。另外,请说明您的RDBMS引擎。

修改 采取(希望不是那么)疯狂猜测尝试这个

SELECT (CAST(Empid AS VARCHAR)+' '+EmployeeName) AS Employee
       ,COUNT(ActualDate) Total_No_Days 
       ,COUNT(BufferEmpName) as BillDays
       ,COUNT(BufferEmpName)-COUNT(ActualDate) as NonBillDays
FROM BufferListforBilling 
WHERE Team = 'ABC'
GROUP BY Empid ,EmployeeName

(请注意,您的Total_No_Days将只计算AcutalDate为非NULL的行;如果要计算所有行,请使用COUNT(*)

答案 1 :(得分:0)

看起来您正在使用+连接字符串。据我所知,SQL不支持这一点。你应该看一下CONCAT()-function