  | 
              
	              
	                
  
    
	 
        มีเรื่องขอคำแนะนำเกี่ยวกับ Stored เพื่อ Return ค่า เพื่อออกรายงาน     | 
   
  
    |   | 
   
 
 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 แบบที่ 1 ?? 
ถ้าเอา 12 * 4 มันจะได้ 48 ช่อง 
หรือก็คือ 1-48 
เวลาแบ่งเป็นเดือนก็ให้เป็น 
1-4 คือ เดือนที่ 1 ดูจากเดือนน่าจะเป็นปีงบประมาณ 
5-8 = 2 
9-12 = 3 
ไปเรื่อย ๆจนครับ 12 เดือน 
ซึ่งการ เริ่มปี กับสิ้นสุดปีก็ให้เป็นอีกแบบไปเลยน่าจะใช้ตัวเดิมที่มีอยู่ในฐานข้อมูล 
 
ส่วนการบันทึกข้อมูลก็สร้าง ตารางขึ้นมาอีกตารางซึ่งใช้ในการเก็บตัวเลข 1-48 
แล้วก็เอาคีย์หลักมาบันทึกไว้ในตารางใหม่ก็จะได้เป็นรูปแบบประมาณนี้ครับ 
 
โดยตารางหลักชื่อตาราง A 
------------------------------------- 
ตารางที่เราสร้างใหม่ชื่อตาราง B 
id 	id_name_water 	number_set(ตัวเลข 1-48) 
1   	1                     	1 
2    	1                     	2 
3    	1                      	3 
4     	1 		4 
5 	1 		5 
6 	1 		6 
7  	2 		7 
8	2		8 
9	2		9 
10	2		10 
---------------------------------------------- 
ส่วนเวลาการออกข้อมูลมาก็จะเป็น 
ให้แสดง number_set โดยมีเงื่อนไขว่า ให้ id หลัก ของตาราง A ตรงกับ id_name_water ของตาราง B 
ก็จะได้ข้อมูลเป็น 
id_name_water = 1 มีข้อมูล number_set 1,2,3,4,5,6 
แล้วก็เอาตัวเลขไปหาอีกทีก็จะได้ผลลัพธ์เป็น ผลลัพธ์ของข้อมูลที่ 1 คือเริ่มจากเดือนแรก กับอีก 2 อาทิตย์แรกของเดือนที่ 2 
id_name_water = 2 มีข้อมูล number_set 7,8,9,10 
ผลลัพธ์ของข้อมูลที่ 2 คือ เริ่มจาก 2 อาทิตย์หลังของเดือนที่ 2  กับอีก 2 อาทิตย์ของเดือนที่ 3 
หรือจะเอาไปทำเป็นแบบตารางก็ว่ากันไป 
***************** 
แบบที่ 2 คือสร้างตารางไปเลย 
มี 48 อาทิตย์ให้เลือก ก็ทำตารางออกมา 48 ตาราง 
รูปแบบตารางก็จะเป็น 
id	id_name_water  week1 	week2 	week3 	week4 	week5 	week6 	week7 	...  week48 
1  	1 		0	0	0	0	0	0	0	...  0 
---- 
กรณีที่ต้องการจะเลือกอาทิตย์ก็ให้คลิกที่ week นั้นแล้วให้อัพเดท week นั้นเป็น 1  
ถ้าทำแบบนี้ต้อง query ออกมาก็จะได้ตารางคล้ายๆ อันที่มีสีเหลืองครับ 
กรณีที่แบบที่ 2 อาจจะเพิ่มตารางเดือนเข้ามาก็ได้ 
มันก็จะสั่นลงเป็น 
id 	id_name_water	 mount 	week1 	week2  	week3 	week4 
1 	1 		1            0  	0   	0 	0 
2 	1 		2 	0 	0	0	0 
3	1 		3	0	0	0	0 
 
แต่เวลาที่ดึงออกมาแสดงต้องดึงมันเรียงจากเดือนที่เรากำหนด 
 
