Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the total number of orders made and the number of orders for which payment has been done

Tags:

sql

Retrieve the total number of orders made and the number of orders for which payment has been done(delivered).

TABLE ORDER

------------------------------------------------------
ORDERID QUOTATIONID     STATUS  
----------------------------------------------------
Q1001   Q1002           Delivered
O1002   Q1006           Ordered                         
O1003   Q1003           Delivered               
O1004   Q1006           Delivered               
O1005   Q1002           Delivered               
O1006   Q1008           Delivered               
O1007   Q1009           Ordered                             
O1008   Q1013           Ordered     

Unable to get the total number of orderid i.e 8

select count(orderid) as "TOTALORDERSCOUNT",count(Status) as "PAIDORDERSCOUNT" 
  from orders 
 where status ='Delivered'

The expected output is

TOTALORDERDSCOUNT     PAIDORDERSCOUNT
      8                     5
like image 842
deepak gupta Avatar asked Nov 21 '25 10:11

deepak gupta


1 Answers

I think you want conditional aggregation:

select count(*) as TOTALORDERSCOUNT,
       sum(case when status = 'Delivered' then 1 else 0 end) as PAIDORDERSCOUNT
from orders;
like image 66
Gordon Linoff Avatar answered Nov 24 '25 01:11

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!