我是两张表内连接查出来是下面格式序号    公司名称                        08年销售 09年销售 
1    江苏******有限公司                 14975     578
1    江苏******有限公司              16799     522
1    江苏******有限公司     18000     600
1    江苏******有限公司     18500     650
1    江苏******有限公司     18500     650我想把报表格式变换为
序号    公司名称                        08年销售 09年销售 
1    江苏******有限公司                 14975     578
1                                    16799     522
1                           18000     600
1                           18500     650
1                           18500     650

解决方案 »

  1.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([序号] int,[公司名称] varchar(18),[08年销售] int,[09年销售] int)
    insert [tb]
    select 1,'江苏******有限公司',14975,578 union all
    select 1,'江苏******有限公司',16799,522 union all
    select 1,'江苏******有限公司',18000,600 union all
    select 1,'江苏******有限公司',18500,650 union all
    select 1,'江苏******有限公司',18500,650
     
    ---查询---
    select 序号,
    公司名称=case 
               when exists(select 1 from (select *,rn=row_number() over(order by getdate()) from tb)b where rn<a.rn)
                 then '' else 公司名称 end,
    [08年销售],[09年销售] 
    from
    (select *,rn=row_number() over(order by getdate()) from tb)a
    ---结果---
    序号          公司名称               08年销售       09年销售
    ----------- ------------------ ----------- -----------
    1           江苏******有限公司       14975       578
    1                              16799       522
    1                              18000       600
    1                              18500       650
    1                              18500       650(5 行受影响)
      

  2.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2010-04-30 17:36:14
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([序号] int,[公司名称] varchar(18),[08年销售] int,[09年销售] int)
    insert [tb]
    select 1,'江苏******有限公司',14975,578 union all
    select 1,'江苏******有限公司',16799,522 union all
    select 1,'江苏******有限公司',18000,600 union all
    select 1,'江苏******有限公司',18500,650 union all
    select 1,'江苏******有限公司',18500,650
    --------------开始查询--------------------------
    select
      序号,case id when 1 then 公司名称 else '' end as 公司名称,[08年销售],[09年销售]
    from
      (select id=row_number()over(order by  getdate()),* from tb)t
    ----------------结果----------------------------
    /* 序号          公司名称               08年销售       09年销售
    ----------- ------------------ ----------- -----------
    1           江苏******有限公司       14975       578
    1                              16799       522
    1                              18000       600
    1                              18500       650
    1                              18500       650(5 行受影响)*/
      

  3.   

    --------------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2010-04-30 18:34:40
    --  Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    --          Jul  9 2008 14:43:34 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
    --  Blog   : http://blog.csdn.net/htl258
    --------------------------------------------------------------------------
    --> 生成测试数据表:tbIF NOT OBJECT_ID('[tb]') IS NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([序号] INT,[公司名称] NVARCHAR(20),[08年销售] INT,[09年销售] INT)
    INSERT [tb]
    SELECT 1,N'江苏******有限公司',14975,578 UNION ALL
    SELECT 1,N'江苏******有限公司',16799,522 UNION ALL
    SELECT 1,N'江苏******有限公司',18000,600 UNION ALL
    SELECT 1,N'江苏******有限公司',18500,650 UNION ALL
    SELECT 1,N'江苏******有限公司',18500,650 UNION ALL
    SELECT 2,N'浙江******有限公司',14975,578 UNION ALL
    SELECT 2,N'浙江******有限公司',16799,522 UNION ALL
    SELECT 2,N'浙江******有限公司',18000,600 UNION ALL
    SELECT 2,N'浙江******有限公司',18500,650 UNION ALL
    SELECT 2,N'浙江******有限公司',18500,650GO
    --SELECT * FROM [tb]-->SQL查询如下:
    select 序号
    ,公司名称=case ROW_NUMBER()over(partition by 公司名称 ORDER by getdate()) when 1 then 公司名称 else '' end
    ,[08年销售]
    ,[09年销售] 
    from tb
    /*
    序号          公司名称                 08年销售       09年销售
    ----------- -------------------- ----------- -----------
    1           江苏******有限公司         14975       578
    1                                16799       522
    1                                18000       600
    1                                18500       650
    1                                18500       650
    2           浙江******有限公司         14975       578
    2                                16799       522
    2                                18000       600
    2                                18500       650
    2                                18500       650(10 行受影响)
    */
      

  4.   

    create table [tb]([序号] int,[公司名称] varchar(18),[08年销售] int,[09年销售] int)
    insert [tb]
    select 1,'江苏******有限公司',14975,578 union all
    select 1,'江苏******有限公司',16799,522 union all
    select 1,'江苏******有限公司',18000,600 union all
    select 1,'江苏******有限公司',18500,650 union all
    select 1,'江苏******有限公司',18500,650
    goselect 序号,
           [公司名称] = case when [08年销售] = (select top 1 [08年销售] from tb where 序号 = t.序号) then 公司名称 else '' end,
           [08年销售] ,
           [09年销售]
    from tb tdrop table tb/*
    序号          公司名称               08年销售       09年销售       
    ----------- ------------------ ----------- ----------- 
    1           江苏******有限公司       14975       578
    1                              16799       522
    1                              18000       600
    1                              18500       650
    1                              18500       650(所影响的行数为 5 行)
    */
      

  5.   

    row_number() 未能识别的函数  这个是 SQL2005 才有的函数把.
    我想在SQL2000 里面实现.  请问怎么处理?
      

  6.   

    我再把问题描述下. 2张表 (A表,B表)是关联查询出来的. 关联字段FID  SQL2000 实现
    A表 是单据上表头数据, B表是 单据上 表体(分录)的数据.
    A表结构
    FID,  FBillNO,    FDate,   FBillerID ......( 后面还要许多字段)
    1   ,0000001,   2010-01-04,    4     ......
    B表结构
    FID,FentryID,Fitemid,FQty,FAmount,.......( 后面还要许多字段)
    1  ,1       ,12      ,46, 12.34,......
    1  ,2       ,15      ,66, 78.34,......
    1  ,3       ,18      ,35, 67.34,......
    FentryID 表示分录行
    我想2表关联 查询出的结果是
    FID, FentryID, FBillNO,    FDate,   FBillerID....(表体段),Fitemid,FQty,FAmount,...
    1,    1       , 0000001,  2010-01-04, 4 ,     .........,  12,      46,12.34  ,  ...
    1,     2,                                                 15      ,66, 78.43,...   
    1,    3,                                             
    总的效果就是,一张单据 表头数据,除了第一行有,其他几行就为空,但是FID, FentryID不能为空,而且FID不只就一行.要用SQL2000 实现
      

  7.   

    Use subselect to achieve what you want:select
    a.fid,
    b.entryid,
    coalesce((
    select
    top 1 0
    from
    b c
    where
    c.fid=b.fid and
    c.fentryid<b.fentryid
    ), a.FBillNO)
    from
    a inner join b on
    a.fid=b.fid
    order by
    a.fid, b.fentryid