2011-07-14 67 views
0

在我的sql結束時,我使用下面的代碼。是否有任何方法將固定字符串[2011/07/14],[2011/07/16]等替換爲GetDate()值?數據透視值與日期?

PIVOT 
    (
     count([AppointmentsBooked]) 
     FOR [date] IN ([2011/07/14], [2011/07/16], [2011/07/17],[2011/07/18],[2011/07/21]) 
    ) as pivottable 
+0

從這些日期哪裏應該通過GetDate()調用?任何包含這些日期的表格? – sll

+0

我想:... IN(GetDate(),GetDate()+ 1,GetDate()+ 2) – scouserider

+0

您可以使用BETWEEN查看日期是否在範圍內? – sll

回答

0

你可以嘗試以下方式使用BETWEENDATEADD

DECLARE @dates TABLE(value DateTime) 

INSERT INTO @dates VALUES 
(GETDATE()), 
('2011/07/9'), 
('2011/07/17'), 
('2011/07/18'), 
('2011/07/21') 

SELECT value FROM @dates 
WHERE value BETWEEN GETDATE() AND DATEADD(day, 5, GETDATE()) 
0

您可以創建的所有日期的字符串是逗號分隔用「[」和「]」前後各有日期。將此字符串分配給字符串變量(@dates)並使用字符串spit方法拆分數據透視表中的所有日期。

0

這個問題發佈了大約一年前。我不在乎。我有一些代碼,可能正是OP想要的....我確信他永遠不會回來,並選擇一個答案,但仍然....我想要做的是計數記錄按月份的數據透視爲幾個表格,並最終比較每個表格每個月的記錄數量。然而...在這個代碼中只有一個表(rt_taco_15m),但這並不重要。我只是沒有寫完剩餘,完全符合我的需求。但我認爲這符合OP的需求,或者至少讓他有一個好的開始......如果他真的在這個問題上等了一年。大聲笑。

if object_id('tempdb..#temp') is not null drop table #temp 
if object_id('tempdb..#temp2') is not null drop table #temp2 
if object_id('tempdb..#temp3') is not null drop table #temp3 

declare @start_date as datetime 
    set @start_date = cast('1-1-2012' as datetime) 
declare @end_date as datetime 
    set @end_date = cast('9-1-2012' as datetime) 


;with cte as (
    select @start_date as [start], 
      dateadd(month, 1, @start_date) as [end] 

    union all 

    select dateadd(month, 1, [start]) as [start], 
      dateadd(month, 1, dateadd(month, 1, [start])) as [end] 
    from cte 
    where dateadd(month, 1, [start]) <= @end_date 
) 


(select 'rt_taco_15m' as table_name, 
     convert(varchar(10), [start], 101) as [start], 
     convert(varchar(10), [end], 101) as [end], 
     datename(month, [start]) as month_name, 
     cast([start] as integer) as orderby, 
     count(taco.taco_record_id) as [range_count] 

into #temp 

from cte 

     left outer join rt_taco_15m as taco 
      on taco.period >= cte.[start] and 
       taco.period < cte.[end] 

group by cte.[start], cte.[end]) 


select table_name as table_name, 
     convert(varchar(10), getdate(), 101) as [start], 
     convert(varchar(10), getdate(), 101) as [end], 
     'Total' as month_name, 
     cast(dateadd(month,2,@end_date) as integer) as orderby, 
     range_sum as [range_count] 

into #temp2 

from (select table_name, sum([range_count]) as range_sum 
      from #temp group by table_name) as summed_up 

select * 
into #temp3 
from (select * from #temp 
     union all 
     select * from #temp2) as x 
order by orderby 


select * from #temp3 

declare @cols nvarchar(2000) 
select @cols = stuff(
        (select '],[' + month_name 
        from #temp3 
        order by orderby 
        for xml path('')) 
       , 1, 2, '') + ']' 
print @cols 


if object_id('tempdb..#temp2') is not null drop table #temp2 
declare @query varchar(max) 
set @query = N' 
       select table_name, ' + @cols + N' 

       from (select table_name, month_name, range_count 
          from #temp3) p 

         pivot (sum(range_count) for month_name in ('+ @cols +')) as pvt' 

execute(@query