Winform环境下:数据库Sql2000公司名   价格
A        10
B         9
C         8怎样才可以生成>>水晶报表10  公司名    A   B    C
          ------------
 价格      10  9    8 
请各位大侠提供思路.谢谢!分不够可以再加...

解决方案 »

  1.   

    看下面的做法可以实现你的功能:
    简单的行转列SQL语句
     
    CREATE TABLE [Test] (
           [id] [int] IDENTITY (1, 1) NOT NULL ,
           [name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
           [subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
           [Source] [numeric](18, 0) NULL 
    ) ON [PRIMARY]
    GO
    INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'语文',60)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'数学',70)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'英语',80)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'数学',75)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'语文',57)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'语文',80)
    INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'英语',100)
    Go
    交叉表语句的实现:
    --用于:交叉表的列数是确定的
    select name,sum(case subject when '数学' then source else 0 end) as '数学',
              sum(case subject when '英语' then source else 0 end) as '英语',
                sum(case subject when '语文' then source else 0 end) as '语文' 
    from test 
    group by name
     
    --用于:交叉表的列数是不确定的
    declare @sql varchar(8000)
    set @sql = 'select name,'
    select @sql = @sql + 'sum(case subject when '''+subject+''' 
                              then source else 0 end) as '''+subject+''','
      from (select distinct subject from test) as a 
    select @sql = left(@sql,len(@sql)-1) + ' from test group by name'
    exec(@sql)
    go
    select * from test 
    查询出的结果
      name  subject source
    1  张三  语文   60 
    2  李四  数学   70 
    3  王五  英语   80 
    4  王五  数学   75 
    5  王五  语文   57 
    6  李四  语文   80 
    7  张三  英语   100 
    --用于:交叉表的列数是确定的
    select name,sum(case subject when '数学' then source else 0 end) as '数学',
              sum(case subject when '英语' then source else 0 end) as '英语',
                sum(case subject when '语文' then source else 0 end) as '语文' 
    from test 
    group by name
    查询出的结果
      name  数学  英语   语文
    1 李四 70    0     80
    2 王五 75   80     57
    3 张三 0     100     60
     
    --用于:交叉表的列数是不确定的
    declare @sql varchar(8000)
    set @sql = 'select name,'
    select @sql = @sql + 'sum(case subject when '''+subject+''' 
                              then source else 0 end) as '''+subject+''','
      from (select distinct subject from test) as a 
    select @sql = left(@sql,len(@sql)-1) + ' from test group by name'
    exec(@sql)
    go
    查询的结果也为
      name  数学  英语   语文
    1 李四 70    0     80
    2 王五 75   80     57
    3 张三 0     100     60
      

  2.   

    myreport2.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape预览后是横向的,但打印出来是纵向.有什么方法解?谢谢!!