如何在一个SELECT语句中使用两个或更多ACCOUNT?

时间:2010-03-17 05:42:22

标签: sql join count

我开发此代码:

SELECT COUNT(NewEmployee.EmployeeID), NewEmployee.EmployeeId,EmployeeName 
FROM NewEmployee INNER JOIN NewTimeAttendance  
  ON NewEmployee.EmployeeID = NewTimeAttendance.EmployeeID 
  and NewTimeAttendance.TotalTime is  null 
  and (NewTimeAttendance.note = '' or NewTimeAttendance.note is null)
  and (month = 1 or month = 2 or month = 3)  
GROUP BY NewEmployee.EmployeeID, EmployeeName
order by EmployeeID

来自我之前的两个问题 selecting null stuff counting issue ......令人惊叹的代码工作得非常好......但现在我需要select多个count ...

...搜索(谷歌)....找到alias ...尝试:

SELECT COUNT(NewEmployee.EmployeeID) as attenddays, COUNT(NewEmployee.EmployeeID) as empabsent
 , NewEmployee.EmployeeId,EmployeeName 
 FROM NewEmployee INNER JOIN NewTimeAttendance  
  ON empabsent =NewEmployee.EmployeeID = NewTimeAttendance.EmployeeID 
  and NewTimeAttendance.TotalTime is  null 
  and (NewTimeAttendance.note = '' or NewTimeAttendance.note is null )
    and (month=1 or month =2 or month = 3) , 
    attenddays = NewTimeAttendance.EmployeeID 
  and NewTimeAttendance.TotalTime is  null 
  and (NewTimeAttendance.note = '' or NewTimeAttendance.note is null )
    and (month=1 or month =2 or month = 3)
GROUP BY NewEmployee.EmployeeID, EmployeeName  order by EmployeeID

Incorrect syntax near '='.

第二次尝试:

SELECT COUNT(NewEmployee.EmployeeID) as attenddays, COUNT(NewEmployee.EmployeeID) as absentdays,
NewEmployee.EmployeeId,EmployeeName 
FROM NewEmployee INNER JOIN NewTimeAttendance  
  ON attenddays(NewEmployee.EmployeeID = NewTimeAttendance.EmployeeID 
  and NewTimeAttendance.TotalTime is  null 
  and (NewTimeAttendance.note = '' or NewTimeAttendance.note is null )
  and (month=1 or month =2 or month = 3)) ,
  absentdays(NewEmployee.EmployeeID = NewTimeAttendance.EmployeeID 
  and NewTimeAttendance.TotalTime is  null 
  and (NewTimeAttendance.note = '' or NewTimeAttendance.note is null )
  and (month=1 or month =2 or month = 3))
GROUP BY NewEmployee.EmployeeID, EmployeeName  order by EmployeeID

Incorrect syntax near '='.

不是很好的想法...所以......帮助

我想要的只是count具有不同条件的ID ...

COUNT(NewEmployee.EmployeeID)
ON NewEmployee.EmployeeID = NewTimeAttendance.EmployeeID 
  and NewTimeAttendance.TotalTime is  null 
  and (NewTimeAttendance.note = '' or NewTimeAttendance.note is null )
  and (month=1 or month =2 or month = 3)

COUNT(NewEmployee.EmployeeID)
ON NewEmployee.EmployeeID = NewTimeAttendance.EmployeeID 
  and NewTimeAttendance.TotalTime is not  null 
  and (NewTimeAttendance.note = '' or NewTimeAttendance.note is null )
  and (month=1 or month =2 or month = 3)

在相同的选择状态中。

提前致谢

2 个答案:

答案 0 :(得分:5)

看起来您正试图找出每位员工出席和缺席的天数。我建议这样的查询:

SELECT EmployeeId, EmployeeName,
    (SELECT COUNT(*)
     FROM NewTimeAttendance
     WHERE NewTimeAttendance.EmployeeID = NewEmployee.EmployeeId
         /* include condition for employee in attendance */
         AND TotalTime IS NULL
         AND (Note = '' or Note IS NULL)
         AND Month BETWEEN 1 AND 3) AS AttendDays,
    (SELECT COUNT(*)
     FROM NewTimeAttendance
     WHERE NewTimeAttendance.EmployeeID = NewEmployee.EmployeeId
         /* include condition for employee absent */
         AND TotalTime IS NULL
         AND (Note = '' or Note IS NULL)
         AND Month BETWEEN 1 AND 3) AS AbsentDays
FROM NewEmployee
ORDER BY EmployeeID

答案 1 :(得分:1)

由于ON子句中存在大量错误,第一个查询会引发语法错误。即:

1. empabsent =NewEmployee.EmployeeID = NewTimeAttendance.EmployeeID 
2. and (month=1 or month =2 or month = 3) , attenddays = NewTimeAttendance.EmployeeID 

在第二个查询中,AttendDaysAbsentDays应该是用户定义的函数吗?如果它们是,我无法想象这种语法适用于任何SQL方言。

你到底想要达到什么目的?你想算什么?你能提供两种不同的SQL语句来独立地给你你想要的数量,然后我们可以尝试将它们组合起来吗?

编辑鉴于新信息,您应该可以在一个查询中执行此操作:

SELECT EmployeeId, EmployeeName
    , Sum(Case When NewTimeAttendence.TotalTime Is Null Then 1 Else 0 End) As AttendDays
    , Sum(Case When NewTimeAttendence.TotalTime Is Not Null Then 1 Else 0 End) As AbsentDays
FROM NewEmployee
    Join NewTimeAttendence
        On NewTimeAttendence.EmployeeId = NewEmployee.EmployeeeId
Where (NewTimeAttendence.Note = '' Or NewTimeAttendence.Note Is Null)
    And Month In(1,2,3)
Group By EmployeeId, EmployeeName