1

我正在尝试进行查询,但这是不对的。

我想做的是申请加入以防万一

我的查询是

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+'% '

谁能帮忙??

4

2 回答 2

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

可能还有其他几种方法。如果您可以快速定义所涉及的表以及您想要在输出中使用的列,那将是最好的。

希望这可以帮助。

于 2012-12-27T19:55:04.267 回答
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+'% '
于 2012-12-27T19:58:42.340 回答