我有这个问题:
SELECT groupname,
Result = CASE
WHEN ( thd_requesttype_dca_oms_form IS NULL
OR thd_requesttype_dca_oms_form = '' ) THEN
thd_requesttypeidmform
ELSE thd_requesttype_dca_oms_form
END
FROM zendeskticketexport
WHERE (groupname = 'DC Maintenance')
AND thd_requesttype_dca_oms_form = 'attribute_update_requests__discontinue___obsolete_request'
GROUP BY groupname,
thd_requesttype_dca_oms_form,
thd_requesttypeidmform
ORDER BY groupname
我收到的结果:
groupname Result
DC Maintenance attribute_update_requests__discontinue___obsolete_request
DC Maintenance attribute_update_requests__discontinue___obsolete_request
DC Maintenance attribute_update_requests__discontinue___obsolete_request
结果是什么让我失望;我认为group by运算符应该将相同的记录组合在一起,但是group by by运算符与case运算符一起使得这些记录不会被分组。在测试这两种情况时,我看到以下查询返回0条记录:
select * from ZendeskTicketExport
where groupName = 'DC Maintenance' and thd_requesttypeidmform = 'attribute_update_requests__discontinue___obsolete_request'
在我看来,这表明所有记录都应该组合在一起,因为case运算符只返回thd_requesttype_dca_oms_form字段的值。提前谢谢。
答案 0 :(得分:2)
您按第三列分组。 SQL Server将创建您的组,即使该列本身不在SELECT
。
请改为尝试:
SELECT groupname,
CASE
WHEN (thd_requesttype_dca_oms_form IS NULL OR
thd_requesttype_dca_oms_form = '' ) THEN thd_requesttypeidmform
ELSE thd_requesttype_dca_oms_form
END
FROM zendeskticketexport
WHERE
groupname = 'DC Maintenance' AND
thd_requesttype_dca_oms_form = 'attribute_update_requests__discontinue___obsolete_request'
GROUP BY groupname,
thd_requesttype_dca_oms_form,
CASE
WHEN (thd_requesttype_dca_oms_form IS NULL OR
thd_requesttype_dca_oms_form = '' ) THEN thd_requesttypeidmform
ELSE thd_requesttype_dca_oms_form
END
ORDER BY groupname