2012-12-27 145 views
1

我想查詢,但它是不正確的。使用連接的情況下sql

我想要做的是申請加入的情況下

我查詢

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

誰能幫助?

回答

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

其工作完全正常:) :)三江源現在 – ssbb

+0

我想從學生名字修剪dity_name與Standard_Name更換。實際上,我有一個名爲「Name_Lookup_Table」的查找表,其中包含字段Standard_name和dirty_name ,如代碼 tas.Student_First_Name like'%'+ nlt.Dirty_Name – ssbb

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 

可能有一對夫婦的其他方法。如果你能給我們一個關於表的快速定義和你在輸出中想要的列,最好。

希望這會有所幫助。

+0

我使用SQL在Visual Studio 2012 – ssbb

相關問題