求一个SQL句子,看看能不能实现这个要求?

youzhj 2012-03-02 02:11:38
表tb
PrNum sType
1 ASN2
2 ASN3
3 KYN28A

以上数据只是例子,实际中可能只有其中任意一行或任意两行,也可能三行都有,但最多只有这3行。

问题:
如果只有任意一行的话(比如第2行),想得到如下结果
PrNum sType
0 ASN2
2 ASN3
0 KYN28A
如果只有任意两行的话(比如第1行和第3行),想得到如下结果
PrNum sType
1 ASN2
0 ASN3
3 KYN28A


实际中,事先是不知道到底有哪几行的,反正就是要得到3种类型(sType)对应的数量,没有的就直接置为0.
...全文
144 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
youzhj 2012-03-02
  • 打赏
  • 举报
回复
实在不好意思,我的表其实也是通过一个复杂的SQL句子查询出来的结果集,所以觉得麻烦。[Quote=引用 11 楼 hermanyoung 的回复:]
@tb是测试的表,你数据库中应该有这张表,上面主要是测试用的,代码就下面这点:
select * from
(select 0 as prnum,'ASN2' as stype union all
select 0 as prnum,'ASN3' as stype union all
select 0 as prnum,'KYN28A' as stype) as temp
whe……
[/Quote]
无名小猿 2012-03-02
  • 打赏
  • 举报
回复
@tb是测试的表,你数据库中应该有这张表,上面主要是测试用的,代码就下面这点:
select * from
(select 0 as prnum,'ASN2' as stype union all
select 0 as prnum,'ASN3' as stype union all
select 0 as prnum,'KYN28A' as stype) as temp
where stype not in(select stype from @tb) union all select * from @tb order by stype
youzhj 2012-03-02
  • 打赏
  • 举报
回复
还是一样的方法,有点复杂。
无名小猿 2012-03-02
  • 打赏
  • 举报
回复
可以不需要,这样写,
declare @tb table(prnum int,stype varchar(10))
insert into @tb
select 1,'ASN2' union all
select 2,'ASN3' union all
select 3,'KYN28A'

select * from
(select 0 as prnum,'ASN2' as stype union all
select 0 as prnum,'ASN3' as stype union all
select 0 as prnum,'KYN28A' as stype) as temp
where stype not in(select stype from @tb) union all select * from @tb order by stype
youzhj 2012-03-02
  • 打赏
  • 举报
回复
是这个意思,但是一定要借助变量表么?还有没有别的办法啊。[Quote=引用 6 楼 hermanyoung 的回复:]
declare @tb table(prnum int,stype varchar(10))
insert into @tb
select 1,'ASN2' union all
--select 2,'ASN3' union all
select 3,'KYN28A'

declare @temp table(prnum int,stype varchar(10))
insert ……
[/Quote]
无名小猿 2012-03-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hermanyoung 的回复:]
declare @tb table(prnum int,stype varchar(10))
insert into @tb
select 1,'ASN2' union all
--select 2,'ASN3' union all
select 3,'KYN28A'

declare @temp table(prnum int,stype varchar(10))
insert ……
[/Quote]楼主可否能满足你的条件
无名小猿 2012-03-02
  • 打赏
  • 举报
回复
declare @tb table(prnum int,stype varchar(10))
insert into @tb
select 1,'ASN2' union all
--select 2,'ASN3' union all
select 3,'KYN28A'

declare @temp table(prnum int,stype varchar(10))
insert into @temp
select 0,'ASN2' union all
select 0,'ASN3' union all
select 0,'KYN28A'

select * from @temp where stype not in(select stype from @tb) union all select * from @tb order by stype

结果
1 ASN2
0 ASN3
3 KYN28A



youzhj 2012-03-02
  • 打赏
  • 举报
回复
没人理解我的意思么?
youzhj 2012-03-02
  • 打赏
  • 举报
回复
不是您这样的,我的意思:表里面两个字段,一个是类型sType,另一个是数量PrNum,其中类型只有3种(ASN2,ASN3,KYN28A),但是实际山我并不知道表里现在有几行数据,最好的情况是3行都有,即3种类型对应的数量都有,那么我和字节select出来就可以,但是它可能只有一行或者两行,那么我就要把表里实际有的select出来,同时还要加上没有的类型对应的数量,这样来凑够3行结果。[Quote=引用 1 楼 lhqdyy9 的回复:]
select isnull(B.PrNum,0) PrNum,A.sType
from
(
select
1 PrNum,'ASN2' sType
union
select
2,'ASN2'
union
select
3,'KYN28A'
) A left join
tb B on B.sType = A.sType
[/Quote]
youzhj 2012-03-02
  • 打赏
  • 举报
回复
不是,第一张表里的最多有3行记录,但是不知道到底几行数据。[Quote=引用 2 楼 travylee 的回复:]
楼主第一张表里的三行数据是存在的吗?也就是表tb固定,数据也都存在,然后另一个表里存在表tb里的一行或者任意两行??是这个意思么
[/Quote]
  • 打赏
  • 举报
回复
楼主第一张表里的三行数据是存在的吗?也就是表tb固定,数据也都存在,然后另一个表里存在表tb里的一行或者任意两行??是这个意思么
老猫五号 2012-03-02
  • 打赏
  • 举报
回复

select isnull(B.PrNum,0) PrNum,A.sType
from
(
select
1 PrNum,'ASN2' sType
union
select
2,'ASN2'
union
select
3,'KYN28A'
) A left join
tb B on B.sType = A.sType
基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制问题,并提供完整的Matlab代码实现。文章结合数据驱动方法与Koopman算子理论,利用递归神经网络(RNN)对非线性系统进行建模与线性化处理,从而提升纳米级定位系统的精度与动态响应性能。该方法通过提取系统隐含动态特征,构建近似线性模型,便于后续模型预测控制(MPC)的设计与优化,适用于高精度自动化控制场景。文中还展示了相关实验验证与仿真结果,证明了该方法的有效性和先进性。; 适合人群:具备一定控制理论基础和Matlab编程能力,从事精密控制、智能制造、自动化或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能控制设计;②为非线性系统建模与线性化提供一种结合深度学习与现代控制理论的新思路;③帮助读者掌握Koopman算子、RNN建模与模型预测控制的综合应用。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现流程,重点关注数据预处理、RNN结构设计、Koopman观测矩阵构建及MPC控制器集成等关键环节,并可通过更换实际系统数据进行迁移验证,深化对方法泛化能力的理解。

34,872

社区成员

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

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