求条sql语句 急等

我2我骄傲 2012-08-22 02:50:22
A B
1 x1
1 x2
2 x3
2 x4
2 xxx5
3 xxx
3 xxxx

表中有个字段 A B 有多条记录
我只想要期中的第一条

得到的结果是

A B
1 x1
2 x3
3 xxxx
这个SQL怎么写
...全文
163 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaozhanger 2012-08-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

with
cte as (
select rn=row_number() over(partition by A order by B),* from tab
)
select * from cte where rn=1
s……
[/Quote]

我看不懂。


發糞塗牆 2012-08-22
  • 打赏
  • 举报
回复
SELECT * FROM test a 
WHERE EXISTS (SELECT 1 FROM
(SELECT A,NTILE(1) OVER(ORDER BY A) groups
FROM test) b WHERE a.A=b.A AND b.groups=1)
w87875251l 2012-08-22
  • 打赏
  • 举报
回复
select A,min(B) from @test
group by A
以学习为目的 2012-08-22
  • 打赏
  • 举报
回复
-->测试数据
IF OBJECT_ID('tab') IS NOT NULL
DROP TABLE tab
GO
CREATE TABLE tab(A INT,B VARCHAR(10))
INSERT INTO tab

SELECT '1','x1' UNION ALL
SELECT '1','x2' UNION ALL
SELECT '2','x3' UNION ALL
SELECT '2','x4' UNION ALL
SELECT '2','xxx5' UNION ALL
SELECT '3','xxx' UNION ALL
SELECT '3','xxxx'
GO
--select * from tab
-->查询
;
with
cte as (
select rn=row_number() over(partition by A order by B),* from tab
)
select * from cte where rn=1

/*
A B
1 x1
2 x3
3 xxx
*/
以学习为目的 2012-08-22
  • 打赏
  • 举报
回复

-->测试数据
IF OBJECT_ID('tab') IS NOT NULL
DROP TABLE tab
GO
CREATE TABLE tab(A INT,B VARCHAR(10))
INSERT INTO tab

SELECT '1','x1' UNION ALL
SELECT '1','x2' UNION ALL
SELECT '2','x3' UNION ALL
SELECT '2','x4' UNION ALL
SELECT '2','xxx5' UNION ALL
SELECT '3','xxx' UNION ALL
SELECT '3','xxxx'
GO
--select * from tab
-->查询
;
with
cte as (
select rn=row_number() over(partition by A order by B),* from tab
)
select * from cte where rn=1

/*
A B
1 x1
2 x3
3 xxx
*/
我2我骄傲 2012-08-22
  • 打赏
  • 举报
回复
outer apply 也行。
我2我骄傲 2012-08-22
  • 打赏
  • 举报
回复
可以了 谢谢 马上结贴。
ORAClE SE 2012-08-22
  • 打赏
  • 举报
回复
我顶。。。。declare @test table(A int, B varchar(4))
insert into @test
select 1, 'x1' union
select 1, 'x2' union
select 2, 'x3' union
select 2, 'x4' union
select 2, 'xxx5' union
select 3, 'xxx' union
select 3, 'xxxx'
select A,B from
(
select row_number() over(partition by A order by A) rn,* from @test
) t
where t.rn=1

/*
A B
----------- ----
1 x1
2 x3
3 xxx
*/
  • 打赏
  • 举报
回复
declare @test table(A int, B varchar(4))
insert into @test
select 1, 'x1' union
select 1, 'x2' union
select 2, 'x3' union
select 2, 'x4' union
select 2, 'xxx5' union
select 3, 'xxx' union
select 3, 'xxxx'
select A,B from
(
select row_number() over(partition by A order by A) rn,* from @test
) t
where t.rn=1

/*
A B
----------- ----
1 x1
2 x3
3 xxx
*/
wanshichen 2012-08-22
  • 打赏
  • 举报
回复
select * from tb a where b in(select top 1 b from tb where id=a.id)
gw6328 2012-08-22
  • 打赏
  • 举报
回复
;with cte as (
select rn=row_number() over(partition by a order by getdate()),* from t
)
select * from t where rn=1;
我2我骄傲 2012-08-22
  • 打赏
  • 举报
回复
能不用Cross Apply吗??? Cross Apply 好像是inner join 吧。

有其他方法吗
我腫了 2012-08-22
  • 打赏
  • 举报
回复

SELECT a.A,b.B FROM t1 AS a
CROSS APPLY(SELECT TOP 1 B FROM t1 AS x WHERE x.A=a.A) AS b
GROUP BY a.A,b.B
ORDER BY a.A
我2我骄傲 2012-08-22
  • 打赏
  • 举报
回复
得到的结果
A B
1 x1
2 x3
3 xxx

我多打了个X

这怎么写?

老白猫 2012-08-22
  • 打赏
  • 举报
回复
对阿 这啥规律呢
我2我骄傲 2012-08-22
  • 打赏
  • 举报
回复
是的,这怎么写?
wanshichen 2012-08-22
  • 打赏
  • 举报
回复
得到的结果是

A B
1 x1
2 x3
3 xxxx

如果是要其中第一条的话
3应该是
3 xxx 吧?

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