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
Related
I'm trying to create a new column with the daily orders (the count of OrderNumber for each day). Since I have data coming from multiple sources, I'm using SSIS. My final table should look like this:
Date | Product Number | Quantity Sold | Number of Orders (for that date)
I've tried using Aggregate, but it's not working because of the other columns. I was thinking about creating a parallel source (the same staging table), on which I would use Aggregate to find the number of daily orders, and then find a way to bring it back to the final table, but there must be an easier way?
Aggregate transform takes and outputs only columns you select. So, for your case, select Date, Product, Quantity and some column for Order Count - we will return to this later. Specify Group by for the first two columns, Sum for the third, and Count for the forth. At output you will receive four columns with desired result.
Source column for Count should represent orders and does not include columns used in the first three functions. If you need to use one of these three columns, create a copy of it with Derived Column transfer. I would not recommend using (*) (all columns) for Count, since it will count rows with Null values as well.
in the below image I'm using
SELECT DISTINCT(name),date,reporting,leaving from attendance where date='2016-09-01
and I'm still getting repeating names. Why?
When using DISCTINCT, MySQL uses all columns as grouping factor. If you want group by only one column and get all corresponding column values, use GROUP BY instead
SELECT name, date, reporting, leaving FROM attendance GROUP BY name WHERE ...
Actually your all rows have distinct data apart from Name column if you want only distinct names then you can get it with help of Aggregate functions, you can use MIN or MAX as per your business requirement
SELECT Name,MAX(date),MAX(reporting),MAX(leaving)
FROM attendance
WHERE date='2016-09-01'
GROUP BY Name
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.
I have a table with 3 columns.
Column A contain numbers.
Column B contain numbers.
Column C is empty.
Is there a way where I can get the sum of column A and column B into column C like in excel =sum(a1+b1)
You can do math in your SELECT statements.
SELECT price, tax, price+tax AS total
FROM orders
This will give you three columns: price and tax are direct from the row, and total is calculated on the fly when the SELECT is executed.
You could update a third column like this:
UPDATE orders
SET total=price+tax
and that would update the total column in every row, but that is unnecessary and is bad practice. You don't need to store the values when they can be calculated on the fly.
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?