当列在mysql中包含挂起和已完成时,如何在状态列中显示部分挂起

时间:2016-07-09 05:21:26

标签: mysql

当状态列包含待处理和已完成时,我想在状态列中显示部分待处理。

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

我写了并尝试了很多查询,但我没有得到预期的结果。

请帮帮我。

1 个答案:

答案 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也是如此。