高分请教一个sql语句的写法,在线等待

lixueming3000 2008-09-18 02:25:27

//表结构
CREATE TABLE [dbo].[test_tb](
[id] [int] IDENTITY(1,1) NOT NULL,
[height] [int] NULL,
[width] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[pvt] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
//测试数据
insert into test_tb
select 0,'5.11','6.11'
union all
select 0,'5.11','6.11'
union all
select 0,'5.11','6.11'
union all
select 0,'5.11','6.11'
union all
select 0,'5.11','6.11'
union all
select 1,'5.22','6.22'
union all
select 1,'5.22','6.22'
union all
select 1,'5.22','6.22'
union all
select 1,'5.22','6.22'
union all
select 1,'5.22','6.22'
union all
select 2,'5.33','6.33'

//要的结果
height width pvt
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
3 2 5.33 6.33

说明一下这个结果的由来,首先按升序取一条(给他加上一个新的ID为1),然后判断除id列表的其它列,如果相同的话,相同的显示在一起
显示结果中最新的列是根据条件自动添加的,还有就是真实的数据中表中的id可能是不连续的
...全文
246 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
lixueming3000 2008-09-18
  • 打赏
  • 举报
回复
经过实现数据测试happyflystone,Haiwer符合要求,而且happyflystone的适合不同的字段类型,lgxyz用了临时表,也是一种解决问题的思路,在此感谢各位,结贴给分.
等不到来世 2008-09-18
  • 打赏
  • 举报
回复

select nid=(select count(distinct height) from test_tb where height<=a.height),a.height,a.width,a.pvt from test_tb a

/*
--------------------------------------
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
3 2 5.33 6.33
*/
昵称被占用了 2008-09-18
  • 打赏
  • 举报
回复
估计他要按照 width pvt
排序,不是按照height和id


CREATE TABLE [dbo].[test_tb](
[id] [int] IDENTITY(1,1) NOT NULL,
[height] [int] NULL,
[width] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[pvt] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
insert into test_tb
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 45,'5.33','6.33'
select (select count(*) from (select distinct width,pvt from test_tb where width<a.width or width=a.width and pvt<=a.pvt) as t) as id
,height,width,pvt from test_tb a order by width,pvt
go

--结果
id height width pvt
----------- ----------- ---------- ----------
1 50 5.11 6.11
1 50 5.11 6.11
1 50 5.11 6.11
1 50 5.11 6.11
1 50 5.11 6.11
2 70 5.22 6.22
2 70 5.22 6.22
2 70 5.22 6.22
2 70 5.22 6.22
2 70 5.22 6.22
3 45 5.33 6.33

(所影响的行数为 11 行)
lixueming3000 2008-09-18
  • 打赏
  • 举报
回复
qianjin036a你这个不是我想要的,Garnett_KG 和happyflystone 这个是我想要的,正在测试实际数据,感谢各位
lgxyz 2008-09-18
  • 打赏
  • 举报
回复

select Height,[width],[pvt],idno=identity(int,1,1) into #t from test_tb
group by Height,[width],[pvt] order by Height

select ID=idno,a.Height,a.width,a.pvt from test_tb a
left join #t b
on a.Height=b.Height and a.width=b.width and a.pvt=b.pvt

drop table #t

/*
ID Height width pvt
----------- ----------- ---------- ----------
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
3 2 5.33 6.33

(所影响的行数为 11 行)
*/
Garnett_KG 2008-09-18
  • 打赏
  • 举报
回复

--这样?
select (
SELECT COUNT(DISTINCT height)
FROM test_tb
WHERE height<=a.height
AND width<=a.width
AND pvt<=a.pvt
) as New_ID,
height,width,pvt
from test_tb a

/*
NewID height width pvt
----------------------------------------
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
3 2 5.33 6.33

*/

-晴天 2008-09-18
  • 打赏
  • 举报
回复
CREATE TABLE [dbo].[test_tb](
[id] [int] IDENTITY(1,1) NOT NULL,
[height] [int] NULL,
[width] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[pvt] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
insert into test_tb
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 45,'5.33','6.33'
select height+1,height,width,pvt from test_tb order by height
go
drop table test_tb
/*
height width pvt
----------- ----------- ---------- ----------
46 45 5.33 6.33
51 50 5.11 6.11
51 50 5.11 6.11
51 50 5.11 6.11
51 50 5.11 6.11
51 50 5.11 6.11
71 70 5.22 6.22
71 70 5.22 6.22
71 70 5.22 6.22
71 70 5.22 6.22
71 70 5.22 6.22

(11 行受影响)

*/
-晴天 2008-09-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lixueming3000 的回复:]
上面的二位,说的是因为这个表的测试数据,你们的那个才能实现的,实际中那样都是不对的
[/Quote]
那,实际的数据是怎么样的呢?
lixueming3000 2008-09-18
  • 打赏
  • 举报
回复
当然不一样了,那是因为测试数据相同的我都放在一起了,请细看
-狙击手- 2008-09-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lixueming3000 的回复:]
上面的二位,说的是因为这个表的测试数据,你们的那个才能实现的,实际中那样都是不对的
[/Quote]


[Quote=引用 7 楼 happyflystone 的回复:]
select pid = (select count(distinct CHECKSUM (height,width,pvt))
from test_tb where id <= a.id)
,*
from test_tb a
[/Quote]
lixueming3000 2008-09-18
  • 打赏
  • 举报
回复
insert into test_tb
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 50,'5.11','6.11'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 70,'5.22','6.22'
union all
select 45,'5.33','6.33'
yangkunjie 2008-09-18
  • 打赏
  • 举报
回复
感觉你表达的有问题,或者说表达和举例之间存在差距
cl9132008 2008-09-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 happyflystone 的回复:]
select pid = (select count(distinct height)
from test_tb where id <= a.id)
,*
from test_tb a
[/Quote]

