Это нормально ????
declare @date datetime
set @date = '20140301';
with DaysInMonth as (
select @date as Date,DATENAME(DD,@date) as [DAY],DATENAME(WEEKDAY,@date) as [DAYNAME]
union all
select dateadd(dd,1,Date),DATENAME(DD,Date+1) as [DAY],DATENAME(WEEKDAY,Date+1) as [DAYNAME] from DaysInMonth where month(date) = month(@Date)
)
select [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31]
--Saturday,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday
from
(
SELECT [DAY],[DAYNAME] FROM DaysInMonth where month(date) = month(@Date)
) D
PIVOT (MIN([DAYNAME]) FOR [DAY] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31])) AS PVT
мои другой ответ для точного требования
declare @date datetime
declare @lastWeek int
set @date = '20140301';
set @lastWeek = DATENAME(WW,@date)-1
;with DaysInMonth as (
select @date as Date,
DATENAME(DD,@date) as [DAY],
DATENAME(WEEKDAY,@date)+cast(cast(DATENAME(WW,@date) as int)[email protected] as varchar(2)) as [DAYNAME]
union all
select dateadd(dd,1,Date),DATENAME(DD,Date+1) as [DAY],
DATENAME(WEEKDAY,Date+1)+cast(cast(DATENAME(WW,Date+1) as int)[email protected] as varchar(2)) as [DAYNAME]
from DaysInMonth
where month(date) = month(@Date)
)
select
Sunday1,Monday1,Tuesday1,Wednesday1,Thursday1,Friday1,Saturday1,
Sunday2,Monday2,Tuesday2,Wednesday2,Thursday2,Friday2,Saturday2,
Sunday3,Monday3,Tuesday3,Wednesday3,Thursday3,Friday3,Saturday3,
Sunday4,Monday4,Tuesday4,Wednesday4,Thursday4,Friday4,Saturday4,
Sunday5,Monday5,Tuesday5,Wednesday5,Thursday5,Friday5,Saturday5,
Sunday6,Monday6,Tuesday6,Wednesday6,Thursday6,Friday6,Saturday6
from
(
SELECT [DAY],[DAYNAME] FROM DaysInMonth where month(date) = month(@Date)
) D
PIVOT (max([DAY]) FOR [DAYNAME] IN
(Sunday1,Monday1,Tuesday1,Wednesday1,Thursday1,Friday1,Saturday1,
Sunday2,Monday2,Tuesday2,Wednesday2,Thursday2,Friday2,Saturday2,
Sunday3,Monday3,Tuesday3,Wednesday3,Thursday3,Friday3,Saturday3,
Sunday4,Monday4,Tuesday4,Wednesday4,Thursday4,Friday4,Saturday4,
Sunday5,Monday5,Tuesday5,Wednesday5,Thursday5,Friday5,Saturday5,
Sunday6,Monday6,Tuesday6,Wednesday6,Thursday6,Friday6,Saturday6
)
) AS PVT
невозможно иметь четыре (несколько) столбцов с именами MON в наборе записей. –
Но, возможно, переименуйте это colours Mon1 (соответствует неделе 1) Mon2 (соответствует неделе 2) – Lumina