简体   繁体   中英

Error in formation of my SQL Query

The below is my query,its getting a formation error,the query will describe the structure of it .....Could anyone help for this

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

The above is my table structure and i need to get a ouput like Below

Employee  Total_No_of_Days   Bill_Days  Non_Bill_Days
===============================================================
1-Roy     4                  2          2
2-Deb     4                  1          3

You use subqueries in the select part, which is allowed only if they return scalar (single column and single row) value.

It seems that

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

Returns more than one row (due to group by).

It is not completely clear what your intention is. If you explain exactly what is that you want you will get better help. Also, do state your RDBMS engine.

EDIT Taking a (hopefully not so) wild guess try this

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

(do notice that your Total_No_Days will count only rows where AcutalDate is NOT NULL; use COUNT(*) if you want to count all rows)

It looks like you are concatenating strings with + . As far as i know, SQL does not support this. You should take a look at the CONCAT() -function .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM