How to send two query into one? Is there any other alternative?

Posted on

Question :

So I have this query that gets me the number of failed parts in the order. for example 94 failed. However, I want to see the total amount of parts in that order. For example, 94 failed and 200 total. How can I achieve that in one query? If it’s not possible how can I combine two queries in one result table?

SELECT status, COUNT(order)
FROM result
WHERE status='failed' and order = '4373'
GROUP BY status

Answer :

For MySQL (as the question tagged):

SELECT SUM(status='failed') AS failed_orders, COUNT(1) AS total_orders
FROM result
WHERE order = '4373'
-- GROUP BY status -- ?? 

For another DBMS (for example, for Microsoft SQL Server):

SELECT COUNT(CASE WHEN status='failed' THEN 1 END) AS failed_orders, COUNT(1) AS total_orders
FROM result
WHERE order = '4373'

Leave a Reply

Your email address will not be published.