读书人

求教一查询语句!救急高手们!该怎么解

发布时间: 2012-01-08 22:48:50 作者: rapoo

求教一查询语句!!!救急啊!!高手们!!
表A name scot
a 1
b 1
c 1
d 2
e 2

实现成: a 1
d 2

问题2: 实现成: a b 1
a c 1
b c 1
d e 2

[解决办法]
1. select min(name),scot from A group by scot
[解决办法]
select
t.*
from
表 t
where
not exists(select 1 from 表 where name <t.name and scot=t.scot)

select
a.name,b.name,a.scot
from
表 a,表 b
where
a.scot=b.scot and a.name <b.name
order by
a.scot.a.name,b.name
[解决办法]
表A name scot
a 1
b 1
c 1
d 2
e 2

实现成: a 1
d 2

select min(name) name,scot from tb group by scot
[解决办法]
--------------例子--------
create table A(name varchar(10),scot int)
insert A
select 'a ',1 union all
select 'b ',1 union all
select 'c ',1 union all
select 'd ',2 union all
select 'e ',2

select min(name),scot from a group by scot

select
a.name,b.name,a.scot
from
a,a b
where
a.scot=b.scot and a.name <b.name
order by
a.scot,a.name,b.name

读书人网 >SQL Server

热点推荐