确实如此!有待更正。。。
-狙击手- 2008-09-18
  • 打赏
  • 举报
回复
select pid = (select count(distinct CHECKSUM (height,width,pvt))
from test_tb where id <= a.id)
,*
from test_tb a
-晴天 2008-09-18
  • 打赏
  • 举报
回复
如果源表未排序,则
select height+1,height,width,pvt from test_tb order by height
lixueming3000 2008-09-18
  • 打赏
  • 举报
回复
上面的二位,说的是因为这个表的测试数据,你们的那个才能实现的,实际中那样都是不对的
fa_ge 2008-09-18
  • 打赏
  • 举报
回复
结果和表的数据不是一样的吗?看了几遍没看出什么不同,楼下继续
-狙击手- 2008-09-18
  • 打赏
  • 举报
回复
select pid = (select count(distinct height)
from test_tb where id <= a.id)
,*
from test_tb a
水族杰纶 2008-09-18
  • 打赏
  • 举报
回复
select IDnew=Height+1, Height,[width], [pvt] from [dbo].[test_tb]
???
-晴天 2008-09-18
  • 打赏
  • 举报
回复
CREATE TABLE [dbo].[test_tb](
[id] [int] IDENTITY(1,1) NOT NULL,
[height] [int] NULL,
[width] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[pvt] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
insert into test_tb
select 0,'5.11','6.11'
union all
select 0,'5.11','6.11'
union all
select 0,'5.11','6.11'
union all
select 0,'5.11','6.11'
union all
select 0,'5.11','6.11'
union all
select 1,'5.22','6.22'
union all
select 1,'5.22','6.22'
union all
select 1,'5.22','6.22'
union all
select 1,'5.22','6.22'
union all
select 1,'5.22','6.22'
union all
select 2,'5.33','6.33'
select height+1,height,width,pvt from test_tb
go
drop table test_tb
/*
(11 行受影响)
height width pvt
----------- ----------- ---------- ----------
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
1 0 5.11 6.11
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
2 1 5.22 6.22
3 2 5.33 6.33

(11 行受影响)
*/

是这样么?
内容概要:本报告基于寻汇与万事达卡在2026年联合发布的《超越自动化:定义智能体驱动的全球支付》白皮书,系统分析了AI智能体在B2B跨境支付领域的应用与发展。报告指出,传统跨境支付存在效率低、人工干预多、合规风险高等问题,当前正从数字化、数据化迈向“自主化”新阶段。AI智能体可在授权下自主完成支付、换汇、合规审核、对账等全流程操作,核心技术包括深度强化学习、自然语言处理和图神经网络,用于路径优化、合规解析与异常检测。报告揭示了决策可解释性不足、跨系统协同标准缺失、安全审计机制缺位三大研究空白,并探讨了法律责任归属、监管碎片化、数据主权与技术可靠性四大现实挑战。寻汇与万事达卡的合作构建了“智能体编排引擎”与全球合规决策网络,首次提出L0-L5的智能体自主化等级框架,推动行业标准化。预计2026至2027年将实现首批大规模商业部署,提升支付效率超30%。; 适合人群:金融科技研究人员、AI技术开发者、跨境支付行业从业者、企业财资管理人员及政策监管机构相关人员。; 使用场景及目标:①理解AI智能体在跨境支付中的技术架构与应用场景;②把握自主化支付的演进趋势与商业化前景;③为金融机构和技术公司布局AI驱动型支付系统提供战略参考;④助力监管机构制定适应智能体时代的合规框架。; 阅读建议:本报告兼具技术深度与产业视野,建议结合白皮书原文及相关技术文献对照研读,重点关注智能体决策逻辑、合规实现机制与跨系统集成方案,并关注后续试点项目的实际成效与监管反馈。

22,296

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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