2013-07-26 121 views
1

下面是一段SQL代碼不起作用:如何使用SELECT ...如在WHERE子句

SELECT bl.regn_id, 
    RTRIM(LTRIM(dv.dv_id)) + '_' + RTRIM(LTRIM(bl.regn_id)) AS bu_regn, 
    (SELECT COUNT (em.em_id) 
     FROM em 
     LEFT OUTER JOIN bl bl_s ON em.bl_id = bl_s.bl_id 
     LEFT OUTER JOIN irs_self_cert_em ON em.em_id = irs_self_cert_em.em_id 
     WHERE dv.dv_id = em.dv_id 
     AND bl.bl_id = bl_s.bl_id 
     AND irs_self_cert_em.date_cert_loc >= DATEADD(month, -1, GETDATE()) 
     AND (em.date_last_update_cads >= (select date_last_update_completed FROM ddi_completed WHERE ddi_id='TRA_CADS_EM'))   
    ) AS certified 
FROM bl  
    CROSS JOIN dv 
    WHERE bl.status = 'A' AND (certified > 0) 

我收到錯誤:「查找錯誤 - SQL Server數據庫錯誤:無效的列名稱'認證'。「

正如你所看到的,我在SELECT語句中使用了一個子查詢,並給它起了'認證'這個名字。然後我嘗試在WHERE子句中使用該值。

有人可以建議和替代方法來完成這個?

非常感謝, 馬特

+0

您不能在'WHERE'子句中使用別名列。我會創建另一個子查詢來爲該字段添加標準。 – JoeFletch

+1

您將不得不再次包含整個SELECT –

+1

您無法在條件中使用列別名。 –

回答

1

不能在WHERE子句中使用別名(計算值)列。我會創建另一個子查詢來爲該字段添加標準。

SELECT * FROM 
(
    SELECT 
     bl.regn_id, 
     RTRIM(LTRIM(dv.dv_id)) + '_' + RTRIM(LTRIM(bl.regn_id)) AS bu_regn, 
     (
      SELECT 
       COUNT(em.em_id) AS [Count] 
      FROM 
       em LEFT OUTER JOIN bl AS bl_s 
        ON 
         em.bl_id = bl_s.bl_id LEFT OUTER JOIN irs_self_cert_em 
          ON 
           em.em_id = irs_self_cert_em.em_id 
      WHERE 
       dv.dv_id = em.dv_id 
        AND 
       bl.bl_id = bl_s.bl_id 
       AND 
        irs_self_cert_em.date_cert_loc >= DATEADD(month, -1, GETDATE()) 
       AND 
        (em.date_last_update_cads >= (select date_last_update_completed FROM ddi_completed WHERE ddi_id='TRA_CADS_EM'))   
     ) AS certified 
    FROM 
     bl CROSS JOIN dv 
    WHERE 
     bl.status = 'A' 
) AS temp 
    WHERE 
     certified > 0 

我也試圖清理查詢了一下。你使用的是LEFT OUTER JOIN,右邊有標準表,所以它確實是一個INNER JOININNER JOIN s會更有效/更快地工作)。檢查出來,讓我知道這是否符合您的預期。而不必表結構

;WITH 
cte AS 
(
    SELECT 
     bl.regn_id, 
     RTRIM(LTRIM(dv.dv_id)) + '_' + RTRIM(LTRIM(bl.regn_id)) AS bu_regn 
     COUNT(*) OVER (PARTITION BY bl.regn_id, dv.dv_id, bl.regn_id ORDER BY bl.regn_id) AS [Certified] 
    FROM 
     dv INNER JOIN em 
      ON 
       dv.dv_id = em.dv_id INNER JOIN JOIN bl 
        ON 
         em.bl_id = bl.bl_id INNER JOIN JOIN irs_self_cert_em 
          ON 
           em.em_id = irs_self_cert_em.em_id 
    WHERE 
     bl.status = N'A' 
      AND 
     irs_self_cert_em.date_cert_loc >= DATEADD(month, -1, GETDATE()) 
      AND 
     (em.date_last_update_cads >= (select date_last_update_completed FROM ddi_completed WHERE ddi_id='TRA_CADS_EM') 
) 
SELECT 
    DISTINCT 
    * 
FROM 
    cte 
WHERE 
    Certified > 0 
0
SELECT bl.regn_id, 
    RTRIM(LTRIM(dv.dv_id)) + '_' + RTRIM(LTRIM(bl.regn_id)) AS bu_regn, 
    S.certified 
FROM bl  
    CROSS JOIN dv 
    CROSS APPLY (SELECT COUNT (em.em_id) as certified 
     FROM em 
     LEFT OUTER JOIN bl bl_s ON em.bl_id = bl_s.bl_id 
     LEFT OUTER JOIN irs_self_cert_em ON em.em_id = irs_self_cert_em.em_id 
     WHERE dv.dv_id = em.dv_id 
     AND bl.bl_id = bl_s.bl_id 
     AND irs_self_cert_em.date_cert_loc >= DATEADD(month, -1, GETDATE()) 
     AND (em.date_last_update_cads >= (select date_last_update_completed FROM ddi_completed WHERE ddi_id='TRA_CADS_EM'))   
    ) AS S 
    WHERE bl.status = 'A' AND (S.certified > 0) 
1

(例如:知道哪個ID是唯一的)涉及到他們,你需要的輸出方式的一些數據,這是很難說和測試,但我的東西,看起來像這樣去:

SELECT bl.regn_id, 
    RTRIM(LTRIM(dv.dv_id)) + '_' + RTRIM(LTRIM(bl.regn_id)) AS bu_regn, 
    result.certified 
FROM bl  
INNER JOIN 
    (SELECT em.bl_id AS bl_id, COUNT (em.em_id) as certified 
     FROM em 
     LEFT OUTER JOIN bl bl_s ON em.bl_id = bl_s.bl_id 
     INNER JOIN irs_self_cert_em ON em.em_id = irs_self_cert_em.em_id 
       AND irs_self_cert_em.date_cert_loc >= DATEADD(month, -1, GETDATE()) 
    GROUP BY em.bl_id 
    HAVING COUNT (em.em_id) > 0 
    ) AS result ON result.bl_id = bl.bl_id 
INNER JOIN em ON result.bl_id = em.bl_id 
     AND (em.date_last_update_cads >= 
    (SELECT date_last_update_completed FROM ddi_completed WHERE ddi_id='TRA_CADS_EM')) 
CROSS JOIN dv ON dv.dv_id = em.dv_id  
WHERE bl.status = 'A'