在 VIEW 中编写此特定 SELECT 查询的更好方法

发布于 2024-12-17 14:14:50 字数 949 浏览 0 评论 0原文

我想知道是否有更好的方法在下面的查询中编写 SELECT 子句。 status 存在三种可能的情况。它的值可以是“A”或“N”或为空。

我认为,当您将子表值与“A”值进行比较时,将 AND status = 'A' 放在 where 子句中是多余的。有什么方法可以更有效地重写它吗?我觉得限制 WHERE 子句中的状态总是会给你一个“A”,因此执行 CASE 语句是毫无意义的。

ALTER VIEW dbo.st_review_status_vw AS
(
    SELECT c.st_id, c.ms_price_comp_fy, 
        CASE
            WHEN (SELECT status FROM 
                    (SELECT st_id, status, ms_price_comp_fy
                     FROM ms_price_comp
                     WHERE st_id = c.st_id 
                         AND ms_price_comp_fy = c.ms_price_comp_fy 
                         AND status = 'A'
                     GROUP BY st_id, status, ms_price_comp_fy)
                 AS subTable) = 'A' THEN 'C'
            ELSE 'I'
        END AS status,
        MAX(date_approved) AS date_completed
    FROM ms_price_comp AS c
    GROUP BY c.st_id, c.ms_price_comp_fy
)

I would like to know if there is a better way of writing the SELECT clause in the query below. There are three possible cases for status. It can either have a value of 'A' or 'N' or null.

I think that placing the AND status = 'A' in the where clause is redundant when you are comparing the subTable values to a value of 'A'. Is there any way that this could be rewritten more efficiently? I feel like restricting the status in the WHERE clause is always going to give you an 'A' and hence pointless to do the CASE statement.

ALTER VIEW dbo.st_review_status_vw AS
(
    SELECT c.st_id, c.ms_price_comp_fy, 
        CASE
            WHEN (SELECT status FROM 
                    (SELECT st_id, status, ms_price_comp_fy
                     FROM ms_price_comp
                     WHERE st_id = c.st_id 
                         AND ms_price_comp_fy = c.ms_price_comp_fy 
                         AND status = 'A'
                     GROUP BY st_id, status, ms_price_comp_fy)
                 AS subTable) = 'A' THEN 'C'
            ELSE 'I'
        END AS status,
        MAX(date_approved) AS date_completed
    FROM ms_price_comp AS c
    GROUP BY c.st_id, c.ms_price_comp_fy
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生戏 2024-12-24 14:14:50

试试这个

ALTER VIEW dbo.st_review_status_vw AS (
SELECT c.st_id, c.ms_price_comp_fy, 

    CASE WHEN MIN(status)  = 'A' THEN 'C'
    ELSE 'I'
    END AS status,

MAX(date_approved) AS date_completed
FROM ms_price_comp AS c
GROUP BY c.st_id, c.ms_price_comp_fy
)

try this

ALTER VIEW dbo.st_review_status_vw AS (
SELECT c.st_id, c.ms_price_comp_fy, 

    CASE WHEN MIN(status)  = 'A' THEN 'C'
    ELSE 'I'
    END AS status,

MAX(date_approved) AS date_completed
FROM ms_price_comp AS c
GROUP BY c.st_id, c.ms_price_comp_fy
)
谁许谁一生繁华 2024-12-24 14:14:50

这对我来说似乎是一样的

ALTER VIEW dbo.st_review_status_vw AS (
SELECT
        c.st_id
    ,   c.ms_price_comp_fy
    ,   CASE
            WHEN MIN(status) = 'A' THEN 'C'
            ELSE 'I'
        END AS status
    ,   MAX(date_approved) AS date_completed
FROM ms_price_comp AS c
GROUP BY c.st_id, c.ms_price_comp_fy
)

This looks to be the same to me

ALTER VIEW dbo.st_review_status_vw AS (
SELECT
        c.st_id
    ,   c.ms_price_comp_fy
    ,   CASE
            WHEN MIN(status) = 'A' THEN 'C'
            ELSE 'I'
        END AS status
    ,   MAX(date_approved) AS date_completed
FROM ms_price_comp AS c
GROUP BY c.st_id, c.ms_price_comp_fy
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文