使用group by name  来进行分组

解决方案 »

  1.   

      结果在问题中说的很明了   根据 select money form where name='a' and time='上月'  这样的条件语句 查询的结果money 合并成一行数据  结果 18 20 22 16 10 5   
    我这边不能操作到数据库  无法去写存错过程或重编视图  很少做sql这类的项目  一时间找不到解决办法               
      

  2.   

     根据 这样的  单条查询   select money form where name='a' and time='上月'  查询出money 合并money结果到一行数据 结果 18 20 22 16 10 5 
               
      

  3.   

      一条数据 一行  多列 
    自己知道每列名   nameA1  nameA2  nameB1 nameB2
                     18      20      22     16
      

  4.   

    这样吗?----------------------------------------------------------------
    -- Author  :DBA_Huangzj(發糞塗牆)
    -- Date    :2014-03-03 10:21:28
    -- Version:
    --      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
    -- Dec 28 2012 20:23:12 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
    --
    ----------------------------------------------------------------
    --> 测试数据:[test]
    if object_id('[test]') is not null drop table [test]
    go 
    create table [test]([name] varchar(1),[time] varchar(4),[money] int)
    insert [test]
    select 'a','上月',18 union all
    select 'a','本月',20 union all
    select 'b','上月',22 union all
    select 'b','本月',16 union all
    select 'c','上月',10 union all
    select 'c','本月',5
    --------------开始查询--------------------------
    declare @s nvarchar(4000)
    set @s=''
    Select     @s=@s+','+quotename('Money')+'=max(case when [name]='+quotename([name],'''')+' and [time]='+quotename([time],'''')+'   then [money] else 0 end)'
    from [test] group by [name],[time]
    exec('select [name],[time]'+@s+' from [test] where name=''a''  and time=''上月'' group by [name],[time]')----------------结果----------------------------
    /* 
    name time Money       Money       Money       Money       Money       Money
    ---- ---- ----------- ----------- ----------- ----------- ----------- -----------
    a    上月   0           18          0           0           0           0
    */
      

  5.   

    ----------------------------------------------------------------
    -- Author  :DBA_Huangzj(發糞塗牆)
    -- Date    :2014-03-03 10:21:28
    -- Version:
    --      Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
    -- Dec 28 2012 20:23:12 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
    --
    ----------------------------------------------------------------
    --> 测试数据:[test]
    if object_id('[test]') is not null drop table [test]
    go 
    create table [test]([name] varchar(1),[time] varchar(4),[money] int)
    insert [test]
    select 'a','上月',18 union all
    select 'a','本月',20 union all
    select 'b','上月',22 union all
    select 'b','本月',16 union all
    select 'c','上月',10 union all
    select 'c','本月',5
    --------------开始查询--------------------------
    declare @s nvarchar(4000)
    set @s=''
    Select     @s=@s+','+quotename('name'+name+CAST(ROW_NUMBER()OVER(PARTITION BY name ORDER BY getdate()) AS VARCHAR))+'=max(case when [name]='+quotename([name],'''')+' and [time]='+quotename([time],'''')+'   then [money] else 0 end)'
    from [test] group by [name],[time]
    exec('select [name],[time]'+@s+' from [test] where name=''a''  and time=''上月'' group by [name],[time]')----------------结果----------------------------
    /* 
    name time namea1      namea2      nameb1      nameb2      namec1      namec2
    ---- ---- ----------- ----------- ----------- ----------- ----------- -----------
    a    上月   0           18          0           0           0           0*/
      

  6.   

       不用 name 和time 列    
    你这样也太复杂了 老板一直让我尽量别用函数 或许客户那边不支持  我看了别人给我这样的 select a.money+''+b.money+''+c.money... from ((select money from where )as a ,(select money from where )as b...)
    可行吗