如何将一周中的日子分组,比如日历?

时间:2017-06-07 04:06:57

标签: sql sql-server

我有一个包含计划列的表,我想在Day_Name列上进行调整,以便日期显示为列,如星期一,星期二等。 这是我的疑问:

SELECT 
     Month_Name,Day_Name, Year, Week_Number, Day_Number
    , isnull((Select (CONVERT(varchar(5), empschedfrom, 108) + '-' + convert(varchar(5), empschedto, 108)) from empdayscheds where empno = '0030' and empday_no = DayNumber_of_Month and empday_month = Month_Number and empday_year = Year), 'OFF') as emp_sched  
    from dtRule where DATE between '05/28/2017' and '07/08/2017'

输出:

   Month_Name | Day_Name | Year | Week_Number |Day_number| emp_sched 
   May        |Sunday    | 2017 | 4           | 28       | 11:45-20:45
   May        | Tuesday  | 2017 | 5           | 29       | 14:15-23:15
   May        | Wednesday| 2017 | 5           | 30       | 13:45-22:45
   June       | Thursday | 2017 | 1           |  1       |10:00-19:00
   June       | Friday   | 2017 | 1           |  2       |10:00-19:00
   June       | Saturday | 2017 | 1           |  3       |10:00-19:00

我希望它能像这样出现:

Week_Number | Month_Name | Year | Monday | Tuesday | Wednesday | Thursday  | Friday    | Saturday
1           | June       | 2017 |        |         |           |10:00-19:00|10:00-19:00|10:00-19:00

2 个答案:

答案 0 :(得分:0)

 drop table if exists dbo.Schedule;

create table dbo.Schedule (
Month_Name varchar(100)
, Day_Name varchar(100)
, Year int
, Week_Number int
, Day_Number int
, emp_sched varchar(100)
);

insert into dbo.Schedule (Month_Name, Day_Name, Year, Week_Number, Day_Number, emp_sched)
values ('May', 'Sunday', 2017, 4           ,  28       ,  '11:45-20:45')
 , ('May'        ,  'Tuesday'  ,  2017 ,  5           ,  29       ,  '14:15-23:15')
 , ('May'        ,  'Wednesday',  2017 ,  5           ,  30       ,  '13:45-22:45')
 , ('June'       ,  'Thursday' ,  2017 ,  1           ,   1       , '10:00-19:00')
 , ('June'       ,  'Friday'   ,  2017 ,  1           ,   2       , '10:00-19:00')
 , ('June'       ,  'Saturday' ,  2017 ,  1           ,   3       , '10:00-19:00');


select
s.Week_Number, s.Month_Name, s.Year
, max(case when s.Day_Name = 'Monday' then s.emp_sched else '' end) as Monday
, max(case when s.Day_Name = 'Tuesday' then s.emp_sched else '' end) as Tuesday
, max(case when s.Day_Name = 'Wednesday' then s.emp_sched else '' end) as Wednesday
, max(case when s.Day_Name = 'Thursday' then s.emp_sched else '' end) as Thursday
, max(case when s.Day_Name = 'Friday' then s.emp_sched else '' end) as Friday
, max(case when s.Day_Name = 'Saturday' then s.emp_sched else '' end) as Saturday
from dbo.Schedule s
group by s.Week_Number, s.Month_Name, s.Year;

答案 1 :(得分:0)

If OBJECT_ID('Tempdb..#Temp') IS NOt NUll
Drop Table #Temp
 ;With cte( Month_Name , Day_Name , Year , Week_Number ,Day_number, emp_sched )
 AS
 (
 SELECT 'May' ,  'Sunday'  , 2017 , 4  , 28, '11:45-20:45' Union all
 SELECT 'May' , 'Tuesday'  , 2017 , 5  , 29, '14:15-23:15' Union all
 SELECT 'May' , 'Wednesday', 2017 , 5  , 30, '13:45-22:45' Union all
 SELECT 'June', 'Thursday' , 2017 , 1  ,  1,'10:00-19:00'  Union all
 SELECT 'June', 'Friday'   , 2017 , 1  ,  2,'10:00-19:00'  Union all
 SELECT 'June', 'Saturday' , 2017 , 1  ,  3,'10:00-19:00'
 )
 SELECT * INTo #Temp from cte


 Declare 
         @Sql nvarchar(max),
         @dynamicCol nvarchar(max),@dynamicCol2 nvarchar(max)

Declare @DynamicColumn Table (ID Int IDentity, WeekName varchar(20))
INsert Into @DynamicColumn(WeekName)
SELECT 'Monday'   Union all
SELECT 'Tuesday'  Union all
SELECT 'Wednesday'Union all
SELECT 'Thursday' Union all
SELECT 'Friday'   Union all
SELECT 'Saturday' Union all
SELECT 'Sunday'

SELECT @dynamicCol=STUFF((SELECT ', ' +WeekName FROM  @DynamicColumn
FOR XML PATH('')),1,1,'')

--SELECT @dynamicCol

SELECT @dynamicCol2=STUFF((SELECT ', ' + 'MAX('+ WeekName +')' +' Over(Order by Week_Number Asc ) AS ' 
+ WeekName  FROM  @DynamicColumn
FOR XML PATH('')),1,1,'')

--SELECT @dynamicCol2

SET @Sql='
SELECT Top 1 Week_Number,Month_Name,[Year], '+ @dynamicCol2 +' From
(
SELECT Month_Name,Day_Name,[Year],Week_Number,Day_number,[emp_sched] From
#temp
)AS Src
PIVOT 
(
MAX([emp_sched]) For [Day_Name ] IN ('+@dynamicCol+')
)
AS Pvt
'

PRINT @Sql

EXEC(@Sql)

输出

Week_Number Month_Name  Year    Monday  Tuesday Wednesday   Thursday        Friday          Saturday        Sunday
---------------------------------------------------------------------------------------------------------------------
1           June        2017    NULL    NULL    NULL        10:00-19:00     10:00-19:00     10:00-19:00     NULL