当状态列包含待处理和已完成时,我想在状态列中显示部分待处理。
表的
Enquiry Name Part Number Status
------------------------------------------
Enq1 aar-12332 Pending
Enq1 aar-12555 Completed
Enq2 aar-12666 Pending
预期结果:
Enquiry Name Status
----------------------------------
Enq1 Partial Pending
Enq2 Pending
我写了并尝试了很多查询,但我没有得到预期的结果。
请帮帮我。
答案 0 :(得分:1)
通过使用以下方法,您可以实现预期的结果:
-- Get 'Partial Pending'
SELECT EnquiryName, 'Partial Pending' AS `Status`
FROM EnquiryTable
WHERE `Status` IN ('Pending', 'Completed' )
GROUP BY EnquiryName
HAVING COUNT(DISTINCT `Status`) = 2
UNION
-- Get the remaining Status
SELECT EnquiryName, `Status`
FROM EnquiryTable
WHERE `Status` IN ('Pending', 'Completed' )
GROUP BY EnquiryName
HAVING COUNT(DISTINCT `Status`) <> 2
结果:
EnquiryName Status
Enq1 Partial Pending
Enq2 Pending
此post
的参考资料 SQL Fiddle DEMO
也是如此。