邹建,扬帆等强人请进,求一个SQL

mahongxi 2005-04-21 02:12:08
select v,bn from plsqltab_int

V BN
------ ---
1 a
2 a
3 a
7 b
8 b
9 b

想得到
a b -----------------> a,b 列名不实现也可
--- ----
1 7
2 8
3 9

要如何做呢? 在ORACLE中有一个ROWNUM可以帮上忙,但MSSQL中没有这个东东啊. 各位大哥帮想想办法吧.
...全文
269 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sjjf 2005-04-28
  • 打赏
  • 举报
回复
最顶层的关联作 左联接或者是右联接吧 我指我写的那个。
mahongxi 2005-04-28
  • 打赏
  • 举报
回复
To sjjf(水晶剑锋):
不好意思,好久没关注这个贴子了.不是一定是对半的,就是相合并成二列的一个表,如果对不齐,就NULL
sjjf 2005-04-21
  • 打赏
  • 举报
回复
to mahongxi 你强调 a 和 b 之间的约束关系 吗? 是否属于那种对半折取得意思?
sjjf 2005-04-21
  • 打赏
  • 举报
回复
//-- 修正一下 ,这回应该符合结果了
select * from (select A.f1 ,(select count(*) from t1 B where B.f2=A.f2 and b.f1<=A.f1) As rownum from t1 A ) T2 ,
(select A.f1 ,(select count(*) from t1 B where B.f2=A.f2 and b.f1<=A.f1) As rownum from t1 A ) T3
where t2.rownum = t3.rownum and t2.f1<>t3.f1 and t2.f1 > t3.f1
shan1119 2005-04-21
  • 打赏
  • 举报
回复
不懂。。。。。。。。。。
sjjf 2005-04-21
  • 打赏
  • 举报
回复
//-- sql 调试
//-- os: windows 2000
//-- sql调试器: access 2000
//-- 注释符号: //--
//--- 附加说明:
//-- 对数据的假定,
//-- 1.内容只有两项,a,和b,
//-- 2.a,和b的个数应该是一样的,(不一样也没有关系)
//-- 3. 任意一个b的数据对应的id必须比 任意的a的 id大。
//-- 否则做这样的查询会有二义性。
//-- 还有些没有完成下班了。明天再来写



create table t1
(
f1 int,
f2 char(10)
)

insert into t1 values(1,'a')
insert into t1 values(2,'a')
insert into t1 values(3,'a')
insert into t1 values(4,'a')

insert into t1 values(6,'b')
insert into t1 values(7,'b')
insert into t1 values(8,'b')


select * from (select A.f1 ,(select count(*) from t1 B where B.f2=A.f2 and b.f1<=A.f1) As rownum from t1 A ) T2 ,
(select A.f1 ,(select count(*) from t1 B where B.f2=A.f2 and b.f1<=A.f1) As rownum from t1 A ) T3
where t2.rownum = t3.rownum and t2.f1<>t3.f1
xuhongying8848 2005-04-21
  • 打赏
  • 举报
回复
select V=1,BN='a' into # union all
select V=2,BN='a' union all
select V=3,BN='a' union all
select V=7,BN='b' union all
select V=8,BN='b' union all
select V=9,BN='b'
--------------------------------------

这个是建临时表吗?

看不懂,能帮忙解释下语法吗?

还有这个union all 看了半天联机丛书,还是搞不清楚。



xuhongying8848 2005-04-21
  • 打赏
  • 举报
回复
邹建 强人。

实在是佩服,仰慕中。
wxylvmnn 2005-04-21
  • 打赏
  • 举报
回复
(邹建) ......
paoluo 2005-04-21
  • 打赏
  • 举报
回复
试着写了下,用临时表也可以实现的,不过还是老大的方法好。
mahongxi 2005-04-21
  • 打赏
  • 举报
回复
谢谢 zjcxc(邹建) , 这种SQL,我把脑袋鳖块了也想不去来啊.
to csdnzm(明飞) :
如果不是a b
case BN when @a then V end
  • 打赏
  • 举报
回复
你的BN列的内容只有a,b么,有其他的值怎么办?
zjcxc 元老 2005-04-21
  • 打赏
  • 举报
回复
用子查询实现ROWNUM的功能

如果同一BN,V的值不唯一,同需要其他可以唯一确定一条记录的列,如果没有,则改用临时表
zjcxc 元老 2005-04-21
  • 打赏
  • 举报
回复
--测试数据
select V=1,BN='a' into # union all
select V=2,BN='a' union all
select V=3,BN='a' union all
select V=7,BN='b' union all
select V=8,BN='b' union all
select V=9,BN='b'
go

--查询
select a=min(case BN when 'a' then V end),
b=min(case BN when 'b' then V end)
from(select *,gid=(select count(*) from # where BN=a.BN and V<=a.V) from # a)a
group by gid
go

--删除测试
drop table #

/*--结果
a b
----------- -----------
1 7
2 8
3 9

(所影响的行数为 3 行)
--*/

34,664

社区成员

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

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