一段很复杂很复杂的SQL语句。。求大神解释。。。
我只想知道到底那个select跟哪个from的。。好乱,完全不知道那些select想找的是什麼东西。。。跪求大神解释。。...
我只想知道到底那个select跟哪个from的。。好乱,完全不知道那些select想找的是什麼东西。。。跪求大神解释。。
展开
3个回答
展开全部
Select
*
From
(
Select
*
From
(
Select
Number
,ChineseName
,tb1.DepartmentCode
,DepartmentName
,ShiftId
,ShiftName
,Degree
,AskForLeaveType
,WorkOnDate
,OnDutyType
,StartTime
,EndTime
,AOnDuty
,AOffDuty
,Allday
,tb1.SumTotal
,'Error'=
Case
When Degree=1 AND StartTime<=AOnDuty AND EndTime<=AOnDuty AND ShiftId>0 AND Left(AskForLeaveType,1)='0' Then 0
When Degree=1 AND StartTime>=AOffDuty AND ShiftId>0 AND Left(AskForLeaveType,1)='0' Then 0
When Degree=1 AND StartTime>=WorkOnDate+' '+'12:30' AND EndTime <= WorkOnDate+' '+'13:30' AND Charindex('常白班',ShiftName)>0 AND ShiftId>0 AND Left(AskForLeaveType,1)='0' Then 0
When Degree=2 AND StartTime<=AOnDuty AND EndTime<=AOnDuty AND ShiftId>0 AND Left(AskForLeaveType,1)='0' Then 0
When Degree=2 AND StartTime>=AOffDuty AND EndTime<=BOnDuty AND ShiftId>0 AND Left(AskForLeaveType,1)='0' Then 0
When Degree=2 AND StartTime>=BOffDuty AND ShiftId>0 AND Left(AskForLeaveType,1)='0' Then 0
When Degree=3 AND StartTime<=AOnDuty AND EndTime<=AOnDuty AND ShiftId>0 AND Left(AskForLeaveType,1)='0' Then 0
When Degree=3 AND StartTime>=AOffDuty AND EndTime<=BOnDuty AND ShiftId>0 AND Left(AskForLeaveType,1)='0' Then 0
When Degree=3 AND StartTime>=BOffDuty AND EndTime<=COnDuty AND ShiftId>0 AND Left(AskForLeaveType,1)='0' Then 0
When Degree=3 AND StartTime>=COffDuty AND ShiftId>0 AND Left(AskForLeaveType,1)='0' Then 0
When ShiftId=0 AND Left(AskForLeaveType,1)='0' Then 0
When ShiftId>0 AND Left(AskForLeaveType,1)<>'0' AND Left(OnDutyType,1)=0 Then 0
When ShiftId>0 AND Left(AskForLeaveType,1)<>'0' AND Left(OnDutyType,1)=2 AND AskForLeaveType='出差假' Then 0
When ShiftId>0 AND Left(AskForLeaveType,1)<>'0' AND Left(OnDutyType,1)=2 AND AskForLeaveType='产假' Then 0
When ShiftId=0 AND Left(AskForLeaveType,1)<>'0' AND Convert(varchar(10),StartTime,108)='00:00:00' AND Convert(varchar(10),EndTime,108)='00:30:00' Then 0
Else
1
End
From
(
Select
Number
,ChineseName
,DepartmentName
,d.DepartmentCode
,p.ShiftId
,p.ShiftName
,Degree
,AskForLeaveType
,Convert(varchar(10),WorkOnDate,120) as 'WorkOnDate'
,StartTime
,EndTime
,AllDay
,SumTotal
,Case When Right(Convert(Varchar(10),WorkOnDate,120),5) IN (select Right(Convert(Varchar(10),HolidayDate,120),5) From AttendanceHoliday) THEN '2 - 公众假上班' ELSE '0 - 平时上班' END AS 'OnDutyType'
,Case When Left(FristBrushDate,1)='1' AND Convert(varchar(10),AOffDuty,120)>Convert(varchar(10),AOnDuty,120) Then CAST(Convert(Varchar(11),Dateadd(dd,-1,OnDutyDate),120)+Convert(Varchar(5),AOnDuty,108) AS DateTime) ELSE CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),AOnDuty,108) AS DateTime) End AS 'AOnDuty'
,Case When Left(FristBrushDate,1)='0' AND Convert(varchar(10),AOnDuty,120)<Convert(varchar(10),AOffDuty,120) Then CAST(Convert(Varchar(11),Dateadd(dd,1,OnDutyDate),120)+Convert(Varchar(5),AOffDuty,108) AS DateTime) ELSE CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),AOffDuty,108) AS DateTime) End AS 'AOffDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),BOnDuty,108) AS DateTime) AS 'BOnDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),BOffDuty,108) AS DateTime) AS 'BOffDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),COnDuty,108) AS DateTime) AS 'COnDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),COffDuty,108) AS DateTime) AS 'COffDuty'
From
Employees e INNER JOIN Departments d on e.DepartmentCode=d.DepartmentCode
INNER JOIN AttendanceAskForLeave a on e.Employeeid=a.Employeeid
INNER JOIN AttendanceSpell p on a.Employeeid=p.Employeeid and a.WorkOndate=p.OnDutyDate
LEFT JOIN AttendanceShifts s on p.Shiftid=s.Shiftid
) tb1
) tb2 Where Error=1
UNION ALL
Select
Number
,ChineseName
,DepartmentCode
,DepartmentName
,ShiftId
,ShiftName
,Degree
,AskForLeaveType
,WorkOnDate
,Case When Right(Convert(Varchar(10),WorkOnDate,120),5) IN (select Right(Convert(Varchar(10),HolidayDate,120),5) from AttendanceHoliday) THEN '2 - 公众假上班' ELSE '0 - 平时上班' END AS 'OnDutyType'
,StartTime
,EndTime
,AOnDuty
,AOffDuty
,Allday
,SumTotal
,Error
From
(
Select
Number
,ChineseName
,d.DepartmentCode
,DepartmentName
,p.ShiftId
,p.ShiftName
,Degree
,AskForLeaveType
,WorkOnDate
,StartTime
,EndTime
,Sumtotal
,NULL AS 'AOnDuty'
,NULL AS 'AOffDuty'
,AllDay
,1 AS 'Error'
From
AttendanceAskForLeave a
LEFT JOIN Employees e ON a.Employeeid=e.Employeeid
LEFT JOIN Departments d ON e.DepartmentCode=d.DepartmentCode
LEFT JOIN AttendanceSpell p ON a.Employeeid=p.Employeeid AND a.WorkOnDate=p.OnDutyDate
LEFT JOIN AttendanceShifts s ON p.ShiftId=s.ShiftId
Where
Left(IncumbencyStatus,1)<>1
Group by
Number,ChineseName,d.DepartmentCode,DepartmentName,p.ShiftId,p.ShiftName,Degree,AskForLeaveType,AllDay,WorkOnDate,StartTime,EndTime,Sumtotal
having
Count(1)>1
) tb3
) tb4
对你的代码格式化后发现,还缺少一些语句,你是否粘贴完整了?
读完SQL语句后发现,其实是一个考勤核算的查询语句,是两个大的SQL查询Union ALL的结果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |