日期:2014-05-19  浏览次数:20500 次

==帮忙看个Sql,十万火急,拜托了===
Table   tblTest
    (CityDis   不固定)
      TypeName     CityDis           Unit               Result  
        A                     北京           1000元                 1245
        A                     上海           10000元               548
        B                     北京           1000元                 2134
        B                     上海           10000元               5412
        ..                   ...             .....                   ....

  想得到以下结果

    TypeName               北京                                 上海
          A                       1245(1000元)             548(10000元)
          B                       2134(1000元)               5412(10000元)


拜托各位了.

------解决方案--------------------
--参考

/*-- 数据测试环境 --*/
if exists (select * from dbo.sysobjects where id = object_id(N '[tb] ') and OBJECTPROPERTY(id, N 'IsUserTable ') = 1)
drop table [tb]
GO

create table tb(单位名称 varchar(10),日期 datetime,销售额 int)
insert into tb
select 'A单位 ', '2001-01-01 ',100
union all select 'B单位 ', '2001-01-02 ',101
union all select 'C单位 ', '2001-01-03 ',102
union all select 'D单位 ', '2001-01-04 ',103
union all select 'E单位 ', '2001-01-05 ',104
union all select 'F单位 ', '2001-01-06 ',105
union all select 'G单位 ', '2001-01-07 ',106
union all select 'H单位 ', '2001-01-08 ',107
union all select 'I单位 ', '2001-01-09 ',108
union all select 'J单位 ', '2001-01-11 ',109

/*-- 要求结果
日期 A单位 B单位 C单位 D单位 E单位 F单位 G单位 H单位 I单位 J单位
---------- ----- ----- ----- ----- ----- ----- ---- ---- ---- ------
2001-01-01 100 0 0 0 0 0 0 0 0 0
2001-01-02 0 101 0 0 0 0 0 0 0 0
2001-01-03 0 0 102 0 0 0 0 0 0 0
2001-01-04 0 0 0 103 0 0 0 0 0 0
2001-01-05 0 0 0 0 104 0 0 0 0 0
2001-01-06 0 0 0 0 0 105 0 0 0 0
2001-01-07 0 0 0 0 0 0 106 0 0 0
2001-01-08 0 0 0 0 0 0 0 107 0 0
2001-01-09 0 0 0 0 0