Sql server Sql server是否按组加入?
我有这张桌子:Sql server Sql server是否按组加入?,sql-server,tsql,join,left-join,dataexplorer,Sql Server,Tsql,Join,Left Join,Dataexplorer,我有这张桌子: id | type | date 1 | a | 01/1/2012 2 | b | 01/1/2012 3 | b | 01/2/2012 4 | b | 01/3/2012 5 | a | 01/5/2012 6 | b | 01/5/2012 7 | b | 01/9/2012 8 |
id | type | date
1 | a | 01/1/2012
2 | b | 01/1/2012
3 | b | 01/2/2012
4 | b | 01/3/2012
5 | a | 01/5/2012
6 | b | 01/5/2012
7 | b | 01/9/2012
8 | a | 01/10/2012
POV为每个日期。如果两行包含相同的日期,则这两行将在同一行中可见(左连接)
同一日期最多可由2行共享
所以这种情况不可能是:
如果在同一日期有组a
和b
使用左连接将它们显示在一行中
如果在日期中只有a
组,则将其显示为单行(+右侧为null)
如果在日期中只有b组,则将其显示为单行(+左侧为空)
预期结果:
Date |typeA|typeB |a'id|b'id
01/1/2012 | a | b | 1 | 2
01/2/2012 | | b | | 3
01/3/2012 | | b | | 4
01/5/2012 | a | b | 5 | 6
01/9/2012 | | b | | 7
01/10/2012 | a | | 8 |
我知道这个假设很简单,但是join的主要锚定是日期。
我遇到的问题是,当我阅读第1行时,我在表中搜索所有日期相同的行…很好。-没关系
但是当我读第二行的时候,我也读了,它产生了第一行——已经被计算过了
有什么帮助吗
下面是sql小提琴:
我想你需要一个
如果您想使用联接执行此操作
select ISNULL(a.date, b.date), a.type,b.type, a.id,b.id
from
(select * from yourtable where type='a') a
full outer join
(select * from yourtable where type='b') b
on a.date = b.date
我想你想要一个
如果您想使用联接执行此操作
select ISNULL(a.date, b.date), a.type,b.type, a.id,b.id
from
(select * from yourtable where type='a') a
full outer join
(select * from yourtable where type='b') b
on a.date = b.date
谢谢。有什么加入解决方案吗?哦,嘘**-你是怎么做到的。。。?。。。你太棒了。是什么触发了你对完全外接的思考?哦,真的,没有必要:)。我不知道为什么。我猜是你提到了左右。谢谢。有什么加入的解决方案吗?哦,嘘**-你是怎么做到的。。。?。。。你太棒了。是什么触发了你对完全外接的思考?哦,真的,没有必要:)。我不知道为什么。我猜是你提到了左右。
select ISNULL(a.date, b.date), a.type,b.type, a.id,b.id
from
(select * from yourtable where type='a') a
full outer join
(select * from yourtable where type='b') b
on a.date = b.date