此查询的设计使每行具有唯一的NAME
值。
除非我引入SELECT CASE
语句来选择ROLENAME
,否则查询工作正常。添加后,我收到错误
没有为' a'。
的第4列指定列名
如何解决这个问题?
SELECT *
FROM
(SELECT
NAME, CREATE_DATE, OTHERNAME,
CASE
WHEN ID = 'test' THEN ROLENAME
END,
STATUS,
ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY NAME DESC) AS RowNumber
FROM
BUSRULES br1
INNER JOIN
BUSRULES br2 ON br1.ID_NUM = br2.ID_NUM
INNER JOIN
BUSRULES br3 ON br1.ID_NUM = br3.ID_NUM
INNER JOIN
dbo.MDI ON NAME = br3.VALUE_TXT
INNER JOIN
(PERSON
LEFT JOIN
OP_TYPE ON OP_CASE.ID_NUM = OP_TYPE.ID_NUM)
ON OP_ID_NUM = OCO_OP_ID_NUM
WHERE
br1.KEY_TXT = 'EVENT'
AND br1.VALUE_TXT = 'YES'
AND br2.ABR_KEY_TXT = 'LIST'
AND br2.ABR_VALUE_TXT = 'test'
AND br3.ABR_KEY_TXT = 'NAME') AS a
WHERE
a.RowNumber = 1
答案 0 :(得分:1)
尝试修改:
CASE WHEN ID = 'test'
THEN ROLENAME
END
进入:
CASE WHEN ID = 'test'
THEN ROLENAME
END AS ROLENAME
原因是因为当你使用CASE子句时,它没有名称,除非你定义它
答案 1 :(得分:0)
错误消息非常具体 - 您没有使用CASE
声明创建的列的列别名。
, CASE WHEN ID = 'test'
THEN ROLENAME
END AS SomeColumnName -- Add a column name alias here
学习实际读取错误消息中的单词非常重要。它们通常包含一些相关信息。了解在哪里查找您正在使用的DBMS的文档也很重要,因为它会告诉您CASE
的要求以及使用它的示例。