在 VIEW 中编写此特定 SELECT 查询的更好方法
我想知道是否有更好的方法在下面的查询中编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个
try this
这对我来说似乎是一样的
This looks to be the same to me