************** 
ตัวเลขที่แสดงไม่ตรงตารางต้องมีจิตนาการนิดนึงนะครับ 
ประมาณนี้ครับ  
รอพี่ๆ เขามาตอบอีกที                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2019-06-26 10:56:54              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2019-06-26 10:52:10 | 
                        By :
                            nobetaking | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ตอนนี้ผมทำแบบนี้ ก็จะได้แค่จุดเริ่ม กับจุดสิ้นสุด มีวิธีไหนบ้างครับ ที่ช่วงระหว่างทั้งคู่ ให้ return เป็น 1 ให้ครบช่อง 
 
เช่น เดือน 2 วีค 1 ไปจบที่ เดือน 4 วีค 3 จะต้องได้ค่า 
 
0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0 
 
เป็นต้น 
 
  
 
Code (SQL) 
SELECT HS.ST_YEAR,
		   HS.ST_MONTH,
		   HS.ST_WEEK,
		   HS.END_YEAR,
		   HS.END_MONTH,
		   HS.END_WEEK,
		   
		   		   (case when HS.ST_MONTH = 10 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_101,
		   (case when HS.ST_MONTH = 10 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_102,
		   (case when HS.ST_MONTH = 10 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_103,
		   (case when HS.ST_MONTH = 10 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_104,
		   		   (case when HS.ST_MONTH = 11 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_111,
		   (case when HS.ST_MONTH = 11 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_112,
		   (case when HS.ST_MONTH = 11 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_113,
		   (case when HS.ST_MONTH = 11 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_114,
		   		   (case when HS.ST_MONTH = 12 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_121,
		   (case when HS.ST_MONTH = 12 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_122,
		   (case when HS.ST_MONTH = 12 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_123,
		   (case when HS.ST_MONTH = 12 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_124,
		   		   (case when HS.ST_MONTH = 1 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_011,
		   (case when HS.ST_MONTH = 1 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_012,
		   (case when HS.ST_MONTH = 1 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_013,
		   (case when HS.ST_MONTH = 1 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_014,
		   		   (case when HS.ST_MONTH = 2 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_021,
		   (case when HS.ST_MONTH = 2 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_022,
		   (case when HS.ST_MONTH = 2 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_023,
		   (case when HS.ST_MONTH = 2 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_024,
		   		   (case when HS.ST_MONTH = 3 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_031,
		   (case when HS.ST_MONTH = 3 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_032,
		   (case when HS.ST_MONTH = 3 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_033,
		   (case when HS.ST_MONTH = 3 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_034,
		   		   (case when HS.ST_MONTH = 4 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_041,
		   (case when HS.ST_MONTH = 4 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_042,
		   (case when HS.ST_MONTH = 4 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_043,
		   (case when HS.ST_MONTH = 4 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_044,
		   		   (case when HS.ST_MONTH = 5 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_051,
		   (case when HS.ST_MONTH = 5 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_052,
		   (case when HS.ST_MONTH = 5 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_053,
		   (case when HS.ST_MONTH = 5 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_054,
		   		   (case when HS.ST_MONTH = 6 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_061,
		   (case when HS.ST_MONTH = 6 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_062,
		   (case when HS.ST_MONTH = 6 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_063,
		   (case when HS.ST_MONTH = 6 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_064,
		   		   (case when HS.ST_MONTH = 7 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_071,
		   (case when HS.ST_MONTH = 7 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_072,
		   (case when HS.ST_MONTH = 7 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_073,
		   (case when HS.ST_MONTH = 7 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_074,
		   		   (case when HS.ST_MONTH = 8 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_081,
		   (case when HS.ST_MONTH = 8 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_082,
		   (case when HS.ST_MONTH = 8 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_083,
		   (case when HS.ST_MONTH = 8 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_084,
		   		   (case when HS.ST_MONTH = 9 AND HS.ST_WEEK = 1 then 1 else 0 end) ST_091,
		   (case when HS.ST_MONTH = 9 AND HS.ST_WEEK = 2 then 1 else 0 end) ST_092,
		   (case when HS.ST_MONTH = 9 AND HS.ST_WEEK = 3 then 1 else 0 end) ST_093,
		   (case when HS.ST_MONTH = 9 AND HS.ST_WEEK = 4 then 1 else 0 end) ST_094,
		   --------------
		   		   (case when HS.END_MONTH = 10 AND HS.END_WEEK = 1 then 1 else 0 end) END_101,
		   (case when HS.END_MONTH = 10 AND HS.END_WEEK = 2 then 1 else 0 end) END_102,
		   (case when HS.END_MONTH = 10 AND HS.END_WEEK = 3 then 1 else 0 end) END_103,
		   (case when HS.END_MONTH = 10 AND HS.END_WEEK = 4 then 1 else 0 end) END_104,
		   		   (case when HS.END_MONTH = 11 AND HS.END_WEEK = 1 then 1 else 0 end) END_111,
		   (case when HS.END_MONTH = 11 AND HS.END_WEEK = 2 then 1 else 0 end) END_112,
		   (case when HS.END_MONTH = 11 AND HS.END_WEEK = 3 then 1 else 0 end) END_113,
		   (case when HS.END_MONTH = 11 AND HS.END_WEEK = 4 then 1 else 0 end) END_114,
		   		   (case when HS.END_MONTH = 12 AND HS.END_WEEK = 1 then 1 else 0 end) END_121,
		   (case when HS.END_MONTH = 12 AND HS.END_WEEK = 2 then 1 else 0 end) END_122,
		   (case when HS.END_MONTH = 12 AND HS.END_WEEK = 3 then 1 else 0 end) END_123,
		   (case when HS.END_MONTH = 12 AND HS.END_WEEK = 4 then 1 else 0 end) END_124,
		   		   (case when HS.END_MONTH = 1 AND HS.END_WEEK = 1 then 1 else 0 end) END_011,
		   (case when HS.END_MONTH = 1 AND HS.END_WEEK = 2 then 1 else 0 end) END_012,
		   (case when HS.END_MONTH = 1 AND HS.END_WEEK = 3 then 1 else 0 end) END_013,
		   (case when HS.END_MONTH = 1 AND HS.END_WEEK = 4 then 1 else 0 end) END_014,
		   		   (case when HS.END_MONTH = 2 AND HS.END_WEEK = 1 then 1 else 0 end) END_021,
		   (case when HS.END_MONTH = 2 AND HS.END_WEEK = 2 then 1 else 0 end) END_022,
		   (case when HS.END_MONTH = 2 AND HS.END_WEEK = 3 then 1 else 0 end) END_023,
		   (case when HS.END_MONTH = 2 AND HS.END_WEEK = 4 then 1 else 0 end) END_024,
		   		   (case when HS.END_MONTH = 3 AND HS.END_WEEK = 1 then 1 else 0 end) END_031,
		   (case when HS.END_MONTH = 3 AND HS.END_WEEK = 2 then 1 else 0 end) END_032,
		   (case when HS.END_MONTH = 3 AND HS.END_WEEK = 3 then 1 else 0 end) END_033,
		   (case when HS.END_MONTH = 3 AND HS.END_WEEK = 4 then 1 else 0 end) END_034,
		   		   (case when HS.END_MONTH = 4 AND HS.END_WEEK = 1 then 1 else 0 end) END_041,
		   (case when HS.END_MONTH = 4 AND HS.END_WEEK = 2 then 1 else 0 end) END_042,
		   (case when HS.END_MONTH = 4 AND HS.END_WEEK = 3 then 1 else 0 end) END_043,
		   (case when HS.END_MONTH = 4 AND HS.END_WEEK = 4 then 1 else 0 end) END_044,
		   		   (case when HS.END_MONTH = 5 AND HS.END_WEEK = 1 then 1 else 0 end) END_051,
		   (case when HS.END_MONTH = 5 AND HS.END_WEEK = 2 then 1 else 0 end) END_052,
		   (case when HS.END_MONTH = 5 AND HS.END_WEEK = 3 then 1 else 0 end) END_053,
		   (case when HS.END_MONTH = 5 AND HS.END_WEEK = 4 then 1 else 0 end) END_054,
		   		   (case when HS.END_MONTH = 6 AND HS.END_WEEK = 1 then 1 else 0 end) END_061,
		   (case when HS.END_MONTH = 6 AND HS.END_WEEK = 2 then 1 else 0 end) END_062,
		   (case when HS.END_MONTH = 6 AND HS.END_WEEK = 3 then 1 else 0 end) END_063,
		   (case when HS.END_MONTH = 6 AND HS.END_WEEK = 4 then 1 else 0 end) END_064,
		   		   (case when HS.END_MONTH = 7 AND HS.END_WEEK = 1 then 1 else 0 end) END_071,
		   (case when HS.END_MONTH = 7 AND HS.END_WEEK = 2 then 1 else 0 end) END_072,
		   (case when HS.END_MONTH = 7 AND HS.END_WEEK = 3 then 1 else 0 end) END_073,
		   (case when HS.END_MONTH = 7 AND HS.END_WEEK = 4 then 1 else 0 end) END_074,
		   		   (case when HS.END_MONTH = 8 AND HS.END_WEEK = 1 then 1 else 0 end) END_081,
		   (case when HS.END_MONTH = 8 AND HS.END_WEEK = 2 then 1 else 0 end) END_082,
		   (case when HS.END_MONTH = 8 AND HS.END_WEEK = 3 then 1 else 0 end) END_083,
		   (case when HS.END_MONTH = 8 AND HS.END_WEEK = 4 then 1 else 0 end) END_084,
		   		   (case when HS.END_MONTH = 9 AND HS.END_WEEK = 1 then 1 else 0 end) END_091,
		   (case when HS.END_MONTH = 9 AND HS.END_WEEK = 2 then 1 else 0 end) END_092,
		   (case when HS.END_MONTH = 9 AND HS.END_WEEK = 3 then 1 else 0 end) END_093,
		   (case when HS.END_MONTH = 9 AND HS.END_WEEK = 4 then 1 else 0 end) END_094
		   FROM HYC_SURVEY HS
                         
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2019-06-28 15:41:17 2019-06-28 15:42:24 2019-06-28 15:43:12              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2019-06-28 15:40:55 | 
                        By :
                            por-jr | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ไม่รุ้ได้ไหม ไกด์ได้ประมาณนี้ครับ ไป inner join, กะ pivot  ต่อเอาเองนะครับ 
 
Code (SQL) 
CREATE TABLE [dbo].[tbl](
	[st_year] [int] NULL,
	[st_month] [int] NULL,
	[st_week] [int] NULL,
	[end_year] [int] NULL,
	[end_month] [int] NULL,
	[end_week] [int] NULL
) ON [PRIMARY]
GO
insert into [tbl] values (2019	,1,	3	,2019,	3,	3)
select * 
from tbl;
with cte as
(
	select iif(st_month>1  ,st_month*4-4 + st_week,st_week) as ws , iif(end_month>1,end_month*4-4 + end_week,end_week) wn, 1 as cnt, 0 as run
	from tbl
	union all
	select ws,wn,cnt+1 as cnt,case when cnt+1 between ws and wn then 1 else 0 end run from cte
	where cnt <48
)
select cnt, run as stampDate from cte
                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2019-11-22 16:57:54 | 
                        By :
                            WWWWIN | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                   ตอบความคิดเห็นที่ : 5 เขียนโดย : WWWWIN เมื่อวันที่ 2019-11-22 16:57:54 
รายละเอียดของการตอบ ::
   แก้บักย้อนปี ครับ
 Code (SQL)
with cte as
(
	select iif(st_month>1  ,st_month*4-4 + st_week,st_week) as ws , iif(end_month>1,end_month*4-4 + end_week,end_week) wn, 1 as cnt,
		   iif( iif(st_month>1  ,st_month*4-4 + st_week,st_week) <= iif(end_month>1,end_month*4-4 + end_week,end_week) ,0 , 1) as run
	 --0 as run
	from tbl
	union all
	select ws,wn,cnt+1 as cnt,
	iif(ws<= wn,
			case when cnt+1 between ws and wn then 1 else 0 end,
			case when cnt between wn and ws-2 then 0 else 1 end) as run
	from cte
	where cnt <48
)
select ws,wn,cnt, run as stampDate from cte
 
                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2019-11-22 17:23:15 | 
                        By :
                            WWWWIN | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |