SUM of column that has been GROUPED BY - mysql

I am using the following query:
SELECT mgap_growth
FROM mgap_orders
WHERE account_manager_id = '159795'
GROUP BY mgap_ska_report_category
mgap_growth is a column with identical amounts that differ only per mgap_ska_report_category, which is the reason for the grouping. Now hat I have normalized the individual amounts per category, how can I use SUM to tally their total?
Here is a screenshot of the data:
I only need the SUM of the growth amounts per category, not of all of the mgap_growth records, but Im unsure as to how to SUM after the grouping.
Thanks!
EDIT FOR ADDITIONAL QUERY:
Let me throw another issue into the mix: we know I need to SUM only once per category, but what if I needed to GROUP BY CUSTOMER? I just found out that there are multiple customers in the data, each is duplicated per growth record, but differ by category. I really need to use two groupings, one for category to single out and SUM the growth amount and then another the single out the customer.
Here is an image describing the data:

If I understand you correctly, you need to sum the results from the subquery.
SELECT SUM(mgap_growth) AS total_mgap_growth
FROM (SELECT mgap_growth
from mgap_orders
WHERE account_manager_id = '159795'
GROUP BY mgap_ska_report_category) AS x

This should should show the total growth per category for that particular account manager:
SELECT sum(mgap_growth) AS Growth, mgap_ska_report_category as Category
FROM mgap_orders
WHERE account_manager_id = '159795'
GROUP BY mgap_ska_report_category
Rather than thinking of doing the SUM after the grouping, you can do the two together in the one statement. You were 99% of the way there with what you had already.
To answer your additional question in the comment, you can add another column to group by. The order that you list them in the group by section is the important part. The overall grouping comes first. So assuming 'Customer' is the customer column name you would do this:
SELECT mgap_ska_report_category as Category, Customer, sum(mgap_growth) AS Growth
FROM mgap_orders
WHERE account_manager_id = '159795'
GROUP BY mgap_ska_report_category, customer
WITH ROLLUP
Note that changing the SELECT columns in the top line was just for aesthetics, you can put them in any order and will get the same data, but this will be the easiest to read.
This shows the growth per customer by category for that particular account manager.
Edited again to add WITH ROLLUP. This will give you the totals per category as well. Try it with and without the WITH ROLLUP to see the how it changes things.

Related

Different output when I include more columns to the select statement in MySQL

Why do I not get the same results when running the two queries? If I run the second one I get the course with the smallest amount of credits and when I run the first one I get the courses ordered by courseid
select min(credits), title, courseid
from course
group by title, courseid
select min(credits)
from course
An aggregation query is any query that has a group by or an aggregation function in the select.
An aggregation query returns one row per group, where a "group" is defined as the unique combination of values of the keys in the group by clause. If there is no group by clause, then all rows are taken to be a single group and one row is returned.
So, your first query returns one row for each combination of title and courseid in the course table. That row contains the minimum value of credits for that combination. If the course table has only one row per courseid, then the results are very similar to the contents of the table.
The second query returns one row overall, with the minimum number of credits of all rows.
If you want to get one row from with the minimum number of credits, then you don't want an aggregation query. Instead, you can use:
select c.*
from course c
order by c.credits
limit 1;
When you use a group by, you are using a sort of "filter", in the first query you group by title, then all the same titles are grouped by courseid, in the second you only select the minimum value of credits without filtering.
Take a look at a group by doc maybe with some graphical examples like this:
https://www.geeksforgeeks.org/sql-group-by/

Understanding simple count logic in sql

I have a very basic question which I cannot answer myself but shouldn't take much of your time.
The following query works, it lists all the exhibition_category_id and counts the total of objects that are assigned to each category.
My question is: Why does it do it? I don't understand the query. It says count(*) - why doesn't it give me the total of different exhibition_category_id's (79), but instead counts, how many objects are assigned to each category?
Here is the query in question, as well as a screen shot from the actual output:
SELECT eb.exhibition_category_id, count(*) AS total
FROM exhibition_brand eb
GROUP BY eb.exhibition_category_id
https://i.stack.imgur.com/6deMv.png
Hope its understandable what I am asking for, eager to improve my post based on feedback.
Cheers
Your query is a basic aggregation query:
SELECT eb.exhibition_category_id, count(*) AS total
FROM exhibition_brand eb
GROUP BY eb.exhibition_category_id;
The GROUP BY specifies that the result set will contain one row for each value of eb.exhibition_category_id. The result set consists of two columns, one is the value that defines the row. The other is a count of the number of rows in each group. That is what COUNT(*) does.
If you wanted the total count of different eb.exhibition_category_id, then you want one row and COUNT(DISTINCT):
select count(distinct eb.exhibition_category_id)
from exhibition_brand eb;
The GROUP BY function groups the COUNT() by eb.exhibition_category_id, so the query groups the records by eb.exhibition_category_id, then counts the corresponding records.

SSRS Column chart, total as last column

I've got the following column chart:
Product names as Category groups with values for budget and revenue on each column. Now I want to create a final column (Total) which is the sum of each column. ie. one column with the total value of the budget and one with the total value of the revenue.
Can this be done directly in the graph without having to do the calculations in the dataset? It's very easy to add a total to a table but seems to be hard to add it to a chart.
No, you cannot just add a total column like you can add a total to a table and you are correct that the best approach is to modify your dataset to query.
Either perform a UNION to append a total row or utilize GROUPING SETS (if you want to get fancy) and you should get what you need.
Example UNION:
SELECT product, bedget, revenue
FROM myTable
UNION ALL
SELECT 'total', SUM(budget) as budget, SUM(revenue) as revenue
FROM myTable

where to find different types of items in a particular order_header_key IBM Sterling OMS

I am trying to write a query in STERLING database. I really wonder where to get the number of items ordered in a order.
I can get number of orders from yfs_order_header table but how to get how much quantity of of different items being ordered in each order.
For this you have yfs_order_line which is a hangoff enabled table and has the reference of yfs_order_header table. So there can be many order lines for a single order. So write a query to get the sum of order quantity from order lines table with order header as the condition. This will give you the number of quantities in an order if you group it with inventory_item_Id you ll get the no of quantities against each item.

SQL with condition on calculated value

I have a table with products, their amount and their price. I need to select all entries where the average price per article is between a range.
My query so far:
SELECT productid,AVG(SUM(price)/SUM(amount)) AS avg
FROM stock WHERE avg>=$from AND avg<=$to GROUP BY productid
If do this, it tells me avg doesn't exist.
Also I obviously need to group by because the sum and average need to be per wine
You need to put it in the HAVING clause. You cannot filter by the result of aggregates using WHERE.
MySQL does allow you to reference column aliases in HAVING. You might need
SELECT
productid,
AVG(price/amount) AS avg ,
/*SUM(price)/SUM(amount) AS avg (<--- Or perhaps this?)*/
FROM stock
GROUP BY productid
HAVING avg>=$from AND avg<=$to
But I'm not sure exactly what you are trying to do with AVG(SUM(price)/SUM(amount)) can you show some example data?