loyal Turkman
loyal Turkman

Reputation: 33

MULTIPLE COUNTS IN THE SAME QUERY

I have this table, i want to count the number of orders which are of the same type , and the count of all orders, as follow

ord_id type  
1      A
2      B
3      A
4      C

Here is the result :

TYPE COUNT  TOTAL
A    2      4
B    1      4
C    1      4

where count column is the count of orders based on their type, and total is the total orders.

Here is my code:

SELECT type, COUNT(*)
FROM  
  table
where type = 'A'

Union

SELECT type, COUNT(*)
FROM  
  table
where type = 'b';

Upvotes: 0

Views: 42

Answers (1)

GMB
GMB

Reputation: 222462

Use aggregation and window functions:

select 
    type,
    count(*) cnt,
    sum(count(*)) over() total
from mytable
group by type

Upvotes: 1

Related Questions