How to combine integers from rows and make it one distinct row - mysql

My current SQL query outputs something like this:
Team Amount
A 10.00
B 20.00
C 40.00
C 30.00
I was wondering how I could make the query only output a single row for multiple "teams" and add the integers together for all teams - basically all teams display only once and the amount is the sum of all that team's entries in the database.
For example, the correct way I want the example above to output would be like this:
Team Amount
A 10.00
B 20.00
C 70.00

You need a straightforward sum and group-by:
select team, sum(amount) as amount
from mytable
group by team
order by team
It is unclear whether you want arbitrary (ie no) ordering, ordering by team, or ordering by the sum. If you want to order by the sum, change the order-by clause to:
order by sum(amount)

Related

MS-Access : How to sum multiple values from 2 different tables according to dates with output on 1 date per row

I am still getting started learning Access.
I have 3 tables. Table one has Date as primary key and will have all dates. Tables 2 and 3 (Table 3 is mislabeled in the example image as a second Table 2) will both have 2 columns, Date and Amount. Tables 2 and 3 could have multiple rows with the same date (different amounts) and some may miss dates. I am looking for an output query that would have 1 row for every date in table 2 & 3 that has an amount (some dates may not have an amount in either table) and sums all those amounts for that date in 1 row. Below are example tables and the desired output query. Thanks so much for the newbie help!
I now have this code (Note that I have eliminated Table 1):
SELECT Table2.Dat, Sum(Table2.Amount) AS [Sum Of Amount], Sum(Table2.Tax) AS [Sum Of Tax]
FROM Table2
GROUP BY Table2.Dat;
UNION ALL SELECT Table3.Dat, Sum(Table3.Amount) AS [Sum Of Amount], Sum(Table3.Tax) AS [Sum Of Tax]
FROM Table3
GROUP BY Table3.Dat;
This sums the amounts from same dates for each seperate table, but does not sum the dates for both tables. I imagine it is another GROUP function but I have not been successful in forming it correctly.
Current Results from code above
Try below query.
SELECT tt.mDate AS TransactionDate, Sum(tt.SumOfAmount) AS AmountTotal
FROM (SELECT Table2.tDate as mDate, Sum(Table2.Amount) AS SumOfAmount
FROM Table2
GROUP BY tDate
UNION
SELECT Table3.tDate As mDate, Sum(Table3.Amount) AS SumOfAmount
FROM Table3
GROUP BY tDate) AS tt
GROUP BY tt.mDate;

Select Distinct whilst adding tuples together in SQL

I have a table that contains random data against a key with duplicate entries. I'm looking to remove the duplicates (a projection as it is called in relational algebra), but rather than discarding the attached data, sum it together. For example:
orderID cost
1 5
1 2
1 10
2 3
2 3
3 15
Should remove duplicates from orderID whilst summing each orderID's values:
orderID cost
1 17 (5 + 2 + 10)
2 6
3 15
My assumption is I'd use SELECT DISTINCT somehow, but I don't know how I'd go about doing so. I understand GROUP BY might be able to do something but I am unsure.
This is a very basic aggregation:
SELECT orderId, SUM(cost) AS cost
FROM MyTable
GROUP BY orderId
This says, for each "orderId" grouping, sum the "cost" field and return one value per group.
You can use the group by clause to get one row per distinct values of the column(s) you're grouping by - orderId in this case. You can the apply an aggregate function to get a result of the columns you aren't grouping by - sum, in this case:
SELECT orderId, SUM(cost)
FROM mytable
GROUP BY orderId

get AVG() after GROUP BY in MYSQL

I just start to learn MYSQL and meet a problem like this
So the table is like this:
id name moneySpent
1 Alex 3
2 Alex 1
3 Bill 4
4 Alex 2
5 Alex 1
6 Chris 5
7 Chris 3
Lets say I wanna know the Average money spent per person. I try to do that by using SUM() GROUP BY and AVG() but I got stuck at AVG()
SELECT name, sum(moneySpent) AS total FROM table GROUP BY name;
then this will return
name total
Alex 7
Bill 4
Chris 8
Then how can I get a (7+4+8)/3 using AVG()?
You can get average per person using:
SELECT AVG(total) AS AVERAGE
FROM (SELECT name, sum(moneySpent) AS total
FROM table GROUP BY name) A
;
Output:
AVERAGE
6,3333
You can use inner query to get sum and outer query to derive average from sum as below.
SELECT Avg(sum1) FROM (
SELECT Sum(amount) AS sum1
FROM table1
GROUP BY NAME
) T1
It will generate below output.
AVERAGE_AMOUNT_SPENT
------------------
6.3333
which is what you want to be the output i.e. (7+4+8)/3 = 6.333
You can check demo here
So there are 2 ways to do this, first is use a new table to store the SELECT result. It is much more esay but may take more space.
Second is by jarlh, It comes to me that I do not need to GROUP BY the whole table, I can just add all moneySpent up and divided by distinct name count.
Thanks people!
select avg(total) as average from (SELECT name, sum(moneySpent) AS total FROM table GROUP BY name);
You can use this query to get your desired output
OUTPUT:
AVERAGE
6.3333333333333333

