有人必须知道为什么会这样...
前两个脚本几乎完全相同,唯一的区别是一个是left join
,另一个是inner join
。但是它们都返回相同的确切行:
declare @calendar table
(
monthNum int
)
insert into @calendar
select 1 union
select 2 union
select 3 union
select 4
declare @whatjoins table
(
id varchar(3),
monthnum int,
total int
)
insert into @whatjoins
select 'aaa', 1, 400 union
select 'aaa', 2, 400 union
select 'aaa', 3, 400 union
select 'aaa', 4, 400 union
select 'bbb', 1, 500 union
select 'bbb', 3, 500 union
select 'bbb', 4, 999 union
select 'ccc', 1, 999 union
select 'ccc', 2, 999 union
select 'ccc', 4, 999
select
c.monthnum, w.id, w.monthnum, w.total
from @calendar c inner join @whatjoins w on
c.monthNum = w.monthnum
order by c.monthNum, w.monthnum, id
具有左联接的相同脚本返回相同的行数:
declare @calendar table
(
monthNum int
)
insert into @calendar
select 1 union
select 2 union
select 3 union
select 4
declare @whatjoins table
(
id varchar(3),
monthnum int,
total int
)
insert into @whatjoins
select 'aaa', 1, 400 union
select 'aaa', 2, 400 union
select 'aaa', 3, 400 union
select 'aaa', 4, 400 union
select 'bbb', 1, 500 union
select 'bbb', 3, 500 union
select 'bbb', 4, 999 union
select 'ccc', 1, 999 union
select 'ccc', 2, 999 union
select 'ccc', 4, 999
select
c.monthnum, w.id, w.monthnum, w.total
from @calendar c left join @whatjoins w on
c.monthNum = w.monthnum
order by c.monthNum, w.monthnum, id
下一个第3个脚本正确显示左联接。此脚本与上一个脚本之间的唯一区别是,我对插入内容进行了注释,以便@whatjoins
仅具有'ccc'。
declare @calendar table
(
monthNum int
)
insert into @calendar
select 1 union
select 2 union
select 3 union
select 4
declare @whatjoins table
(
id varchar(3),
monthnum int,
total int
)
insert into @whatjoins
--select 'aaa', 1, 400 union
--select 'aaa', 2, 400 union
--select 'aaa', 3, 400 union
--select 'aaa', 4, 400 union
--select 'bbb', 1, 500 union
--select 'bbb', 3, 500 union
--select 'bbb', 4, 999 union
select 'ccc', 1, 999 union
select 'ccc', 2, 999 union
select 'ccc', 4, 999
select
c.monthnum, w.id, w.monthnum, w.total
from @calendar c left join @whatjoins w on
c.monthNum = w.monthnum
order by c.monthNum, w.monthnum, id
我的主要问题:为什么前两个脚本返回相同的数据?
我的第二个问题:是否有类似的方法可以使用left join
来返回所有12行(10个匹配项和2行为null)?
这是我要寻找的结果:
1 bbb 1 500
2 NULL NULL NULL
3 bbb 3 500
4 bbb 4 999
1 ccc 1 999
2 ccc 2 999
3 NULL NULL NULL
4 ccc 4 999
1 aaa 1 400
2 aaa 2 400
3 aaa 3 400
4 aaa 4 400
答案 0 :(得分:0)
不是高效的,但是有效。
declare @calendar table
(
monthNum int
)
insert into @calendar
select 1 union
select 2 union
select 3 union
select 4
declare @whatjoins table
(
id varchar(3),
monthnum int,
total int
)
insert into @whatjoins
select 'aaa', 1, 400 union
select 'aaa', 2, 400 union
select 'aaa', 3, 400 union
select 'aaa', 4, 400 union
select 'bbb', 1, 500 union
select 'bbb', 3, 500 union
select 'bbb', 4, 999 union
select 'ccc', 1, 999 union
select 'ccc', 2, 999 union
select 'ccc', 4, 999
select
c.monthNum,w2.*
from @calendar c CROSS JOIN (SELECT DISTINCT id FROM @whatjoins w)w
LEFT JOIN @whatjoins w2 ON w2.id = w.id AND c.monthNum=w2.monthnum
ORDER BY id
唯一的是,我无法为此找到您要求的确切输出的订单
ORDER BY ROW_NUMBER() OVER(PARTITION BY c.monthNum ORDER BY c.monthNum)
这是我遇到的但仍然无效的
答案 1 :(得分:0)
在CROSS JOIN
之前需要LEFT JOIN
,因为您想为每个monthnum
重复所有id
:
select distinct c.monthNum, w.id, w1.monthnum, w1.total
from @whatjoins w cross join
@calendar c left join
@whatjoins w1
on w1.id = w.id and w1.monthnum = c.monthNum
order by w.id, c.monthNum;