1

这是一段不起作用的 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 语句中使用了一个子查询,并将其命名为“已认证”。然后我尝试在 WHERE 子句中使用该值。

有人可以建议和替代方法来实现这一点吗?

非常感谢,马特

4

3 回答 3

1

You can not use an aliased (calculated) column in a WHERE clause. I would create another subquery to add criteria to that field.

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

I also tried to clean up the query a bit. You were using LEFT OUTER JOINs with criteria on the right had side table, so it was truly an INNER JOIN (INNER JOINs will work more efficiently / quickly). Check it out and let me know if this works for you as expected.

;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
于 2013-07-26T14:41:57.233 回答
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'
于 2013-07-26T15:08:48.410 回答
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 )
于 2013-07-26T14:38:52.003 回答