有表A如下:
列名: 日期        编号     单据号  金额
       2009-10-1  00001    yw00004  100
       2009-10-8  00001    yw00010  800
 
转换为表B——
列名:编号  金额    单据号
     00001  900     yw00004 yw00010 其中表A为明细;经过转换为表B以后,按编号汇总,去掉日期,同时将单据号转在一行中显示;
这样的sql怎么写? 

解决方案 »

  1.   

    http://topic.csdn.net/u/20091014/16/8ba41d87-0288-4e74-b9bb-633ed11cf51d.html参考
      

  2.   

    http://topic.csdn.net/u/20091013/15/9f058df7-4d29-47bf-a338-b63fcab2abc0.html?51371
      

  3.   

    --是不是这个?
    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2009-10-14 18:41:28
    -- 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]([日期] datetime,[编号] varchar(5),[单据号] varchar(7),[金额] int)
    insert [tb]
    select '2009-10-1','00001','yw00004',100 union all
    select '2009-10-8','00001','yw00010',800
    --------------开始查询--------------------------
    select 编号,sum(金额) as 金额, [单据号]=stuff((select ''+[单据号] from tb t where [编号]=tb.[编号] for xml path('')), 1, 0, '') 
    from tb 
    group by 编号 
    ----------------结果----------------------------
    /* 编号    金额          单据号
    ----- ----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    00001 900         yw00004yw00010(1 行受影响)
    */
      

  4.   

    我用的是sql2000 的; 没xml 这个函数!
      

  5.   

    if object_id('f_tmp') is not null
    drop function f_tmp
    go
    create function f_tmp(@value varchar(10))
    returns varchar(100) 
    as
    begin
    declare @s varchar(1000)
    set @s = ''
    select @s = @s+name from t where ck = @value
    return @s
    end
    goif object_id('t') is not null
    drop table tcreate table t (ck varchar(10),name varchar(10),num int)
    insert into t
    select 'B01', 'A',100 
    union all select 'B02', 'B',50 
    union all select 'B01', 'C',30 
    union all select 'B03', 'D',20 
    union all select 'B02', 'A',50 
    union all select 'B04', 'B',100 
    union all select 'B03', 'C',500 
    --sql2000
    select ck
    ,[name] = dbo.f_tmp(ck)
    ,num = sum(num)
    from t a 
    group by ck
    --sql2005
    /*
    select ck
    ,[name] = stuff((select ',' + [name] from t where ck = a.ck for  xml path('')) , 1 , 1 , '')  
    ,num = sum(num)
    from t a
    group by ck
    */
    drop table t
    drop function f_tmpck         name                                                                                                 num         
    ---------- ---------------------------------------------------------------------------------------------------- ----------- 
    B01        AC                                                                                                   130
    B02        BA                                                                                                   100
    B03        DC                                                                                                   520
    B04        B                                                                                                    100(所影响的行数为 4 行)
     给楼主参考,蹭个分
      

  6.   


    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([日期] datetime,[编号] varchar(5),[单据号] varchar(7),[金额] int)
    insert [tb]
    select '2009-10-1','00001','yw00004',100 union all
    select '2009-10-8','00001','yw00010',800declare @s varchar(40)select @s=isnull(@s,'')+' ' +[单据号] from tb
    select [编号],[金额]=sum([金额]),[单据号]=max(@s) from tb group by [编号]
    编号    金额          单据号
    ----- ----------- ----------------------------------------
    00001 900          yw00004 yw00010(1 行受影响)
      

  7.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2009-10-14 18:41:28
    -- 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]([日期] datetime,[编号] varchar(5),[单据号] varchar(7),[金额] int)
    insert [tb]
    select '2009-10-1','00001','yw00004',100 union all
    select '2009-10-8','00001','yw00010',800
    --------------开始查询--------------------------
    CREATE FUNCTION dbo.f_strUnite(@id varchar(10)) 
    RETURNS varchar(8000) 
    AS 
    BEGIN 
        DECLARE @str varchar(8000) 
        SET @str = '' 
        SELECT @str = @str + ' ' + 单据号 FROM tb WHERE 编号=@id 
        RETURN STUFF(@str, 1, 1, '') 
    END 
    GO 
    -- 调用函数 
    SELECT 编号,sum(金额) as 金额, 单据号 = dbo.f_strUnite(编号) FROM tb GROUP BY 编号 
    drop table tb 
    drop function dbo.f_strUnite 
    go 
    ----------------结果----------------------------
    /* 编号    金额          单据号
    ----- ----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    00001 900         yw00004yw00010(1 行受影响)
    */
      

  8.   

    特别感谢: js_szy
             (华夏小卒)   
     你是我的偶像!!!