关于聚合的问题
DeptID PeriodID MenCount shiftID weekid AppointTime AlreadyCount
NULLNULL3103NULL1
NULLNULL333NULL2
NULLNULL101010NULL1
NULLNULL2083NULL1
NULLNULL22101NULL1
NULLNULL23112NULL1
NULLNULL2584NULL1
NULLNULL30193NULL1
NULLNULL3083NULL1
NULLNULL33102NULL1
NULLNULL4083NULL1
NULLNULL44101NULL1
NULLNULL50194NULL1
NULLNULL55195NULL1
NULLNULL5594NULL1
NULLNULL88101NULL1
NULLNULL88107NULL1
NULLNULL8891NULL1
NULLNULL34561NULL2
3125842012-02-02 00:00:00.000
3225842012-02-02 00:00:00.000
declare @AppointTime datetime,@DeptID Nvarchar(50),@ShiftID Nvarchar(50),@WeekID Nvarchar(50)set @deptID='3'set @AppointTime='2012-2-2'set @ShiftID='8'set @WeekID='4'select count(*) AlreadyCount,app.Deptid,app.PeriodID,sd.MenCount ,shiftID ,sd.WeekID,app.AppointTimefrom ShiftDetail sd left join AppointList Appon sd.ShiftID=@ShiftID and sd.WeekID=@WeekID and app.PeriodID=sd.PeriodID and app.AppointTime=@AppointTimegroup by app.AppointTime,app.DeptID,app.PeriodID ,sd.MenCount,shiftID,sd.WeekIDorder by app.periodID
select app.Deptid,app.PeriodID,sd.MenCount ,shiftID ,sd.WeekID,app.AppointTimefrom ShiftDetail sd left join AppointList Appon sd.ShiftID=@ShiftID and sd.WeekID=@WeekID and app.PeriodID=sd.PeriodID and app.AppointTime=@AppointTime
[解决办法]
select count(*) AlreadyCount,app.Deptid,app.PeriodID,sd.MenCount ,shiftID ,sd.WeekID,app.AppointTime
from ShiftDetail sd inner join AppointList App
on sd.ShiftID=@ShiftID and sd.WeekID=@WeekID and app.PeriodID=sd.PeriodID and app.AppointTime=@AppointTime
group by app.AppointTime,app.DeptID,app.PeriodID ,sd.MenCount,shiftID,sd.WeekID
order by app.periodID