我正在尝试进行查询,但这是不对的。
我想要做的是在案例中应用连接
我的查询是
SELECT LEFT(Student_First_Name,LEN(Student_First_Name)-LEN(Name_Lookup_Table.Dirty_Name)),Name_Lookup_Table.Dirty_Name, Name_Lookup_Table.Standard_Name
case
when Transformed_All_Student.Student_First_Name like '% '+Name_Lookup_Table.Dirty_Name
then
from Transformed_All_Student left join Name_Lookup_Table
on Transformed_All_Student.Student_First_Name like '% '+Name_Lookup_Table.Dirty_Name
when Transformed_All_Student.Student_First_Name like '% '+Name_Lookup_Table.Dirty_Name+'%'
then from Transformed_All_Student left join Name_Lookup_Table on Transformed_All_Student.Student_First_Name like '% '+Name_Lookup_Table.Dirty_Name+'%'
when Transformed_All_Student.Student_First_Name like Name_Lookup_Table.Dirty_Name+'% '
then from Transformed_All_Student left join Name_Lookup_Table on Transformed_All_Student.Student_First_Name like Name_Lookup_Table.Dirty_Name+'% '
任何人都可以帮忙吗?
答案 0 :(得分:1)
由于你试图以这种方式做事,我认为表现不是主要关注点。此外,我假设这是用于MS SQL,因为没有另外指定。一般形式如下:
SELECT 1
FROM foo f
JOIN bar b
ON CASE WHEN f.col1 = 'X' then 'Y' ELSE END = b.col1
获得相同结果的另一种可能方法是使用子查询:
SELECT 1
FROM (
SELECT *, CASE WHEN f.col1 = 'X' then 'Y' ELSE END JoinCol
FROM foo
) f
JOIN bar b
ON f.JoinCol = b.col1
可能还有其他一些方法。如果您可以快速定义所涉及的表以及输出中所需的列,那将是最好的。
希望这有帮助。
答案 1 :(得分:1)
我不认为你可以把FROM放在什么时候。一种可能的解决方案是使用UNION。你可能可以优化这个:
SELECT LEFT(Student_First_Name
, LEN(Student_First_Name)-LEN(nlt.Dirty_Name))
, nlt.Dirty_Name
, nlt.Standard_Name
FROM Transformed_All_Student tas
LEFT JOIN Name_Lookup_Table nlt
ON tas.Student_First_Name like '% '+nlt.Dirty_Name
WHERE tas.Student_First_Name like '% '+nlt.Dirty_Name
UNION
SELECT LEFT(Student_First_Name
, LEN(Student_First_Name)-LEN(nlt.Dirty_Name))
, nlt.Dirty_Name
, nlt.Standard_Name
FROM Transformed_All_Student tas
LEFT JOIN Name_Lookup_Table nlt
ON tas.Student_First_Name like '% '+nlt.Dirty_Name+'%'
WHERE tas.Student_First_Name like '% '+nlt.Dirty_Name+'%'
UNION
SELECT LEFT(Student_First_Name
, LEN(Student_First_Name)-LEN(nlt.Dirty_Name))
, nlt.Dirty_Name
, nlt.Standard_Name
FROM Transformed_All_Student tas
LEFT JOIN Name_Lookup_Table nlt
ON tas.Student_First_Name like nlt.Dirty_Name+'% '
WHERE tas.Student_First_Name like nlt.Dirty_Name+'% '