我正在尝试连接两个具有字符键的表,我需要使用SELECT查询来形成一个连接条件。这是数据:
Table M
MKey OtherData
---- ---------
ABCD kjjh
EFGH oioo
IJKL uhdjdhu
MNOP isdid
QRST lkuh
Table FUN
FUNKey BinaryField
------ -----------
ABCD/GGG 1
ABCD/KKK 0
ABCD/MMM 1
EFGH/ABC 1
IJKL/DDD 1
IJKL/VVV 1
IJKL/XXX 0
MNOP/AAA 1
NMOP/DEF 1
NMOP/FFF 1
QRST/SSS 0
两个表中都没有唯一或数字字段,也没有其他字段可以加入。
这是我使用JOIN开始的查询:
Select MKey,Otherdata from M
LEFT OUTER JOIN FUN
ON M.MKey = left(FUNKey, CHARINDEX ('/', FUNKey) -1)
AND FUN.BinaryData=1
Returns
ABCD kjjh
ABCD kjjh
EFGH oioo
IJKL uhdjdhu
ABCD kjjh
MNOP isdid
MNOP isdid
MNOP isdid
MNOP isdid
我需要它返回的是M中的一行,其中FUN中有一行以“MKey”开头并且FUN.BinaryData = 1,如下所示:
ABCD kjjh
EFGH oioo
IJKL uhdjdhu
MNOP isdid
我在FUN.FUNKey列上尝试过GROUP BY,MIN,MAX但没有成功。
如果我使用自己使用ROW_NUMBER,OVER和PARTITION等的查询,我会得到我想加入的内容:
(Select M_Code from
(Select left(F.FUNKey, CHARINDEX ('/', F.FUNKey) -1) as M_Code
,row_number () over (partition by left(F.FUNKey, CHARINDEX ('/', F.FUNKey) -1)
order by left(F.FUNKey, CHARINDEX ('/', F.FUNKey) -1))
as rn
from FUN F
where F.BinaryData=1
) tmp
Where rn=1
)
返回:
ABCD
EFGH
IJKL
MNOP
所以我觉得我很好,直到我尝试用它来加入:
The following code gives me:
"Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as an
expression."
Select MKey,Otherdata from M
LEFT OUTER JOIN ON M.MKEY =
(Select M_Code from
(Select left(F.FUNKey, CHARINDEX ('/', F.FUNKey) -1) as M_Code
,row_number () over (partition by left(F.FUNKey, CHARINDEX ('/', F.FUNKey) -1)
order by left(F.FUNKey, CHARINDEX ('/', F.FUNKey) -1))
as rn
from FUN F
where F.BinaryData=1
) tmp
Where rn=1
)
是否有一个简单的解决方案,在我缺少的连接中使用SELECT的结果,或者我是否尝试将其除以零(再次)?
环境是MS SQL 20012
答案 0 :(得分:0)
有点不清楚你想要完成什么,但也许这就是你想要的:
SELECT MKey, Otherdata
FROM M
LEFT OUTER JOIN (
SELECT DISTINCT
LEFT(FUNKey, CHARINDEX ('/', FUNKey) -1) FUNKEY
, BinaryData
FROM FUN
) F ON M.MKey = F.FUNKEY AND F.BinaryData = 1;
这将返回:
MKey Otherdata
-------------------- --------------------
ABCD kjjh
EFGH oioo
IJKL uhdjdhu
MNOP isdid
QRST lkuh
如果您不想要最后一行(具有BinaryData = 0),则将连接条件中的AND更改为WHERE。
答案 1 :(得分:0)
declare @Table1 TABLE
([MKey] varchar(4), [OtherData] varchar(7))
;
INSERT INTO @Table1
([MKey], [OtherData])
VALUES
('ABCD', 'kjjh'),
('EFGH', 'oioo'),
('IJKL', 'uhdjdhu'),
('MNOP', 'isdid'),
('QRST', 'lkuh')
;
declare @Table2 TABLE
([FUNKey] varchar(8), [BinaryField] int)
;
INSERT INTO @Table2
([FUNKey], [BinaryField])
VALUES
('ABCD/GGG', 1),
('ABCD/KKK', 0),
('ABCD/MMM', 1),
('EFGH/ABC', 1),
('IJKL/DDD', 1),
('IJKL/VVV', 1),
('IJKL/XXX', 0),
('MNOP/AAA', 1),
('NMOP/DEF', 1),
('NMOP/FFF', 1),
('QRST/SSS', 0)
;
;with CTE AS(
select A.MKey,
A.OtherData,
BinaryField,
ROW_NUMBER()OVER(PARTITION BY OtherData ORDER BY (SELECT NULL) )RN from (
Select MKey,Otherdata,BinaryField
from @Table1 M
LEFT OUTER JOIN @Table2 FUN
ON M.MKey = left(FUNKey, CHARINDEX ('/', FUNKey) -1)
AND FUN.BinaryField=1)A )
Select MKey,
OtherData
from CTE
where RN = 1
AND BinaryField IS NOT NULL
ORDER BY Mkey
答案 2 :(得分:0)
我认为你们所有人都在努力实现这一目标
Select distinct M.MKey, M.Otherdata
from M
JOIN FUN
ON M.MKey = left(FUNKey, CHARINDEX ('/', FUNKey) -1)
AND FUN.BinaryData = 1