我想查询一个交易信息表的 , 一 个用户的05年到08年的交易记录CREATE TABLE transInfo  --交易信息表
(
  id BIGINT IDENTITY(1,1),
  cardID  CHAR(19) NOT NULL,  --引用卡号
  transDate  DATETIME NOT NULL, --交易日期
  transType  CHAR(4) NOT NULL, --交易类型(转入/转出)  
  transMoney  MONEY NOT NULL, --交易金额 
)select * from dbo.transInfo
表是这样的
      卡号                     交易时间       交易类型         交易金额
id   cardID               transDate    transType      transMoney   
--   ------               ---------    ----------     -----------                    
1   1010123412345678123 2008-01-25    支取              900.0000
2   1010123412121134789 2008-01-25    转入              5000.0000经过这样查询select   '年份'=   datepart(yy,transDate), 
'数量'=count(*),
'1月'=sum(case when datepart(mm,transDate) = 1 then 1 else 0 end),
'2月'=sum(case when datepart(mm,transDate) = 2 then 1 else 0 end),
'3月'=sum(case when datepart(mm,transDate) = 3 then 1 else 0 end),
'4月'=sum(case when datepart(mm,transDate) = 4 then 1 else 0 end),
'5月'=sum(case when datepart(mm,transDate) = 5 then 1 else 0 end),
'6月'=sum(case when datepart(mm,transDate) = 6 then 1 else 0 end),
'7月'=sum(case when datepart(mm,transDate) = 7 then 1 else 0 end),
'8月'=sum(case when datepart(mm,transDate) = 8 then 1 else 0 end),
'9月'=sum(case when datepart(mm,transDate) = 9 then 1 else 0 end),
'10月'=sum(case when datepart(mm,transDate) = 10 then 1 else 0 end),
'11月'=sum(case when datepart(mm,transDate) = 11 then 1 else 0 end),
'12月'=sum(case when datepart(mm,transDate) = 12 then 1 else 0 end)
FROM transInfo  WHERE   (transDate> = '2002')   AND   (transDate <= '2009')
group   by   datepart(yy,transDate)这样的结果。。只是统计了交易次数而已,
而且还没有帐户 和交易类型。    所以不是我要的。。
年份  交易次数  1月次数   2月次数 ………… 12月次数
---------------------------------------------
2007  0          0       0               0
2008  2          2       0               0 我想要这样的结果!!~  
cardID       年份  交易类型      1月金额   2月金额 ………… 12月金额
-------------------------------------------------------
 11      2006  转入           300        1               1
 11      2007  转入          200       0               0
 11     2008  转入          100       0               0 cardID  年份  交易类型      1月金额   2月金额 ………… 12月金额
-------------------------------------------------------
 11      2006  转出           300        1               1
 11      2007  转出          200       0               0
 11      2008  转出          100       0               0 不知道能否得出这样的结果 。。  请 各位 大侠帮帮我 分数不多 我以倾我所有了
 小弟不胜感激啊~~

