求一个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.
...全文
128 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

34,588

社区成员

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

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