求教1SQL的问题,百分百结贴达人,跪求围观!

小金牛儿 2011-07-07 09:53:34
问大家个报表类型的SQL,我模拟了个数据结构,大家看看
Create table StudentInfo
(
id int primary key,
sname varchar(20)
)

INSERT INTO StudentInfo
SELECT 1,'Hello'


Create Table ReportOne
(
rid int identity primary key,
id int not null,
rnameOne varchar(20)
)

insert into ReportOne
select 1,'Hello的报表One'


Create Table ReportTwo
(
rid int identity primary key,
id int not null,
rnameTwo varchar(20)
)

insert into ReportTwo
select 1,'Hello的报表TwoA'
UNION ALL
select 1,'Hello的报表TwoB'


其中student表是主表,与ReportOne 和 ReportTwo 都是1对多的关系,也就是说 这俩个子表的里面可能没有主表的记录,可能有1条 ,也可能有多条,我想join起来形成1个新的数据集,我是这么写的,但有问题,sql如下:

SELECT St.sname,RTOne.rnameOne,RTTwo.rnameTwo FROM StudentInfo AS St
LEFT JOIN ReportOne AS RTOne ON ST.id = RTOne.id
LEFT JOIN ReportTwo as RTTwo ON ST.id = RTTwo.id

效果图:
sname rnameOne rnameTwo
Hello Hello的报表One Hello的报表TwoA
Hello Hello的报表One Hello的报表TwoB


由于ReportOne 里面只有一条记录,ReportTwo里面有俩条记录,所以ReportOne 中一条记录重复出现了,我想如果ReportOne 里少 1条记录的话,以空行代替,也就是说 ReportOne 有1 条数据 ,ReportTwo里有俩条数据的话,ReportOne的第二行 都是NULL。我希望的结果:

sname rnameOne rnameTwo
Hello Hello的报表One Hello的报表TwoA
Hello NULL Hello的报表TwoB

另外子表ReportOne 与 ReportTwo 可能也没有主表的值,也就是ReportOne或者ReportTwo可能为空~
...全文
147 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-07-08
  • 打赏
  • 举报
回复
create table t1(id int,col int)
create table t2(id int,col int)
insert into t1 select 1,10 union all select 2,20
insert into t2 select 2,40 union all select 3,50
go
select t1.id,t1.col,t2.id,t2.col from t1 full join t2 on t1.id=t2.id
/*
id col id col
----------- ----------- ----------- -----------
1 10 NULL NULL
2 20 2 40
NULL NULL 3 50

(3 行受影响)
*/
go
drop table t1,t2
小金牛儿 2011-07-08
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 cd731107 的回复:]
SQL code
SELECT Stu.sname,(case when Two.rid<>(select min(rid) from ReportTwo)
then null else one.rnameOne end)as [rnameOne],
rnameTwo
FROM StudentInfo AS Stu
……
[/Quote]

多谢回复 但这个貌似也是静态的,不是动态的 。。。
cd731107 2011-07-08
  • 打赏
  • 举报
回复
SELECT Stu.sname,(case when Two.rid<>(select min(rid) from ReportTwo) 
then null else one.rnameOne end)as [rnameOne],
rnameTwo
FROM StudentInfo AS Stu
FULL JOIN ReportOne as one on Stu.id = one.id
FULL JOIN ReportTwo AS Two on Stu.id = Two.id
小金牛儿 2011-07-08
  • 打赏
  • 举报
回复
感谢大家的响应 我在把问题 叙述的清楚点
Create table StudentInfo
(
id int primary key,
sname varchar(20)
)

INSERT INTO StudentInfo
SELECT 1,'Hello'
INSERT INTO StudentInfo
SELECT 2,'What'


Create Table ReportOne
(
rid int identity primary key,
id int not null,
rnameOne varchar(20)
)

insert into ReportOne
select 1,'Hello的报表One'
insert into ReportOne
select 2,'What的报表One'


