当前位置: 代码迷 >> Sql Server >> 请问一个select语句
  详细解决方案

请问一个select语句

热度:86   发布时间:2016-04-24 20:47:32.0
请教一个select语句。
名称  价格
A     12     
A     13
B     12
B     12
C     14

怎么能查询出:有哪些名称的价格不是唯一的?  结果应该是:A

因为两个A的价格一个是12另一个是13,价格不同。
两个B价格都是12,价格是唯一的,即12。
一个C也是唯一的。

------解决方案--------------------
select 名称
from (
select distinct * from tb)t
group by 名称
having COUNT(*)>1

------解决方案--------------------
;with tb as
(
select 'A' as 名称,12 as 价格
union all select 'A',13
union all select 'B',12
union all select 'B',12
union all select 'C',14
)
select 名称
from (
select distinct * from tb)t
group by 名称
having COUNT(*)>1

结果:
/*
A
*/

------解决方案--------------------

create table test (name nvarchar(10),price int)
insert into test values('A',12)
insert into test values('A',13)
insert into test values('B',12)
insert into test values('B',12)
insert into test values('C',14)
insert into test values('C',12)
insert into test values('B',12)
insert into test values('C',13)


select t.name 
from test t
INNER JOIN test e
on t.name=e.name
where e.price<>t.price
group by t.name
/*
name
A
C
*/
  相关解决方案