当前位置: 代码迷 >> Sql Server >> :用SQL语句查询记录成一个类似万年历
  详细解决方案

:用SQL语句查询记录成一个类似万年历

热度:91   发布时间:2016-04-27 14:50:58.0
请教高手:用SQL语句查询记录成一个类似万年历
请教高手,
大概意思是这样的,有如下一个表
create table test
(
t_type varchar(50),
t_people varchar(50),
t_date datetime
)
go

declare @days int
set @days=31
while (@days>0)
begin
insert into test values('白班1','a'+ltrim(@days),convert(datetime,'2010-7-'+ltrim(@days)))
insert into test values('白班2','b'+ltrim(@days),convert(datetime,'2010-7-'+ltrim(@days)))
set @[email protected]
end

我希望根据传[年份][月份]参数可以查询完结果成如下表:


星期 星期一 星期二 星期三 星期四 星期五 星期六 星期日
日期 7月1日 7月2日 7月3日 7月4日
白班1 a1 a2 a3 a4
白班2 b1 b2 b3 b4
日期 7月5日 7月6日 7月7日 7月8日 7月9日 7月10日 7月11日
白班1 a5 a6 a7 a8 a9 a10 a11
白班2 b5 b6 b7 b8 b9 b10 b11
日期 7月12日 7月13日 7月14日 7月15日 7月16日 7月17日 7月18日
白班1 a12 a13 a14 a15 a16 a17 a18
白班2 b12 b13 b14 b15 b16 b17 b18
日期 7月19日 7月20日 7月21日 7月22日 7月23日 7月24日 7月25日
白班1 a19 a20 a21 a22 a23 a24 a25
白班2 b19 b20 b21 b22 b23 b24 b25
日期 7月26日 7月27日 7月28日 7月29日 7月30日 7月31日
白班1 a26 a27 a28 a29 a30 a31
白班2 b26 b27 b28 b29 b30 b31



------解决方案--------------------
给你个按月显示的例.
SQL code
declare @month as varchar(7)set @month = '2007-12'select 日,一,二,三,四,五,六 from(select week ,   max(case weekday when 1 then datename(day,dt) else '' end ) '日',  max(case weekday when 2 then datename(day,dt) else '' end ) '一',  max(case weekday when 3 then datename(day,dt) else '' end ) '二',  max(case weekday when 4 then datename(day,dt) else '' end ) '三',  max(case weekday when 5 then datename(day,dt) else '' end ) '四',  max(case weekday when 6 then datename(day,dt) else '' end ) '五',  max(case weekday when 7 then datename(day,dt) else '' end ) '六'from(  select week = datepart(week , m.dt) , weekday = datepart(weekday , m.dt) , dt from  (    select dt = @month + '-' + right('00'+cast(t.id as varchar),2) from    (      select 1 as id union select 2 union select 3 union select 4 union select 5      union select 6 union select 7 union select 8 union select 9 union select 10      union select 11 union select 12 union select 13 union select 14 union select 15      union select 16 union select 17 union select 18 union select 19 union select 20      union select 21 union select 22 union select 23 union select 24 union select 25      union select 26 union select 27 union select 28 union select 29 union select 30      union select 31    ) t    where isdate(@month + '-' + right('00'+cast(t.id as varchar),2)) = 1 and @month + '-' + right('00'+cast(t.id as varchar),2) <= dateadd(month , 1 , @month + '-01')  ) m) ngroup by week) o/*日  一  二  三  四  五  六                              --  --  --  --  --  --  --                        12   3   4   5   6   7   89   10  11  12  13  14  1516  17  18  19  20  21  2223  24  25  26  27  28  2930  31                                                                                                                                                         (所影响的行数为 6 行)*/
------解决方案--------------------
SQL code
set datefirst 1select 星期,星期一,星期二,星期三,星期四,星期五,星期六,星期日 from (select ltrim(datepart(ww,t_date))+'00' rowid,'日期' [星期],max(case when datepart(dw,t_date)=1 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期一],max(case when datepart(dw,t_date)=2 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期二],max(case when datepart(dw,t_date)=3 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期三],max(case when datepart(dw,t_date)=4 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期四],max(case when datepart(dw,t_date)=5 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期五],max(case when datepart(dw,t_date)=6 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期六],max(case when datepart(dw,t_date)=7 then ltrim(month(t_date))+'月'+ltrim(day(t_date))+'日' end) [星期日]from testgroup by datepart(ww,t_date)union allselect ltrim(datepart(ww,t_date))+case when t_type='白班1' then '01' else '02' end,t_type [星期],max(case when datepart(dw,t_date)=1 then t_people end) [星期一],max(case when datepart(dw,t_date)=2 then t_people end) [星期二],max(case when datepart(dw,t_date)=3 then t_people end) [星期三],max(case when datepart(dw,t_date)=4 then t_people end) [星期四],max(case when datepart(dw,t_date)=5 then t_people end) [星期五],max(case when datepart(dw,t_date)=6 then t_people end) [星期六],max(case when datepart(dw,t_date)=7 then t_people end) [星期日]from testgroup by datepart(ww,t_date),t_type ) torder by rowid/*星期                                                 星期一                                                星期二                                                星期三                                                星期四                                                星期五                                                星期六                                                星期日-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------------------------日期                                                 NULL                                               NULL                                               NULL                                               7月1日                                               7月2日                                               7月3日                                               7月4日白班1                                                NULL                                               NULL                                               NULL                                               a1                                                 a2                                                 a3                                                 a4白班2                                                NULL                                               NULL                                               NULL                                               b1                                                 b2                                                 b3                                                 b4日期                                                 7月5日                                               7月6日                                               7月7日                                               7月8日                                               7月9日                                               7月10日                                              7月11日白班1                                                a5                                                 a6                                                 a7                                                 a8                                                 a9                                                 a10                                                a11白班2                                                b5                                                 b6                                                 b7                                                 b8                                                 b9                                                 b10                                                b11日期                                                 7月12日                                              7月13日                                              7月14日                                              7月15日                                              7月16日                                              7月17日                                              7月18日白班1                                                a12                                                a13                                                a14                                                a15                                                a16                                                a17                                                a18白班2                                                b12                                                b13                                                b14                                                b15                                                b16                                                b17                                                b18日期                                                 7月19日                                              7月20日                                              7月21日                                              7月22日                                              7月23日                                              7月24日                                              7月25日白班1                                                a19                                                a20                                                a21                                                a22                                                a23                                                a24                                                a25白班2                                                b19                                                b20                                                b21                                                b22                                                b23                                                b24                                                b25日期                                                 7月26日                                              7月27日                                              7月28日                                              7月29日                                              7月30日                                              7月31日                                              NULL白班1                                                a26                                                a27                                                a28                                                a29                                                a30                                                a31                                                NULL白班2                                                b26                                                b27                                                b28                                                b29                                                b30                                                b31                                                NULL警告: 聚合或其他 SET 操作消除了空值。(15 行受影响)*/
  相关解决方案