如何在WHERE子句中使用SELECT ... AS

时间:2013-07-26 14:35:29

标签: sql sql-server sql-server-2008

这是一段不起作用的SQL代码:

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语句中使用子查询并将其命名为“certified”。然后我尝试在WHERE子句中使用该值。

有人可以建议和替代方法来实现这个目标吗?

非常感谢, 马特

3 个答案:

答案 0 :(得分: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 s,右侧有标准的边桌,因此它确实是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

答案 1 :(得分: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'

答案 2 :(得分: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 )