SQL case nesting

Source: Internet
Author: User

/*************************************** **************************************** ******************************/
-- Calculate the overtime hours for non-fixed employees
/*************************************** **************************************** ******************************/
Create proc recountworkhourfordf
@ Begindate varchar (10 ),
@ Enddate varchar (10)
As
------------- Delete the overtime record on the Operation Date ------------------------------------------------
Delete from workhourdetail
Where (carddate between @ begindate and @ enddate)
And (left (CDAY, 1) = 'D' or left (CDAY, 1) = 'F ')

Insert into workhourdetail (ecode, carddate, CDAY, factworkhour)
Select -- A. ecode, A. ename, A. card1, A. card2, A. addtime, B. btime, B. etime, CDAY, A. carddate,
A. ecode, A. carddate, B. CDAY,
Case when substring (CDAY, 2, 1) = '*' then
---------------------------- Call the work card before normal work --------------------------------------
Case when (card1 <= B. btime and card1> '12: 00') then
Case when (card2 <B. setime and card2> '12: 00') then
Case when (datediff (MI, B. btime, card2) % 60)> = 30 then
Cast (datediff (MI, B. btime, card2)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, B. btime, card2)/60) as varchar (2) + '. 0'
End
When (card2> = B. setime) and (card2 <'24: 00') and (left (CDAY, 1) = 'F') then
'4. 0'
When (card2> = B. xbtime) and (card2 <B. etime) then
Case when (datediff (MI, B. xbtime, card2) % 60)> = 30 then
Cast (datediff (MI, B. xbtime, card2) + 240)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, B. xbtime, card2) + 240)/60) as varchar (2) + '. 0'
End
When (card2> = B. etime) and (card2 <= B. bworktime) then
'8. 0'
When (card2> B. bworktime and card2 <'12: 00') then
Case when (datediff (MI, B. bworktime, card2) % 60)> = 30 then
Cast (datediff (MI, B. bworktime, card2) + 480)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, B. bworktime, card2) + 480)/60) as varchar (2) + '. 0'
End
Else
'0. 0'
End
--------------------------- Call the work card before the first half of the day ------------------------------------
When (card1> = B. btime) and (card1 <B. setime) then
Case when (card2 <B. setime and card2> '12: 00') then
Case when (datediff (MI, begincard, card2) % 60)> = 30 then
Cast (datediff (MI, begincard, card2)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, begincard, card2)/60) as varchar (2) + '. 0'
End
When (card2> = B. setime) and (card2 <'24: 00') then
Case when (datediff (MI, begincard, B. setime) % 60)> = 30 then
Cast (datediff (MI, begincard, B. setime)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, begincard, B. setime)/60) as varchar (2) + '. 0'
End
When (card2> B. xbtime) and (card2 <B. etime) and (card2 <'12: 00') then
Case when (datediff (MI, begincard, B. setime) + datediff (MI, B. xbtime, card2) % 60)> = 30 then
Cast (datediff (MI, begincard, B. setime) + datediff (MI, B. xbtime, card2)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, begincard, B. setime) + datediff (MI, B. xbtime, card2)/60) as varchar (2) + '. 0'
End
When (card2> = B. etime) and (card2 <= B. bworktime) then
Case when (datediff (MI, begincard, B. setime) + 240) % 60)> = 30 then
Cast (datediff (MI, begincard, B. setime) + 240)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, begincard, B. setime) + 240)/60) as varchar (2) + '. 0'
End
When (card2> B. bworktime and card2 <'12: 00') then
Case when (datediff (MI, begincard, B. setime) + 240 + datediff (MI, B. bworktime, card2) % 60)> = 30 then
Cast (datediff (MI, begincard, B. setime) + 240 + datediff (MI, B. bworktime, card2)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, begincard, B. setime) + 240 + datediff (MI, B. bworktime, card2)/60) as varchar (2) + '. 0'
End
Else
'0. 0'
End
--------------------------- Call the work card before the next half day before work ------------------------------------
When (card1> = B. setime and card1> '12: 00') then
Case when (card2> B. xbtime) and (card2 <B. etime) then
Case when (datediff (MI, B. xbtime, card2) % 60)> = 30 then
Cast (datediff (MI, B. xbtime, card2)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, B. xbtime, card2)/60) as varchar (2) + '. 0'
End
When (card2> = B. etime) and (card2 <= B. bworktime) then
'4. 0'
When (card2> B. bworktime and card2 <'12: 00') then
Case when (240 + datediff (MI, B. bworktime, card2) % 60)> = 30 then
Cast (240 + datediff (MI, B. bworktime, card2)/60) as varchar (2) + '. 5'
Else
Cast (240 + datediff (MI, B. bworktime, card2)/60) as varchar (2) + '. 0'
End
Else
'0. 0'
End
--------------------------- Get off work card before the next half day ------------------------------------
When (card1> = B. xbtime and card1 <B. etime and card1 <'12: 00') then
Case when (card2 <B. etime) then
Case when (datediff (MI, begincard, card2) % 60)> = 30 then
Cast (datediff (MI, begincard, card2)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, begincard, card2)/60) as varchar (2) + '. 0'
End
When (card2> = B. etime) and (card2 <= B. bworktime) then
Case when (datediff (MI, begincard, B. etime) % 60)> = 30 then
Cast (datediff (MI, begincard, B. etime)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, begincard, B. etime)/60) as varchar (2) + '. 0'
End
When (card2> B. bworktime and card2 <'12: 00') then
Case when (datediff (MI, begincard, card2)-datediff (MI, B. etime, B. bworktime) % 60)> = 30 then
Cast (datediff (MI, begincard, card2)-datediff (MI, B. etime, B. bworktime)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, begincard, card2)-datediff (MI, B. etime, B. bworktime)/60) as varchar (2) + '. 0'
End
Else
'0. 0'
End
When (card1> = B. etime and card1 <= B. bworktime) then
Case when (card2> B. bworktime) then
Case when (datediff (MI, B. bworktime, card2) % 60)> = 30 then
Cast (datediff (MI, B. bworktime, card2)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, B. bworktime, card2)/60) as varchar (2) + '. 0'
End
Else
'0. 0'
End
When (card1> B. bworktime) then
Case when (card2> card1 and card2> B. bworktime) then
Case when (datediff (MI, begincard, card2) % 60)> = 30 then
Cast (datediff (MI, begincard, card2)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, begincard, card2)/60) as varchar (2) + '. 0'
End
Else
'0. 0'
End

Else
'0. 0'
End
Else
Case when (datediff (MI, B. bworktime, card2)> = 30) and card2 <= '12: 00') then
Case when (datediff (MI, B. bworktime, card2) % 60)> = 30 then
Cast (datediff (MI, B. bworktime, card2)/60) as varchar (2) + '. 5'
Else
Cast (datediff (MI, B. bworktime, card2)/60) as varchar (2) + '. 0'
End
Else
'0. 0'
End
End factworkhour
From cardrecord A, empclass B
Where a. ecode = B. workid and A. carddate = B. prodate -- and A. ecode in (select ecode from emploees where emoneyfix = 0)
And (A. carddate between @ begindate and @ enddate)
And (left (CDAY, 1) = 'D' or left (CDAY, 1) = 'F ')
-- And A. carddate in ('2017-12-01 ', '2017-12-02', '2017-12-06 ', '2017-12-08', '2017-12-09 ')

Go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.