Kk

Kk

  • NA
  • 9
  • 369

Create a loop for the Calender Form data

Oct 24 2018 11:24 PM

I have created a query using view which give me results for the Tags as '1' if the work is done as per the dates on that particular day or '0' if it didn't. I have created this for October only but I want it for the whole year & also for the next year.

I know I will have to use the loop or the query will be long enough & will not work efficiently & that will turn the worse conditions.
 
So I just want to ask you that is there anyone which can reduce the query of mine & give me the desired output as required.
 
The code is as below :

  1. Use CNSData  
  2. select  [tagno],  
  3. max(case when(datepart(day,DueDate)) ='1' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  4.                                        when(datepart(day,JobDoneDate)) ='1' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-01],  
  5. max(case when(datepart(day,DueDate)) ='2' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  6.                                        when(datepart(day,JobDoneDate)) ='2' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-02],  
  7. max(case when(datepart(day,DueDate)) ='3' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  8.                                        when(datepart(day,JobDoneDate)) ='3' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-03],  
  9. max(case when(datepart(day,DueDate)) ='4' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  10.                                        when(datepart(day,JobDoneDate)) ='4' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-04],  
  11. max(case when(datepart(day,DueDate)) ='5' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  12.                                        when(datepart(day,JobDoneDate)) ='5' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-05],  
  13. max(case when(datepart(day,DueDate)) ='6' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  14.                                        when(datepart(day,JobDoneDate)) ='6' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-06],  
  15. max(case when(datepart(day,DueDate)) ='7' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  16.                                        when(datepart(day,JobDoneDate)) ='7' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-07],  
  17. max(case when(datepart(day,DueDate)) ='8' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  18.                                        when(datepart(day,JobDoneDate)) ='8' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-08],  
  19. max(case when(datepart(day,DueDate)) ='9' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  20.                                        when(datepart(day,JobDoneDate)) ='9' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-09],  
  21. max(case when(datepart(day,DueDate)) ='10' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  22.                                        when(datepart(day,JobDoneDate)) ='10' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-10],  
  23. max(case when(datepart(day,DueDate)) ='11' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  24.                                        when(datepart(day,JobDoneDate)) ='11' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-11],  
  25. max(case when(datepart(day,DueDate)) ='12' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  26.                                        when(datepart(day,JobDoneDate)) ='12' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-12],  
  27. max(case when(datepart(day,DueDate)) ='13' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  28.                                        when(datepart(day,JobDoneDate)) ='13' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-13],  
  29. max(case when(datepart(day,DueDate)) ='14' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  30.                                        when(datepart(day,JobDoneDate)) ='14' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-14],  
  31. max(case when(datepart(day,DueDate)) ='15' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  32.                                        when(datepart(day,JobDoneDate)) ='15' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-15],  
  33. max(case when(datepart(day,DueDate)) ='16' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  34.                                        when(datepart(day,JobDoneDate)) ='16' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-16],  
  35. max(case when(datepart(day,DueDate)) ='17' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  36.                                        when(datepart(day,JobDoneDate)) ='17' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-17],  
  37. max(case when(datepart(day,DueDate)) ='18' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  38.                                        when(datepart(day,JobDoneDate)) ='18' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-18],  
  39. max(case when(datepart(day,DueDate)) ='19' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  40.                                        when(datepart(day,JobDoneDate)) ='19' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-19],  
  41. max(case when(datepart(day,DueDate)) ='20' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  42.                                        when(datepart(day,JobDoneDate)) ='20' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-20],  
  43. max(case when(datepart(day,DueDate)) ='21' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  44.                                        when(datepart(day,JobDoneDate)) ='21' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-21],  
  45. max(case when(datepart(day,DueDate)) ='22' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  46.                                        when(datepart(day,JobDoneDate)) ='22' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-22],  
  47. max(case when(datepart(day,DueDate)) ='23' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  48.                                        when(datepart(day,JobDoneDate)) ='23' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-23],  
  49. max(case when(datepart(day,DueDate)) ='24' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  50.                                        when(datepart(day,JobDoneDate)) ='24' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-24],  
  51. max(case when(datepart(day,DueDate)) ='25' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  52.                                        when(datepart(day,JobDoneDate)) ='25' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-25],  
  53. max(case when(datepart(day,DueDate)) ='26' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  54.                                        when(datepart(day,JobDoneDate)) ='26' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-26],  
  55. max(case when(datepart(day,DueDate)) ='27' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  56.                                        when(datepart(day,JobDoneDate)) ='27' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-27],  
  57. max(case when(datepart(day,DueDate)) ='28' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  58.                                        when(datepart(day,JobDoneDate)) ='28' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-28],  
  59. max(case when(datepart(day,DueDate)) ='29' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  60.                                        when(datepart(day,JobDoneDate)) ='29' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-29],  
  61. max(case when(datepart(day,DueDate)) ='30' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  62.                                        when(datepart(day,JobDoneDate)) ='30' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-30],  
  63. max(case when(datepart(day,DueDate)) ='31' and (datepart(month,DueDate))='10' and (datepart(year,DueDate)) = '2018' then 1   
  64.                                        when(datepart(day,JobDoneDate)) ='31' and (datepart(month,JobDoneDate))='10' and (datepart(year,JobDoneDate)) = '2018'  then 1 else 0 end)[2018-10-31]  
  65. from CNSData.dbo.vw_Schedule group by tagno  
  66. Go    
 

Answers (5)