Sum of Grouped Count in SQL Query

SUM of grouped COUNT in SQL Query

SELECT name, COUNT(name) AS count
FROM table
GROUP BY name

UNION ALL

SELECT 'SUM' name, COUNT(name)
FROM table

OUTPUT:

name                                               count
-------------------------------------------------- -----------
alpha 1
beta 3
Charlie 2
SUM 6

SUM of grouped COUNT and add proportion in SQL Query

Perhaps something like this

select
name,
count(1) cnt,
count(1) / (select count(1) from table1) proportion
from table1
group by name

Here's a SQLFiddle

SQL Count, Sum and Group by

Try this answer. This is very basic level Aggregate functionality. Can you please search on the net, you'll get the answer easily:

SELECT MODEL
,COUNT(1) AS QTY
,SUM(COST) AS COST
FROM [Table] GROUP BY MODEL

SQL: Get SUM of grouped query having count statement

Use a subquery against your current query:

SELECT SUM(summe)
FROM
(
SELECT COUNT(adrnr) AS summe
FROM tablename
WHERE datum >= '2021-08-01' AND datum < '2021-09-01'
GROUP BY adrnr
HAVING COUNT(drnr) > 1
) t;

Note: Your timestamp/date literals looked a bit off. The above assumes you only want to target the month of August, 2021.

How to sum and count Id's grouped by date from joining Tables in SQL

UNION your tables in a subquery and do aggregation and group by on top of that:

select count(1) as trans_count
,sum(amount_tendered) as amount_tendered
,DATE(transaction_datetime) as transaction_datetime
from
(select station_levy_id as trans, amount_tendered, transaction_datetime
from station_levy
union all
select market_levy_id as trans, amount_tendered, transaction_datetime
from market_levy) temp
group by DATE(transaction_datetime)

See result in SQL Fiddle



Related Topics



Leave a reply



Submit