當前位置:首頁 » 編程語言 » 大神解說sql
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

大神解說sql

發布時間: 2023-06-16 12:02:31

A. 剛學sql資料庫就怎麼那麼復雜的例子看不懂求大神解釋

請參閱下面的文章,主要是講述如何使用命令創建資料庫,

在sql命令有指定文件路徑 文件組 大小

使用sql腳本創建資料庫

如果有疑問,請及時溝通

請採納!

B. 一段很復雜很復雜的SQL語句。。求大神解釋。。。

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
WhenDegree=1ANDStartTime<=AOnDutyANDEndTime<=AOnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=1ANDStartTime>=AOffDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=1ANDStartTime>=WorkOnDate+''+'12:30'ANDEndTime<=WorkOnDate+''+'13:30'ANDCharindex('常白班',ShiftName)>0ANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=2ANDStartTime<=AOnDutyANDEndTime<=AOnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=2ANDStartTime>=AOffDutyANDEndTime<=BOnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=2ANDStartTime>=BOffDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=3ANDStartTime<=AOnDutyANDEndTime<=AOnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=3ANDStartTime>=AOffDutyANDEndTime<=BOnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=3ANDStartTime>=BOffDutyANDEndTime<=COnDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenDegree=3ANDStartTime>=COffDutyANDShiftId>0ANDLeft(AskForLeaveType,1)='0'Then0
WhenShiftId=0ANDLeft(AskForLeaveType,1)='0'Then0
WhenShiftId>0ANDLeft(AskForLeaveType,1)<>'0'ANDLeft(OnDutyType,1)=0Then0
WhenShiftId>0ANDLeft(AskForLeaveType,1)<>'0'ANDLeft(OnDutyType,1)=2ANDAskForLeaveType='出差假'Then0
WhenShiftId>0ANDLeft(AskForLeaveType,1)<>'0'ANDLeft(OnDutyType,1)=2ANDAskForLeaveType='產假'Then0
WhenShiftId=0ANDLeft(AskForLeaveType,1)<>'0'ANDConvert(varchar(10),StartTime,108)='00:00:00'ANDConvert(varchar(10),EndTime,108)='00:30:00'Then0
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
,CaseWhenRight(Convert(Varchar(10),WorkOnDate,120),5)IN(selectRight(Convert(Varchar(10),HolidayDate,120),5)FromAttendanceHoliday)THEN'2-公眾假上班'ELSE'0-平時上班'ENDAS'OnDutyType'
,CaseWhenLeft(FristBrushDate,1)='1'ANDConvert(varchar(10),AOffDuty,120)>Convert(varchar(10),AOnDuty,120)ThenCAST(Convert(Varchar(11),Dateadd(dd,-1,OnDutyDate),120)+Convert(Varchar(5),AOnDuty,108)ASDateTime)ELSECAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),AOnDuty,108)ASDateTime)EndAS'AOnDuty'
,CaseWhenLeft(FristBrushDate,1)='0'ANDConvert(varchar(10),AOnDuty,120)<Convert(varchar(10),AOffDuty,120)ThenCAST(Convert(Varchar(11),Dateadd(dd,1,OnDutyDate),120)+Convert(Varchar(5),AOffDuty,108)ASDateTime)ELSECAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),AOffDuty,108)ASDateTime)EndAS'AOffDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),BOnDuty,108)ASDateTime)AS'BOnDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),BOffDuty,108)ASDateTime)AS'BOffDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),COnDuty,108)ASDateTime)AS'COnDuty'
,CAST(Convert(Varchar(11),OnDutyDate,120)+Convert(Varchar(5),COffDuty,108)ASDateTime)AS'COffDuty'
From
.DepartmentCode=d.DepartmentCode
.Employeeid=a.Employeeid
INNERJOINAttendanceSpellpona.Employeeid=p.Employeeidanda.WorkOndate=p.OnDutyDate
LEFTJOINAttendanceShiftssonp.Shiftid=s.Shiftid
)tb1
)tb2WhereError=1

UNIONALL

Select
Number
,ChineseName
,DepartmentCode
,DepartmentName
,ShiftId
,ShiftName
,Degree
,AskForLeaveType
,WorkOnDate
,CaseWhenRight(Convert(Varchar(10),WorkOnDate,120),5)IN(selectRight(Convert(Varchar(10),HolidayDate,120),5)fromAttendanceHoliday)THEN'2-公眾假上班'ELSE'0-平時上班'ENDAS'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
,NULLAS'AOnDuty'
,NULLAS'AOffDuty'
,AllDay
,1AS'Error'
From
AttendanceAskForLeavea
LEFTJOINEmployeeseONa.Employeeid=e.Employeeid
LEFTJOINDepartmentsdONe.DepartmentCode=d.DepartmentCode
LEFTJOINAttendanceSpellpONa.Employeeid=p.EmployeeidANDa.WorkOnDate=p.OnDutyDate
LEFTJOINAttendanceShiftssONp.ShiftId=s.ShiftId
Where
Left(IncumbencyStatus,1)<>1
Groupby
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的結果