SELECT
date,
classification,
validation,
CASE WHEN arrivalTime BETWEEN '00:00' AND '00:10' THEN energy
ELSE 0 END AS '00:00',
CASE WHEN arrivalTime BETWEEN '00:10' AND '00:20' THEN energy
ELSE 0 END AS '00:10',
CASE WHEN arrivalTime BETWEEN '00:20' AND '00:30' THEN energy
ELSE 0 END AS '00:20',
・・・
FROM Energy_date
1BEGIN23SET QUOTED_IDENTIFIER OFF45DECLARE@hourint6DECLARE@minint7DECLARE@sql1varchar(max)=''8DECLARE@sql2varchar(max)=''9DECLARE@sql3varchar(max)=''10DECLARE@sql4varchar(max)=''11DECLARE@sql5varchar(max)=''12DECLARE@sql6varchar(max)=''13DECLARE@startchar(5)14DECLARE@endchar(5)1516SET@hour=017SET@sql1="SELECT"18+' date'19+' ,classification'20+' ,validation'21WHILE@hour<2422BEGIN23SET@min=024WHILE@min<6025BEGIN26SET@start=RIGHT("00"+ CAST(@hourASVARCHAR),2)+":"27+RIGHT("00"+ CAST(@minASVARCHAR),2)28SET@end=RIGHT("00"+ CAST(@hourASVARCHAR),2)+":"29+RIGHT("00"+ CAST(@min+9ASVARCHAR),2)30-- 時間の重なっている部分を両方に出すならこちら31-- SET @end = RIGHT("00" + CAST(@hour AS VARCHAR), 2) + ":"32-- + RIGHT("00" + CAST((@min + 10) % 60 AS VARCHAR), 2)33IF@hour<634BEGIN35SET@sql2=@sql236+" ,CASE WHEN arrivalTime"37+" BETWEEN '"+@start+"' AND '"+@end+"'"38+" THEN energy"39+" ELSE 0"40+" END AS '"+@start+"'"41END42ELSEIF@hour<1243BEGIN44SET@sql3=@sql345+" ,CASE WHEN arrivalTime"46+" BETWEEN '"+@start+"' AND '"+@end+"'"47+" THEN energy"48+" ELSE 0"49+" END AS '"+@start+"'"50END51ELSEIF@hour<1852BEGIN53SET@sql4=@sql454+" ,CASE WHEN arrivalTime"55+" BETWEEN '"+@start+"' AND '"+@end+"'"56+" THEN energy"57+" ELSE 0"58+" END AS '"+@start+"'"59END60ELSE61BEGIN62SET@sql5=@sql563+" ,CASE WHEN arrivalTime"64+" BETWEEN '"+@start+"' AND '"+@end+"'"65+" THEN energy"66+" ELSE 0"67+" END AS '"+@start+"'"68END69SET@min=@min+1070END71SET@hour=@hour+172END7374SET@sql6=" FROM Energy_date"7576EXEC(@sql1+@sql2+@sql3+@sql4+@sql5+@sql6)77END78