Getting sum of a column that needs a distinct value from other column
I guess this is a job for a subquery. So let's take your problem step by step.
I'm trying to find all the rows in the balance column that are the same and have the same date,
This subquery gets you that, I believe. It give the same result as SELECT DISTINCT but it also counts the duplicated rows.
SELECT COUNT(*) num_same_rows, balance, date
FROM `table`
WHERE a.datum BETWEEN '2021-01-01' AND '2021-09-01'
GROUP BY date, balance
and then find the sum of the balance column.
Nest the subquery like this.
SELECT SUM(balance) summed_balance, date
FROM (
SELECT COUNT(*) num_same_rows, balance, date
FROM `table`
WHERE a.datum BETWEEN '2021-01-01' AND '2021-09-01'
GROUP BY date, balance
) subquery
GROUP BY date
If you only want to consider rows that actually have duplicates, change your subquery to
SELECT COUNT(*) num_same_rows, balance, date
FROM `table`
WHERE a.datum BETWEEN '2021-01-01' AND '2021-09-01'
GROUP BY date, balance
HAVING COUNT(*) >= 1
Be careful here, though. You didn't tell us what you want to do, only how you want to do it. The way you described your problem calls for discarding duplicated data before doing the sums. Is that right? Do you want to discard data?
SQL query with distinct and sum
SELECT color, fruit, sum(rating)
FROM medleys
GROUP BY color, fruit
Distinct is used to select distinct elements, nothing more, while you want to aggregate and for that you need GROUP BY
and aggregation functions (SUM
).
SQL Server SUM() for DISTINCT records
Use count()
SELECT count(DISTINCT table_name.users)
FROM table_name
SQLFiddle demo
Sum distinct by separate ID column
Get the distinct combinations of ID
and Value
in a subquery and then the sum of Value
s:
SELECT SUM(Value) sum_value
FROM (SELECT DISTINCT ID, Value FROM tablename) t
Another way to do it is with SUM()
window function:
SELECT DISTINCT SUM(MAX(Value)) OVER() sum_value
FROM tablename
GROUP BY ID
See the demo.
How do I SUM DISTINCT Rows?
If you just want an overall figure for it try
select sum(PopEstimate2005), sum(EstimatesBase2000)
from(
SELECT Distinct
Zipcodes.CountyID,
us_co_est2005_allData.PopEstimate2005,
us_co_est2005_allData.EstimatesBase2000,
users_link_territory.userID
FROM
Zipcodes Inner Join
Users_link_territory ON zipcodes.CountyID = Users_link_territory.CountyID Inner Join
us_co_est2005_alldata ON zipcodes.FIPS = us_co_est2005_alldata.State AND zipcodes.code = us_co_est2005_alldata.County
WHERE
(users_link_territory.userid = 4)
) as foo
Get sum of column with distinct id in SQL
You can use two levels of aggregation:
select sum(avg_amount)
from (select ext_id, avg(amount) as avg_amount
from t
group by ext_id
) x
SUM(DISTINCT) Based on Other Columns
select sum (rate)
from yourTable
group by first_name, last_name
Edit
If you want to get all sum of those little "sums
", you will get a sum of all table..
Select sum(rate) from YourTable
but, if for some reason are differents (if you use a where
, for example)
and you need a sum for that select above, just do.
select sum(SumGrouped) from
( select sum (rate) as 'SumGrouped'
from yourTable
group by first_name, last_name) T1
SQL Server Sum Distinct Group by
With a long version, but easy to understand query. The below query using CTE should help you
with records as
(select distinct id, date, amt from table_name)
select sum (amt), id
from records
group by
id;
Related Topics
How to Create a "Unique" Constraint on a Boolean MySQL Column
How to Change Date Format in Hive
Postgres Syntax Error at or Near "If"
Does Facebook Fql Contain the SQL Like Operator
List of Stored Procedure from Table
How to Use Like and in for a Where Statment
Does Inner Join Performance Depends on Order of Tables
Can You Create Nested with Clauses for Common Table Expressions
Grant Privileges on Future Tables in Postgresql
How to Have Multiple Pivots Using the Same Pivot Column Using SQL Server
SQL Server 2008 Express Concat() Doesn't Exist
Extract Phone Number from Noised String
What SQL Databases Support Subqueries in Check Constraints
How to Have the Table Name as "Option" in MySQL
How to Fix Ora-01427 Single-Row Subquery Returns More Than One Row in Select
Calculating Days to Excluding Weekends (Monday to Friday) in SQL Server