解决方案 »

  1.   


    --tryselect cardID,year(transDate) as [year],transType,
    sum(case when month(transDate)=1 then transMoney else 0 end) as '1月',
    sum(case when month(transDate)=2 then transMoney else 0 end) as '2月',
    ...
    from transInfo
    group by cardID,year(transDate),transType
      

  2.   


    declare @s nvarchar(4000),@i int
    select @s='',@i=12
    while @i>0
    select @s=','+quotename(+rtrim(@i)+'月')+
    '=sum(case when datepart(mm,transDate)='+rtrim(@i)+' then 1 else 0 end)'+@s
    ,@i=@i-1
    print @s
    set @s='select cardID,[年份]=datepart(yy,transDate),transType' +@s+' from transInfo group by cardID,datepart(yy,transDate),transType'
    --print @s--显示语句
    exec(@s)
      

  3.   

    select   '年份'=   datepart(yy,transDate), cardID,transType,
    '数量'=count(*),
    '1月'=sum(case when datepart(mm,transDate) = 1 then 1 else 0 end),
    '2月'=sum(case when datepart(mm,transDate) = 2 then 1 else 0 end),
    '3月'=sum(case when datepart(mm,transDate) = 3 then 1 else 0 end),
    '4月'=sum(case when datepart(mm,transDate) = 4 then 1 else 0 end),
    '5月'=sum(case when datepart(mm,transDate) = 5 then 1 else 0 end),
    '6月'=sum(case when datepart(mm,transDate) = 6 then 1 else 0 end),
    '7月'=sum(case when datepart(mm,transDate) = 7 then 1 else 0 end),
    '8月'=sum(case when datepart(mm,transDate) = 8 then 1 else 0 end),
    '9月'=sum(case when datepart(mm,transDate) = 9 then 1 else 0 end),
    '10月'=sum(case when datepart(mm,transDate) = 10 then 1 else 0 end),
    '11月'=sum(case when datepart(mm,transDate) = 11 then 1 else 0 end),
    '12月'=sum(case when datepart(mm,transDate) = 12 then 1 else 0 end)
    FROM transInfo  WHERE   (transDate> = '2002')   AND   (transDate <= '2009')
    group   by   datepart(yy,transDate),cardID,transType
      

  4.   

    /*
    普通行列转换
    (爱新觉罗.毓华 2007-11-18于海南三亚)假设有张学生成绩表(tb)如下:
    Name Subject Result
    张三 语文  74
    张三 数学  83
    张三 物理  93
    李四 语文  74
    李四 数学  84
    李四 物理  94
    */-------------------------------------------------------------------------
    /*
    想变成 
    姓名         语文        数学        物理          
    ---------- ----------- ----------- ----------- 
    李四         74          84          94
    张三         74          83          93
    */create table tb
    (
       Name    varchar(10) ,
       Subject varchar(10) ,
       Result  int
    )insert into tb(Name , Subject , Result) values('张三' , '语文' , 74)
    insert into tb(Name , Subject , Result) values('张三' , '数学' , 83)
    insert into tb(Name , Subject , Result) values('张三' , '物理' , 93)
    insert into tb(Name , Subject , Result) values('李四' , '语文' , 74)
    insert into tb(Name , Subject , Result) values('李四' , '数学' , 84)
    insert into tb(Name , Subject , Result) values('李四' , '物理' , 94)
    go--静态SQL,指subject只有语文、数学、物理这三门课程。
    select name 姓名,
      max(case subject when '语文' then result else 0 end) 语文,
      max(case subject when '数学' then result else 0 end) 数学,
      max(case subject when '物理' then result else 0 end) 物理
    from tb
    group by name
    /*
    姓名         语文        数学        物理          
    ---------- ----------- ----------- ----------- 
    李四         74          84          94
    张三         74          83          93
    */--动态SQL,指subject不止语文、数学、物理这三门课程。
    declare @sql varchar(8000)
    set @sql = 'select Name as ' + '姓名'
    select @sql = @sql + ' , max(case Subject when ''' + Subject + ''' then Result else 0 end) [' + Subject + ']'
    from (select distinct Subject from tb) as a
    set @sql = @sql + ' from tb group by name'
    exec(@sql) 
    /*
    姓名         数学        物理        语文          
    ---------- ----------- ----------- ----------- 
    李四         84          94          74
    张三         83          93          74
    */-------------------------------------------------------------------
    /*加个平均分,总分
    姓名         语文        数学        物理        平均分                总分          
    ---------- ----------- ----------- ----------- -------------------- ----------- 
    李四         74          84          94          84.00                252
    张三         74          83          93          83.33                250
    */--静态SQL,指subject只有语文、数学、物理这三门课程。
    select name 姓名,
      max(case subject when '语文' then result else 0 end) 语文,
      max(case subject when '数学' then result else 0 end) 数学,
      max(case subject when '物理' then result else 0 end) 物理,
      cast(avg(result*1.0) as decimal(18,2)) 平均分,
      sum(result) 总分
    from tb
    group by name
    /*
    姓名         语文        数学        物理        平均分                总分          
    ---------- ----------- ----------- ----------- -------------------- ----------- 
    李四         74          84          94          84.00                252
    张三         74          83          93          83.33                250
    */--动态SQL,指subject不止语文、数学、物理这三门课程。
    declare @sql1 varchar(8000)
    set @sql1 = 'select Name as ' + '姓名'
    select @sql1 = @sql1 + ' , max(case Subject when ''' + Subject + ''' then Result else 0 end) [' + Subject + ']'
    from (select distinct Subject from tb) as a
    set @sql1 = @sql1 + ' , cast(avg(result*1.0) as decimal(18,2)) 平均分,sum(result) 总分 from tb group by name'
    exec(@sql1) 
    /*
    姓名         数学        物理        语文        平均分                总分          
    ---------- ----------- ----------- ----------- -------------------- ----------- 
    李四         84          94          74          84.00                252
    张三         83          93          74          83.33                250
    */drop table tb ---------------------------------------------------------
    ---------------------------------------------------------
    /*
    如果上述两表互相换一下:即姓名 语文 数学 物理
    张三 74  83  93
    李四 74  84  94想变成 
    Name       Subject Result      
    ---------- ------- ----------- 
    李四         语文      74
    李四         数学      84
    李四         物理      94
    张三         语文      74
    张三         数学      83
    张三         物理      93
    */create table tb1
    (
       姓名 varchar(10) ,
       语文 int ,
       数学 int ,
       物理 int
    )insert into tb1(姓名 , 语文 , 数学 , 物理) values('张三',74,83,93)
    insert into tb1(姓名 , 语文 , 数学 , 物理) values('李四',74,84,94)select * from
    (
      select 姓名 as Name , Subject = '语文' , Result = 语文 from tb1 
      union all
      select 姓名 as Name , Subject = '数学' , Result = 数学 from tb1
      union all
      select 姓名 as Name , Subject = '物理' , Result = 物理 from tb1
    ) t
    order by name , case Subject when '语文' then 1 when '数学' then 2 when '物理' then 3 when '总分' then 4 end--------------------------------------------------------------------
    /*加个平均分,总分
    Name       Subject     Result               
    ---------- -------    -------------------- 
    李四         语文      74.00
    李四         数学      84.00
    李四         物理      94.00
    李四         平均分    84.00
    李四         总分      252.00
    张三         语文      74.00
    张三         数学      83.00
    张三         物理      93.00
    张三         平均分    83.33
    张三         总分      250.00
    */select * from
    (
      select 姓名 as Name , Subject = '语文' , Result = 语文 from tb1 
      union all
      select 姓名 as Name , Subject = '数学' , Result = 数学 from tb1
      union all
      select 姓名 as Name , Subject = '物理' , Result = 物理 from tb1
      union all
      select 姓名 as Name , Subject = '平均分' , Result = cast((语文 + 数学 + 物理)*1.0/3 as decimal(18,2)) from tb1
      union all
      select 姓名 as Name , Subject = '总分' , Result = 语文 + 数学 + 物理 from tb1
    ) t
    order by name , case Subject when '语文' then 1 when '数学' then 2 when '物理' then 3 when '平均分' then 4 when '总分' then 5 enddrop table tb1
      

  5.   

    --静态SQL,月份固定
    select '年份'= datepart(yy,transDate), cardID,transType,
    '数量'=count(*),
    '1月'=sum(case when datepart(mm,transDate) = 1 then 1 else 0 end),
    '2月'=sum(case when datepart(mm,transDate) = 2 then 1 else 0 end),
    '3月'=sum(case when datepart(mm,transDate) = 3 then 1 else 0 end),
    '4月'=sum(case when datepart(mm,transDate) = 4 then 1 else 0 end),
    '5月'=sum(case when datepart(mm,transDate) = 5 then 1 else 0 end),
    '6月'=sum(case when datepart(mm,transDate) = 6 then 1 else 0 end),
    '7月'=sum(case when datepart(mm,transDate) = 7 then 1 else 0 end),
    '8月'=sum(case when datepart(mm,transDate) = 8 then 1 else 0 end),
    '9月'=sum(case when datepart(mm,transDate) = 9 then 1 else 0 end),
    '10月'=sum(case when datepart(mm,transDate) = 10 then 1 else 0 end),
    '11月'=sum(case when datepart(mm,transDate) = 11 then 1 else 0 end),
    '12月'=sum(case when datepart(mm,transDate) = 12 then 1 else 0 end)
    FROM transInfo WHERE (transDate> = '2002') AND (transDate <= '2009')
    group by datepart(yy,transDate),cardID,transType
    --动态SQL,月份不固定
    declare @sql varchar(8000)
    set @sql = 'select datepart(yy,transDate) year_mm, cardID,transType'
    select @sql = @sql + ' , sum(case datepart(mm,transDate) when ''' + datename(mm,transDate) + ''' then 1 else 0 end) [' + datename(mm,transDate) + '月]'
    from (select distinct datepart(mm,transDate) mm from tb WHERE (transDate> = '2002') AND (transDate <= '2009')) as a
    set @sql = @sql + ' from tb WHERE (transDate> = ''2002'') AND (transDate <= ''2009'')
     group by datepart(yy,transDate), cardID,transType'
    exec(@sql) 
      

  6.   

    把动态SQL中的tb改为transInfo
    --动态SQL,月份不固定
    declare @sql varchar(8000)
    set @sql = 'select datepart(yy,transDate) year_mm, cardID,transType'
    select @sql = @sql + ' , sum(case datepart(mm,transDate) when ''' + datename(mm,transDate) + ''' then 1 else 0 end) [' + datename(mm,transDate) + '月]'
    from (select distinct datepart(mm,transDate) mm from transInfo WHERE (transDate> = '2002') AND (transDate <= '2009')) as a
    set @sql = @sql + ' from transInfo WHERE (transDate> = ''2002'') AND (transDate <= ''2009'')
     group by datepart(yy,transDate), cardID,transType'
    exec(@sql)