2014-02-19 11 views
1

я использовать этот запрос для получения в течение всего дня в месяцкак повернуть дни месяца

declare @date datetime 
    set @date = '20140201'; 

with DaysInMonth as (
    select @date as Date 
    union all 
    select dateadd(dd,1,Date) from DaysInMonth where month(date) = month(@Date) 
    ) 
select * from DaysInMonth where month(date) = month(@Date) 

и получить весь день, но в списке

, как я могу получить этот результат, и поставить этот дней в неделю, соответствующей , например, в этом месяце:

Sun1 - Mon1 - TUE1 - WED1 - THU1 - FRI1 - SAT1 - Sun2 - Mon2 - TUE2 - WED2 - THU2 - FRI2 - SAT2 
-  -  -  -  -  -  1  2  3  4  5  6  7  8 

П.Д.: номер 1 и 2, соответствующие недели

+0

невозможно иметь четыре (несколько) столбцов с именами MON в наборе записей. –

+0

Но, возможно, переименуйте это colours Mon1 (соответствует неделе 1) Mon2 (соответствует неделе 2) – Lumina

ответ

0

Это нормально ????

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 
+0

yep, это нормально, но я добавлю [1] в [31] за месяц ... теперь, как я могу поместить эти дней в формате, который объясняет в вопросе? ; c – Lumina

+0

Я отредактировал мое сообщение ... проверьте оба ... – sureshhh

+0

oh man, u босс, эта работа идеально подходит: D, thx много – Lumina

0

попробуйте этот код,

declare @date datetime 
    set @date = '20140901'; 

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],ISNULL([29],'-')[29],ISNULL([30],'-')[30],ISNULL([31],'-')[31] 
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 
+0

в порядке, но мне нужна конкретная неделя, соответствующая дням; c – Lumina

 Смежные вопросы

  • Нет связанных вопросов^_^