2011-07-14 65 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