我有2个表(这是从主表中检索数据)。例:
表格1
id GroupX Source GroupNum Amount
-------------------------------------------------------
1 23 School SH001 1 700
2 23 Bank BA001 2 300
3 23 Music MU001 3 500
4 23 School SH999 1 900
表2
id GroupNum SourceAmt
----------------------------------
1 23 1 700
2 23 2 100
3 23 3 500
4 23 1 900
我的困境是我使用的查询。它返回用于拆分值附加行(在表2中的通知“GroupNum”具有700和900的分割值)
我的结果应该是
id GroupX Source GroupNum Amount SourceAmt
-----------------------------------------------------------------
1 23 School SH001 1 700 700
2 23 Bank BA001 2 300 100
3 23 Music MU001 3 500 500
4 23 School SH999 1 900 900
而是我得到这个
id GroupX Source GroupNum Amount SourceAmt
-----------------------------------------------------------------
1 23 School SH001 1 700 700
2 23 School SH001 1 700 900
3 23 Bank BA001 2 300 100
4 23 Music MU001 3 500 500
5 23 School SH999 1 900 900
6 23 School SH999 1 900 700
这里是我的查询:
SELECT
t1.id,
t1.GroupX,
t1.Source,
t1.GroupNum,
t1.Amount,
t2.SourceAmt
FROM
table1 as t1
INNER JOIN
table2 as t2 ON t1.id = t2.id
AND t1.GroupNum = t2.GroupNum
WHERE
t1.id = 23
我已经用鲜明的,以及审判。援助将不胜感激。
如果我理解正确的话,你想加入Table 1和Table使得id
,GroupNum
和金额一致。如果事实确实如此,那么你就需要加入的数量为好,e.g,。:
Select t1.id, t1.Group, t1.Source, t1.GroupNum, t1.Amount, t2.SourceAmt
From table1 as t1 INNER JOIN
table2 as t2
ON t1.id = t2.id AND t1.GroupNum = t2.GroupNum AND t1.Amount = t2.SourceAmt
where id = 23
如果这不是你想要的,或者您不想使用的金额(例如,你不能保证你不会看到相同数量超过一次)的加入,那么你就有点难题的;你会注意到,(id
,GroupNum
)元组都没有在任一台独一无二的,因此您的加入是不是一个对一个。您可能要包括Source
table2
或以其他方式提供在表1一个transactionId
映射到在table2
一个唯一的ID列。
你需要一个额外的join
关键。没有明显的候选人,也许除了量 - 但我不知道这是你想要的东西。 SQL表表示无序套,所以不存在匹配的概念的“行号”。
您可以使用row_number()
分配行号。下面将做比赛,但你需要指定排序列:
Select t1.id, t1.Group, t1.Source, t1.GroupNum, t1.Amount, t2.SourceAmt
From (select t1.*,
row_number() over (partition by t1.id order by ?) as seqnum
from table1 t1
) t1 inner join
(select t2.*
row_number() over (partition by t1.id order by ?) as seqnum
from table2 t2
) t2
on t1.id = t2.id and t1.GroupNum = t2.GroupNum and
t1.seqnum = t2.seqnum
where id = 23 ;
该?
是每个表中的排序列。
我会选择从简单INNER JOIN
不同的方法,仅仅是因为你能做的事情有限,与结果集(这里的结果从内部设置连接)
我会做多个联接。
首先,我会用默认状态+ LEFT JOIN
where table1.[Amount] = table2.[SourceAmt]
。这会给我设置好[Amount]
和[SourceAmt]
相等
在此之后我用默认状态INNER JOIN
获得不匹配的金额
这里的查询
with t1 as
(
select 23 as [id], 'School' as [Group], 'SH001' as [Source], 1 as [GroupNum], 700 as [Amount]
union all
select 23, 'Bank', 'BA001', 2, 300
union all
select 23, 'Music', 'MU001', 3, 500
union all
select 23, 'School', 'SH999', 1, 900
),
t2 as
(
select 23 as [id], 1 as [GroupNum], 700 as [SourceAmt]
union all
select 23, 2, 100
union all
select 23, 3, 500
union all
select 23, 1, 900
)
select t1.*, a.*, b.*
from t1
left join t2 as a on
t1.[id] = a.[id]
and t1.[GroupNum] = a.[GroupNum]
and t1.[Amount] = a.[SourceAmt]
inner join t2 as b on
t1.[id] = b.[id]
and t1.[GroupNum] = b.[GroupNum]
where t1.[id] = 23
而这里的结果集,你可以检查
现在,我用了这个结果,我的前结果居然和做的小动作与CASE
和[takeIt]
列,这里的最终查询
with t1 as
(
select 23 as [id], 'School' as [Group], 'SH001' as [Source], 1 as [GroupNum], 700 as [Amount]
union all
select 23, 'Bank', 'BA001', 2, 300
union all
select 23, 'Music', 'MU001', 3, 500
union all
select 23, 'School', 'SH999', 1, 900
),
t2 as
(
select 23 as [id], 1 as [GroupNum], 700 as [SourceAmt]
union all
select 23, 2, 100
union all
select 23, 3, 500
union all
select 23, 1, 900
),
res as
(
select t1.[id],
t1.[Group],
t1.[Source],
t1.[GroupNum],
t1.[Amount],
isnull(a.[SourceAmt], b.[SourceAmt]) as [SourceAmt],
case when a.[SourceAmt] is null or a.[SourceAmt] = b.[SourceAmt] then 1
else 0
end as [takeIt]
from t1
left join t2 as a on
t1.[id] = a.[id]
and t1.[GroupNum] = a.[GroupNum]
and t1.[Amount] = a.[SourceAmt]
inner join t2 as b on
t1.[id] = b.[id]
and t1.[GroupNum] = b.[GroupNum]
where t1.[id] = 23
)
select [id],
[Group],
[Source],
[GroupNum],
[Amount],
[SourceAmt]
from res
where [takeIt] = 1