declare @s varchar(8000)
--纵向查询
select top 30 [date]
       ,[count]=count(*)
           into #t
           from table1 where [date]<getdate() order by [date] desc
           group by [date])
--行列转换
select @s=''+[date]+'=max(case [date] when '''+[date]+''' then [count] end),'
  from #t 
set @s=left(@s,len(@s)-1)+' from #t group by [date]'
exec(@s)

解决方案 »

  1.   


    select T.* into #t from (select 日期= substring(convert(varchar(10),[date],120),9,10),訪問次數=count(*) from table1 group by 日期)T
    select * from #t
    --然後對#t進行行列轉換
      

  2.   

    回复人: mschen(Visual【陈】) ( ) 信誉:100  2005-05-16 12:44:00  得分: 0  
     
     
       
    declare @s varchar(8000)
    --纵向查询
    select top 30 [date]
           ,[count]=count(*)
               into #t
               from table1 where [date]<getdate() order by [date] desc
               group by [date])          <------这里
    --行列转换
    select @s=''+[date]+'=max(case [date] when '''+[date]+''' then [count] end),'
      from #t 
    set @s=left(@s,len(@s)-1)+' from #t group by [date]'
    exec(@s)  
    说有语法错误
      

  3.   

    TO mschen(Visual【陈】) 
    #t 是什么意思
      

  4.   

    --下面的代碼是實現最近30天,的記錄
    select 日期=substring(N.日期,9,10),N.訪問次數 into #t from 
    ( select top 30  T.* from
     (select 日期=convert(varchar(10),[date],120),訪問次數=count(*)  from table1 group by 日期)T order by T.日記 desc)N --查看
    select * from #t
    --然後對#t進行行列轉換
      

  5.   

    group by [date])          
    去掉后面的 )即:
    declare @s varchar(8000)
    --纵向查询
    select top 30 [date]
           ,[count]=count(*)
               into #t
               from table1 where [date]<getdate() order by [date] desc
               group by [date]
    --行列转换
    select @s=''+[date]+'=max(case [date] when '''+[date]+''' then [count] end),'
      from #t 
    set @s=left(@s,len(@s)-1)+' from #t group by [date]'
    exec(@s)
      

  6.   

    zhangzs8896(小二) 查询说
    服务器: 消息 156,级别 15,状态 1,行 7
    在关键字 'group' 附近有语法错误。
      

  7.   

    TO: talantlee(把 星星送給她....) 
    谢谢你的帮忙,已经生成的新表,可是记录是纵向的,希望生成横向的,新表里只有一条记录有31个字段,新表可以先创建(如果查询设计很麻烦的话)
      

  8.   

    TO:zhangzs8896(小二) 我修改了一下Group BY的位置
    declare @s varchar(8000)select top 30
    [date],
    [count]=count(*)
        into #t
        from table1
        where [date]<getdate()
        group by [date]
        order by [date] descselect @s=''+[date]+'=max(case [date] when '''+[date]+''' then [count] end),'
      from #t 
    set @s=left(@s,len(@s)-1)+' from #t group by [date]'
    exec(@s)还是报错:(所影响的行数为 8 行)服务器: 消息 241,级别 16,状态 1,行 12
    从字符串转换为 datetime 时发生语法错误。