-1

我正在寻找一种在 SQL Server 中按周名称转换列的有效方法。

这是我的例子:

Id  Value   ondate
1   10      06/05/2013      
2   9       07/05/2013      
3   5       08/05/2013      
4   89      09/05/2013      
5   8       10/05/2013  

场景#1:

  • 开始日期:06/05/2013(星期一)
  • 结束日期:14/05/2013(星期四)

这是我的结果:

       mo   tu  we  th  fr  sa  su
row1   10   9   5   89  8   23  3
row2    5   8

场景#2:

  • 开始日期:06/05/2013(星期一)
  • 结束日期:07/05/2013(星期四)

这是我想要的结果:

        mo   tu
row1    10    9

如何构建结果?

4

2 回答 2

0

试试这个PIVOT: -

Declare @startDate datetime,@endDate datetime
Set @startDate = '05/06/2013' 
Set @endDate = '05/14/2013'


Select  sum([Monday]) as Mo,sum([Tuesday]) as Tu,
        sum([wednesday]) as Wed,sum([Thursday]) as Th,sum([Friday]) as Fr,
        sum([Saturday]) as St,sum([Sunday]) as Su
from 
(
  Select value,datename(weekday,ondate) as dayName,ondate
  from @Sample 
  where ondate >= @startDate and ondate <= @endDate
)p
pivot
(
 min(value)
 for dayName in ([Monday],[Tuesday],[wednesday],[Thursday],[Friday],
                 [Saturday],[Sunday])
)pvt
group by datename(week,ondate)

开始日期的输出:2013 年 5 月 6 日(星期一) 结束日期:2013 年 5 月 14 日(星期四)

 ╔════╦══════╦══════╦══════╦══════╦══════╦══════╗
 ║ Mo ║  Tu  ║ Wed  ║  Th  ║  Fr  ║  St  ║  Su  ║
 ╠════╬══════╬══════╬══════╬══════╬══════╬══════╣
 ║ 10 ║ 9    ║ 5    ║ 89   ║ 8    ║ 23   ║ 3    ║
 ║ 12 ║ NULL ║ NULL ║ NULL ║ NULL ║ NULL ║ NULL ║
 ╚════╩══════╩══════╩══════╩══════╩══════╩══════╝

输出开始日期:05/06/2013(星期一) 结束日期:05/07/2013(星期四)

 ╔════╦════╦══════╦══════╦══════╦══════╦══════╗
 ║ Mo ║ Tu ║ Wed  ║  Th  ║  Fr  ║  St  ║  Su  ║
 ╠════╬════╬══════╬══════╬══════╬══════╬══════╣
 ║ 10 ║  9 ║ NULL ║ NULL ║ NULL ║ NULL ║ NULL ║
 ╚════╩════╩══════╩══════╩══════╩══════╩══════╝

SQL FIDDLE中的演示

于 2013-05-06T07:34:32.173 回答
0

如下使用sum(case...将数据作为列跨天旋转(或查看PIVOT运算符)。此外,请考虑多年可能会如何影响您的结果,并相应地调整分组。

set dateformat dmy;
set datefirst 1; --monday

declare @YourTable table (Id int, Value int, ondate datetime)
insert into @YourTable
    select 1,   10, '06/05/2013' union all     
    select 2,   9,  '07/05/2013' union all      
    select 3,   5,  '08/05/2013' union all      
    select 4,   89, '09/05/2013' union all      
    select 5,   8,  '10/05/2013' union all
    select 6,   23, '11/05/2013' union all
    select 7,   3,  '12/05/2013' union all
    select 8,   5,  '13/05/2013' union all
    select 9,   8,  '14/05/2013'

select  [mo]=sum(case when datename(weekday, ondate) = 'Monday' then Value else 0 end),
        [tu]=sum(case when datename(weekday, ondate) = 'Tuesday' then Value else 0 end),
        [we]=sum(case when datename(weekday, ondate) = 'Wednesday' then Value else 0 end),
        [th]=sum(case when datename(weekday, ondate) = 'Thursday' then Value else 0 end),
        [fr]=sum(case when datename(weekday, ondate) = 'Friday' then Value else 0 end),
        [sa]=sum(case when datename(weekday, ondate) = 'Saturday' then Value else 0 end),
        [su]=sum(case when datename(weekday, ondate) = 'Sunday' then Value else 0 end)
from    @YourTable
where   ondate between '06/05/2013' and '14/05/2013'
group
by      datepart(week, ondate);

-- Result:
/*
mo  tu  we  th  fr  sa  su
10  9   5   89  8   23  3
5   8   0   0   0   0   0
*/
于 2013-05-06T06:40:42.477 回答