我必须在我的SQL Server 2008上运行此查询。
我需要加入2个表格(Gasper_object
和Ticket
)并获得Gasper_object.id
也会显示的结果。
SELECT
TICKET.ACTION_CODE_KEY,
TICKET.OBJECT_KEY,
GASPER_OBJECT.ID,
CASE
WHEN ACTION_CODE_KEY IN (4,8) THEN 'OUT OF SERVICE'
WHEN ACTION_CODE_KEY IS NULL THEN 'IN SERVICE'
ELSE 'FAULTY SERVICE'
END "STATUS"
FROM
TICKET
INNER JOIN
GASPER_OBJECT ON ticket.OBJECT_KEY = GASPER_OBJECT.OBJECT_KEY
GROUP BY
ticket.object_key, GASPER_OBJECT.ID;
我该怎么办?
更新
这是我收到的完成任务
答案 0 :(得分:0)
在聚合查询中,不在group by
中的所有列都需要是聚合函数的参数。以下是至少应该运行的查询示例:
SELECT t.object_key, o.ID,
(CASE WHEN MAX(ACTION_CODE_KEY) IN (4, 8) THEN 'OUT OF SERVICE'
WHEN MAX(ACTION_CODE_KEY) IS NULL THEN 'IN SERVICE'
ELSE 'FAULTY SERVICE'
END) as STATUS
FROM TICKET t inner join
GASPER_OBJECT o
on t.OBJECT_KEY = o.OBJECT_KEY
group by t.object_key, o.ID;
你的问题没有解释你想做什么,所以这可能会或可能不会产生你期望的结果。
答案 1 :(得分:0)
只需将其余列添加到组中即可。
SELECT
TICKET.ACTION_CODE_KEY,
TICKET.OBJECT_KEY,
GASPER_OBJECT.ID,
CASE
WHEN ACTION_CODE_KEY IN (4,8) THEN 'OUT OF SERVICE'
WHEN ACTION_CODE_KEY IS NULL THEN 'IN SERVICE'
ELSE 'FAULTY SERVICE'
END "STATUS"
FROM
TICKET
INNER JOIN
GASPER_OBJECT ON ticket.OBJECT_KEY = GASPER_OBJECT.OBJECT_KEY
GROUP BY
TICKET.ACTION_CODE_KEY, ticket.object_key, GASPER_OBJECT.ID;