有如下表,其中币种分为0:人民币(A股) 1:港币(H股) 2:美元(B股)  证券类型:0:股票 4:基金 5:投资基金 7:国债 等之类的。 
要求统计经纪人的交易量,分币种和股票,基金以及其它除股票和基金之外的所有类型,我想进行一个行列转换,需要按币种和证券类型进行行列的转换。经纪人代码 经纪人姓名 机构代码 资金帐号 客户姓名 币种 证券类型 成交金额
1635041    ...         16    ...     ...    0     0      12153.02
1635041    ...         16                   1     4        ...
1635041    ...         16                         5        ... 
1635041                16
1635041                16
1735026    ...         17
1735026                17 
1735026                17 
1735026                17 
1735026                17
1735026                17
1735026                17
1735026                17
18...
...我分了三段
select 经纪人代码, 经纪人姓名,  机构代码, 
  sum(case 币种 when '0' then 成交金额 else 0 end) as A股,
  sum(case 币种 when '1' then 成交金额 else 0 end) as H股,
  sum(case 币种 when '2' then 成交金额 else 0 end) as B股
 from #xxx where 证券类型 = '0' group by  经纪人代码, 经纪人姓名,  机构代码select 经纪人代码, 经纪人姓名,  机构代码, 
  sum(case 币种 when '0' then 成交金额 else 0 end) + 
  sum(case 币种 when '1' then 成交金额 else 0 end) +
  sum(case 币种 when '2' then 成交金额 else 0 end) as 基金
 from #xxx where 证券类型 = '4' or 证券类型 = '5'  group by  经纪人代码, 经纪人姓名,  机构代码select 经纪人代码, 经纪人姓名,  机构代码, 
  sum(case 币种 when '0' then 成交金额 else 0 end) +
  sum(case 币种 when '1' then 成交金额 else 0 end) +
  sum(case 币种 when '2' then 成交金额 else 0 end) as [国债、债券及回购]
 from #xxx where 证券类型 not in ('0', '4', '5')  group by  经纪人代码, 经纪人姓名,  机构代码如何把他们组合到一起,得到一张表,
字段如下:
经纪人代码 经纪人姓名 机构代码 A股成交金额 B股成交金额 H股成交金额 国债、债券及回购  

解决方案 »

  1.   

    select t1.* , t2.* , t3.* from
    (
    select 经纪人代码, 经纪人姓名,  机构代码, 
      sum(case 币种 when '0' then 成交金额 else 0 end) as A股,
      sum(case 币种 when '1' then 成交金额 else 0 end) as H股,
      sum(case 币种 when '2' then 成交金额 else 0 end) as B股
     from #xxx where 证券类型 = '0' group by  经纪人代码, 经纪人姓名,  机构代码
    ) t1,
    (
    select 经纪人代码, 经纪人姓名,  机构代码, 
      sum(case 币种 when '0' then 成交金额 else 0 end) + 
      sum(case 币种 when '1' then 成交金额 else 0 end) +
      sum(case 币种 when '2' then 成交金额 else 0 end) as 基金
     from #xxx where 证券类型 = '4' or 证券类型 = '5'  group by  经纪人代码, 经纪人姓名,  机构代码
    ) t2, 
    (
    select 经纪人代码, 经纪人姓名,  机构代码, 
      sum(case 币种 when '0' then 成交金额 else 0 end) +
      sum(case 币种 when '1' then 成交金额 else 0 end) +
      sum(case 币种 when '2' then 成交金额 else 0 end) as [国债、债券及回购]
     from #xxx where 证券类型 not in ('0', '4', '5')  group by  经纪人代码, 经纪人姓名,  机构代码
    ) t3
    where t1.关键字 = t2.关键字 and t1.关键字 = t3.关键字
      

  2.   

    --也可以这样[code=SQL]select 经纪人代码, 经纪人姓名,  机构代码, 
      sum(case when 币种 = '0' and 证券类型 = '0' then 成交金额 else 0 end) as A股,
      sum(case when 币种 = '1' and 证券类型 = '0' then 成交金额 else 0 end) as H股,
      sum(case when 币种 = '2' and 证券类型 = '0' then 成交金额 else 0 end) as B股,
      sum(case when 币种 in ('0','1','2') and 证券类型 in ('4' , '5') then 成交金额 else 0 end) as 基金
      ...国债、债券及回购的算法没看明白,你自己加入就是了。
     from #xxx group by  经纪人代码, 经纪人姓名,  机构代码[/code]
      

  3.   

    --TRY
    select 经纪人代码, 经纪人姓名,  机构代码, 
      sum(case when 币种 = '0' and 证券类型 = '0' then 成交金额 else 0 end) as A股,
      sum(case when 币种 = '1' and 证券类型 = '0' then 成交金额 else 0 end) as H股,
      sum(case when 币种 = '2' and 证券类型 = '0' then 成交金额 else 0 end) as B股,
      sum(case when 币种 in ('0','1','2') and 证券类型 in ('4' , '5') then 成交金额 else 0 end) as 基金,
      sum(case when 币种 in ('0','1','2') and 证券类型 in ('1' , '2') then 成交金额 else 0 end) as [国债、债券及回购]
    from #xxx group by  经纪人代码, 经纪人姓名,  机构代码