Create Table ReportTwo
(
rid int identity primary key,
id int not null,
rnameTwo varchar(20)
)

insert into ReportTwo
select 1,'Hello的报表TwoA'
UNION ALL
select 1,'Hello的报表TwoB'

查询:
SELECT Stu.sname,one.rnameOne,rnameTwo FROM StudentInfo AS Stu
FULL JOIN ReportOne as one on Stu.id = one.id
FULL JOIN ReportTwo AS Two on Stu.id = Two.id

这个是查询结果

sname rnameOne rnameTwo
Hello Hello的报表One Hello的报表TwoA
Hello Hello的报表One Hello的报表TwoB
What What的报表One NULL


我希望得到结果:支持条件查询 例如 where sname = ‘Hello’ 或者 其他的

sname rnameOne rnameTwo
Hello Hello的报表One Hello的报表TwoA
Hello NULL Hello的报表TwoB
What What的报表One NULL
-晴天 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fox123871 的回复:]
student 与 reportOne 和 reportTwo 是1对多的 ,如果fullJoin的话 ,不是 student里的其他对应的数据也都出来了吗?
[/Quote]full join ,相等的对应出现,不相等的各自出现,其对面用NULL代替.
小金牛儿 2011-07-07
  • 打赏
  • 举报
回复
student 与 reportOne 和 reportTwo 是1对多的 ,如果fullJoin的话 ,不是 student里的其他对应的数据也都出来了吗?
叶子 2011-07-07
  • 打赏
  • 举报
回复
如果只有一个表中有的数据,你也要在结果集中出现的话,left join 改成full join
叶子 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fox123871 的回复:]
引用 2 楼 maco_wang 的回复:
SQL code

select sname,case rid when 1 then rnameOne else null end as rnameOne,rnameTwo from (
SELECT row_number() over(partition by RTOne.rnameOne order by RTOne.rnameOne)
……
[/Quote]
多条的话,就是一条显示正常,下面重复的就都处理成null了。
小金牛儿 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 maco_wang 的回复:]
SQL code

select sname,case rid when 1 then rnameOne else null end as rnameOne,rnameTwo from (
SELECT row_number() over(partition by RTOne.rnameOne order by RTOne.rnameOne)
as rid,
St.sname,RTOn……
[/Quote]

高手 我看懂你写的?我那个灵活些,俩个子表 不一定 有数据 或者没有 不一定是1条 也可能是多条~
小金牛儿 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 maco_wang 的回复:]
SQL code

select sname,case rid when 1 then rnameOne else null end as rnameOne,rnameTwo from (
SELECT row_number() over(partition by RTOne.rnameOne order by RTOne.rnameOne)
as rid,
St.sname,RTOn……
[/Quote]
SELECT row_number() over(partition by RTOne.rnameOne order by RTOne.rnameOne)
这个我知道
partition by RTOne.rnameOne 这句是什么意思呢 那个加行号的 我看懂了 ~
GoAwayZ 2011-07-07
  • 打赏
  • 举报
回复
多表join起来
叶子 2011-07-07
  • 打赏
  • 举报
回复

select sname,case rid when 1 then rnameOne else null end as rnameOne,rnameTwo from (
SELECT row_number() over(partition by RTOne.rnameOne order by RTOne.rnameOne)
as rid,
St.sname,RTOne.rnameOne,RTTwo.rnameTwo FROM StudentInfo AS St
LEFT JOIN ReportOne AS RTOne ON ST.id = RTOne.id
LEFT JOIN ReportTwo as RTTwo ON ST.id = RTTwo.id) aa
/*
sname rnameOne rnameTwo
-------------------- -------------------- --------------------
Hello Hello的报表One Hello的报表TwoA
Hello NULL Hello的报表TwoB
*/
-晴天 2011-07-07
  • 打赏
  • 举报
回复
围观.

34,588

社区成员

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

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