表结构如下:  id  date  value 均为int型
id        name          date    value
1       路由器_TP       201207   30
2       路由器_dl       201207   35
3       路由器_hc       201207   35
4       不同价位a       201207   25
5       不同价位b       201207   30
6       不同价位c       201207   15
7       不同价位d       201207   50
1       路由器_TP       201206   30
2       路由器_dl       201206   30
3       路由器_hc       201206   35
4       不同价位a       201206   25
5       不同价位b       201206   30
6       不同价位c       201206   15
7       不同价位d       201206   30
实现查找出同类型的name,同时间,value的值之和不为100的数据。
即(201207年的不同价位,2012年的路由器)
我实在是找不到关联性,名称一样但是时间又是分开的,条数也不一样,怎么分类呢?望大手指教!不胜感激。

解决方案 »

  1.   

    select name ,date ,sum(value) from 表 group by name ,date  order by date
    你是要这个吗?
      

  2.   

    他们的name不是一样的
    group by也可以?
      

  3.   

    把name放进去的话,也会按照名字来分组。你能给出你“期望”的结果吗?几条既可以了,看结果比较容易理解。或者你先执行,看看是不是你想要的。
      

  4.   

    我想输出的是
    4 不同价位a 201207 
    5 不同价位b 201207 
    6 不同价位c 201207 
    7 不同价位d 201207 
    1 路由器_TP 201206 
    2 路由器_dl 201206 
    3 路由器_hc 201206 
    因为他们的value之和不为一百
      

  5.   


    -->测试数据
    declare @galenkeny table(id int identity(1,1),name varchar(20),date datetime, value int)
    insert into @galenkeny
    select'路由器_TP', '201207', '30' union all
    select'路由器_dl', '201207', '35' union all
    select'路由器_hc', '201207', '35' union all
    select'不同价位a', '201207', '25' union all
    select'不同价位b' ,'201207', '30' union all
    select'不同价位c', '201207', '15' union all
    select'不同价位d', '201207', '50' union all
    select'路由器_TP', '201206', '30' union all
    select'路由器_dl', '201206', '30' union all
    select'路由器_hc', '201206', '35' union all
    select'不同价位a', '201206', '25' union all
    select'不同价位b', '201206', '30' union all
    select'不同价位c', '201206', '15' union all
    select'不同价位d', '201206', '30'--select * from @galenkeny
    -->测试查询
    select name ,date ,sum(value) from @galenkeny  group by date,name  having sum(value)<>'100'  order by date
      

  6.   

    select name ,date ,sum(value) from 表 group by name ,date having sum(value)<>100
      order by name ,date 
    看少了一个条件,没注意你是不要等于100的
      

  7.   

    可能是我表述的不确切:例如
    4 不同价位a 201207 25
    5 不同价位b 201207 30
    6 不同价位c 201207 15
    7 不同价位d 201207 50
    25+30+15+50不等于100  我需要显示出来
    1 路由器_TP 201207 30
    2 路由器_dl 201207 35
    3 路由器_hc 201207 35
    30+35+35=100  就不需要显示了~~
      

  8.   


    -->测试数据
    declare @test table(id int identity(1,1),name varchar(20),date varchar(6), value int)
    insert into @test
    select'路由器_TP', '201207', '30' union all
    select'路由器_dl', '201207', '35' union all
    select'路由器_hc', '201207', '35' union all
    select'不同价位a', '201207', '25' union all
    select'不同价位b' ,'201207', '30' union all
    select'不同价位c', '201207', '15' union all
    select'不同价位d', '201207', '50' union all
    select'路由器_TP', '201206', '30' union all
    select'路由器_dl', '201206', '30' union all
    select'路由器_hc', '201206', '35' union all
    select'不同价位a', '201206', '25' union all
    select'不同价位b', '201206', '30' union all
    select'不同价位c', '201206', '15' union all
    select'不同价位d', '201206', '30'--select * from @test
    -->测试查询
    select name ,left(date,4) date ,sum(value) from @test  
    where name like '路由器%'
    group by left(date,4),name  having sum(value)<>'100'
    union all
    select name ,date ,sum(value) from @test  
    where name like '不同价位%' and date='201207'
    group by date,name  having sum(value)<>'100'  
    order by date/*
    name                 date   
    -------------------- ------ -----------
    路由器_dl               2012   65
    路由器_hc               2012   70
    路由器_TP               2012   60
    不同价位a                201207 25
    不同价位b                201207 30
    不同价位c                201207 15
    不同价位d                201207 50
    */
      

  9.   

    谢谢 DBA!!是我的原因!不好意思!
      

  10.   

    ----------------------------------------------------------------
    -- Author  :TravyLee(物是人非事事休,欲语泪先流!)
    -- Date    :2012-09-07 10:21:08
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) 
    -- Oct 14 2005 00:33:37 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 6.1 (Build 7601: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[test]
    if object_id('[test]') is not null drop table [test]
    go 
    create table [test]([id] int,[name] varchar(9),[date] int,[value] int)
    insert [test]
    select 1,'路由器_TP',201207,30 union all
    select 2,'路由器_dl',201207,35 union all
    select 3,'路由器_hc',201207,35 union all
    select 4,'不同价位a',201207,25 union all
    select 5,'不同价位b',201207,30 union all
    select 6,'不同价位c',201207,15 union all
    select 7,'不同价位d',201207,50 union all
    select 1,'路由器_TP',201206,30 union all
    select 2,'路由器_dl',201206,30 union all
    select 3,'路由器_hc',201206,35 union all
    select 4,'不同价位a',201206,25 union all
    select 5,'不同价位b',201206,30 union all
    select 6,'不同价位c',201206,15 union all
    select 7,'不同价位d',201206,30
    go
    ;with t
    as(
    select 
    id,
    left([name],case when patindex('%[a-z]%',[name])=0 
    then patindex('%[A-Z]%',[name]) else patindex('%[a-z]%',[name]) end-1) as newname,
    name,
    [date],
    [value]
    from 
    test
    ),
    m as(
    select *,sum([value])over(partition by newname,[date]) as st
    from t
    )
    select [id],[name],date,[value] from m where st<>100 order by [date]/*
    1 路由器_TP 201206 30
    2 路由器_dl 201206 30
    3 路由器_hc 201206 35
    4 不同价位a 201207 25
    5 不同价位b 201207 30
    6 不同价位c 201207 15
    7 不同价位d 201207 50
    */
      

  11.   

    12楼的也只是例子而已,你改一下应该可以了,他只是精确到名字和年份,你把这个where条件去掉看看
      

  12.   


    ----------------------------------------------------------------
    -- Author  :TravyLee(物是人非事事休,欲语泪先流!)
    -- Date    :2012-09-07 10:21:08
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) 
    -- Oct 14 2005 00:33:37 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 6.1 (Build 7601: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[test]
    if object_id('[test]') is not null drop table [test]
    go 
    create table [test]([id] int,[name] varchar(9),[date] int,[value] int)
    insert [test]
    select 1,'路由器_TP',201207,30 union all
    select 2,'路由器_dl',201207,35 union all
    select 3,'路由器_hc',201207,35 union all
    select 4,'不同价位a',201207,25 union all
    select 5,'不同价位b',201207,30 union all
    select 6,'不同价位c',201207,15 union all
    select 7,'不同价位d',201207,50 union all
    select 1,'路由器_TP',201206,30 union all
    select 2,'路由器_dl',201206,30 union all
    select 3,'路由器_hc',201206,35 union all
    select 4,'不同价位a',201206,25 union all
    select 5,'不同价位b',201206,30 union all
    select 6,'不同价位c',201206,15 union all
    select 7,'不同价位d',201206,30
    go
    ;with t
    as(
    select 
    id,
    left([name],case when patindex('%[a-z]%',[name])=0 
    then patindex('%[A-Z]%',[name]) else patindex('%[a-z]%',[name]) end-1) as newname,
    name,
    [date],
    [value]
    from 
    test
    ),
    m as(
    select *,sum([value])over(partition by newname,[date]) as st
    from t
    )
    select [id],[name],date,[value] from m where st<>100 order by [date]/*
    1 路由器_TP 201206 30
    2 路由器_dl 201206 30
    3 路由器_hc 201206 35
    4 不同价位a 201207 25
    5 不同价位b 201207 30
    6 不同价位c 201207 15
    7 不同价位d 201207 50
    */
      

  13.   

    [name],case when patindex('%[a-z]%',[name])=0 
                then patindex('%[A-Z]%',[name]) else patindex('%[a-z]%',[name]) end-1) as newname
     这个条件的意义是什么?请问您是根据name来分组的吗?
      

  14.   


    返回name中第一个字母字符所在的位置,拿出来单独测试一下你就明白了
      

  15.   

    路由器品牌关注比例_TP-LINK_当期值
    路由器品牌关注比例_H3C_当期值
    路由器品牌关注比例_思科_当期值
    路由器品牌关注比例_D-Link_当期值
    路由器品牌关注比例_飞鱼星_当期值
    路由器品牌关注比例_华为_当期值
    我做的是这样呢  为了方便大家看 我才把名字随便改的 
      

  16.   


    函数都给出来了,就不能用case when自己再加一个条件啊
      

  17.   

    select patindex(‘字符串1’,'字符串1')
    返回的是字符串1在字符串2中出现的位置
      

  18.   

    路由器品牌关注比例_维盟_当期值
    路由器品牌关注比例_锐捷网络_当期值
    路由器品牌关注比例_其他_当期值
    企业路由器品牌关注比例_H3C_当期值
    企业路由器品牌关注比例_华为_当期值
    企业路由器品牌关注比例_D-Link_当期值就这两个来说  加什么条件区分呢?
    真不好意思~~真是初学啊......
      

  19.   

    您是截取了相同的字符串出来是吧~~~
    路由器品牌关注比例_维盟_当期值
    企业路由器品牌关注比例_D-Link_当期值
    这样的怎么截取‘_’之前的数字呢...真心不会啊~~望赐教~~
      

  20.   

    如果楼主都是以“_”作为分隔符的话,可以这样:--> 测试数据:[test]
    if object_id('[test]') is not null drop table [test]
    go 
    create table [test]([id] int,[name] varchar(100),[date] int,[value] int)
    insert [test]
    select 1,'路由器品牌关注比例_TP-LINK_当期值',201207,30 union all
    select 2,'路由器品牌关注比例_H3C_当期值',201207,35 union all
    select 3,'路由器品牌关注比例_思科_当期值',201207,35 union all
    select 4,'不同价位_a',201207,25 union all
    select 5,'不同价位_b',201207,30 union all
    select 6,'不同价位_c',201207,15 union all
    select 7,'不同价位_d',201207,50 union all
    select 1,'路由器品牌关注比例_D-Link_当期值',201206,30 union all
    select 2,'路由器品牌关注比例_飞鱼星_当期值',201206,30 union all
    select 3,'路由器品牌关注比例_华为_当期值',201206,35 union all
    select 4,'不同价位_a',201206,25 union all
    select 5,'不同价位_b',201206,30 union all
    select 6,'不同价位_c',201206,15 union all
    select 7,'不同价位_d',201206,30
    go;with
    t1 as(
    select left([name], case when charindex('_',[name]) = 0 then 8000 else charindex('_',[name])-1 end) as groupname,*
    from test
    ),
    t2 as(
    select *,sum([value])over(partition by groupname,[date]) as st
    from t1
    )
    select [id],[name],date,[value] from t2 where st<>100 order by [date]/*
    id name date value
    1 路由器品牌关注比例_D-Link_当期值 201206 30
    2 路由器品牌关注比例_飞鱼星_当期值 201206 30
    3 路由器品牌关注比例_华为_当期值 201206 35
    4 不同价位_a 201207 25
    5 不同价位_b 201207 30
    6 不同价位_c 201207 15
    7 不同价位_d 201207 50
    */
    PS:功劳属于#18楼,我只是看楼主貌似真的很捉急....