表中记录如下,
字段1 字段2 字段3 字段4
aa 1 xx xx
aa 5 xx xx
bb 1 .. ..
cc 1 .. ..
cc 5 .. ..
dd 1 .. ..
ee 1 .. ..
ff 1 .. ..
ff 5 .. ..
希望查询出的记录集,如下:
字段1 字段2 字段3 字段4
aa 1 xx xx
cc 1 .. ..
ff 1 .. ..
请问如何些sql??
恳请高手指点,谢谢~~!在线等候!!
...全文
686打赏收藏
求教一个查询的 sql语句
表中记录如下, 字段1 字段2 字段3 字段4 aa 1 xx xx aa 5 xx xx bb 1 .. .. cc 1 .. .. cc 5 .. .. dd 1 .. .. ee 1 .. .. ff 1 .. .. ff 5 .. .. 希望查询出的记录集,如下: 字段1 字段2 字段3 字段4 aa 1 xx xx cc 1 .. .. ff 1 .. .. 请问如何些sql?? 恳请高手指点,谢谢~~!在线等候!!
select * from yourtable o
where 字段2 in
(
select 字段2 from yourtable i where i.字段1=o.字段1
and i.字段2 =(select min(字段2) from yourtable
where 字段1=i.字段1 group by 字段1)
group by i.字段2 having count(i.字段1)>1
)
SELECT IDENTITY(int, 1,1) AS ID_Num
INTO #NewTable
FROM (select * from YouTable
where 字段1 in(select 字段1 from YourTable
group by 字段1
having count(*) > 1)) as b
select * from #NewTable
where ID_Num in (select min(ID_Num) from #NewTable
group by 字段1)
SELECT IDENTITY(int, 1,1) AS ID_Num
INTO #NewTable
FROM (select * from YouTable
where 字段1 in(select 字段1 from YourTable
group by 字段1
having count(*) > 1)as a) as b
select * from #NewTable
where ID_Num in (select min(ID_Num) from #NewTable
group by 字段1)