Getting a sum of rows and retaining each row data

This is what I am trying to do:
Get the sum of all rows grouping by date
Get all the rows individually
I have successfully done this... The problem is I want the order to change overall
Here is my table columns
[id][date][amount]
Example being:
1, 2013-10-01, 102.10
2, 2013-10-01, 256.15
3, ...
The output I get in Laymen's terms,
the first x of rows are the totals grouped by date
the rest of the rows are the individual amounts
The output I want in laymen's terms,
The first row is the total of the following rows by date, then rinse and repeat.
i.e.
2013-10-01, 200.00 <-- Total
2013-10-01, 150.00
2013-10-01, 50.00
2013-10-02, 300.00 <-- Total
2013-10-02, 150.00
2013-10-02, 150.00
Here is my query:
SELECT
t.date,
round(sum(t.amount), 2) as total
FROM invoice t
GROUP BY t.date
UNION ALL
SELECT
t.date,
round(t.amount, 2)
FROM invoice t;
See my example at SQLFiddle!
Thanks in advance for any assistance on this.
Assuming that you mean you want to change the order of your results by date (and I typically don't make such an assumption), you would simply need to add an "ORDER BY" to your sql. The below should do the trick.
SELECT
t.date,
round(sum(t.amount), 2) as total
FROM invoice t
GROUP BY t.date
UNION ALL
SELECT
t.date,
round(t.amount, 2)
FROM invoice t
ORDER BY date
Given your edits, it appears you want to sort by date, and then total amount descending:
SELECT
date,
round(sum(amount), 2) as total
FROM invoice
GROUP BY date
UNION ALL
SELECT
date,
round(amount, 2)
FROM invoice
ORDER BY date, total DESC
Updated Fiddle Demo

How does this count work?

My query is given below:
select vend_id,
COUNT(*) as num_prods
from Products
group by vend_id;
Please tell me how does this part work - select vend_id, COUNT(vend_id) as opposed to select COUNT(vend_id)?
select COUNT(vend_id)
That will return the number of rows where the vendor ID is not null
select vend_id, COUNT(*) as num_prods
from Products
group by vend_id
That will group the elements by Id's, and return, for each Id, how many rows do you have.
An example:
ID name salary start_date city region
----------- ---------- ----------- ----------------------- ---------- ------
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
If you run this query, you will get One row for city, and you can apply a function (in this case, count) to that row. So, for each city, you will get the count of rows. You can also use other functions.
SELECT City, COUNT(*) as Employees
FROM Employee
GROUP BY City
The result is:
City Employees
--------- ---------
New York 3
Toronto 2
Vancouver 3
as you can compare the numbers of rows for each city
When you simply select COUNT(vend_id) with no GROUP BY clause, you get one row with the total count of rows with a non-NULL vendor ID - that last bit is important and is one reason why you may prefer COUNT(*) so as to avoid "missing" rows. Some people may argue that COUNT(*) is somehow less efficient but that's true in no DBMS I've used. In any case, if you are using a brain-dead DBMS, you can always try COUNT(1).
When you group by vend_id, you get one row per vendor ID with the count being the number of rows for that ID.
In step-by-step detail (conceptually, though there are almost certainly efficiencies to be gained by optimising), the first query:
SELECT COUNT(vend_id) AS num_prods FROM products
Get a list of all rows in products.
Count the rows where vend_id is not NULL, then deliver one row containing that count in the single num_prods column.
For the grouping one:
SELECT vend_id, COUNT(vend_id) AS num_prods FROM products GROUP BY vend_id
Get a list of all rows in products.
For each value of vend_id:
Count the rows matching that vend_id where vend_id is not NULL, then deliver one row containing the vend_id in the first column and that count in the second num_prods column.
Note that those rows with a null vend_id do not contribute to the aggregate function (count in this case).
In the first query, that simply means they don't appear in the overall total.
In the second case, it means that the output row still exists but the count will be zero. That's another good reason to use COUNT(*) or COUNT(1).
select vend_id will only select the vend_id field, where select * will select all the fields
select vend_id, COUNT(vend_id) and select COUNT(vend_id) gives same result for the count column as long as you use group by vend_id. when you use select vend_id, COUNT(vend_id) you must group by it using vend_id