Sum a column or another based on a helper column - SQL - mysql

I'm using Google Data Studio, and have 12 columns, 1 per month, with numbers, and another column with dates. I'd like to SUM all the numbers that will fall inside a date range based on the date column.
So I've something like this:
+---------+---------+---------+---------+
| DATE | January | February | March |
+---------+---------+---------+---------+
|20180101 | 500 | | |
|20180203 | 150 | | |
|20180201 | | 100 | |
|20180301 | | | 200 |
+---------+---------+---------+---------+
I'd like to have 650 as the result from January extraction, but can't find a solution yet.

Are you looking for aggregation?
select sum(january)
from t
where date >= :datestart and date < :dateend

Related

MySQL: Interpolate monthly values from daily recordings

I would like to calculate monthly meter values from irregular readings. My data looks like this:
| Date | Meter Reading |
--------------------------------
| 2018-01-01 | 1.200 |
| 2018-03-22 | 2.800 |
| 2018-05-19 | 4.600 |
As you can see, the entries can happen any date and they are not on the first and / or last day of a month. For a monthly view I therefore need to interpolate the data and distribute it equally across the months between two readings.
If you interpolate the difference in the measurements and divide them with the days passed between them, you get two "daily" estimations of 30,19 and 31,03. This again multiplied with the number of days in each month results in (current day is assumed to be the 2018-05-19):
| Month | Production |
---------------------------
| 2018-01 | 935,89 |
| 2018-02 | 845,32 |
| 2018-03 | 943,45 |
| 2018-04 | 930,90 |
| 2018-05 | 589,57 |
Is there a way to perform this on-the-fly query using SQL?
Thanks!

Properly SQL query

I need to skip results with high price per day. I've got a table like this:
+------+-------------+-------+
| days | return_date | value |
+------+-------------+-------+
| 2 | 2017-12-27 | 15180 |
| 3 | 2017-12-28 | 14449 |
| 4 | 2017-12-29 | 13081 |
| 5 | 2017-12-30 | 11203 |
| 6 | 2017-12-31 | 9497 |
| 6 | 2017-12-31 | 9442 |
+------+-------------+-------+
How can I print only the lowest price for 6 days (9442 in this example).
We can use a GROUP BY clause and an aggregate function. For example:
SELECT t.days
, t.return_date
, MIN(t.value) AS min_value
FROM mytable t
GROUP
BY t.days
, t.return_date
This doesn't really "skip" rows. It accesses all the rows that satisfy the conditions in the WHERE clause (in this example, every row in the table). Then MySQL collapses rows into groups (in this example, rows with identical values of days and return_date get put into a group. The MIN(t.value) aggregate function selects out the minimum (lowest) value out of the group.
The query above is just an example of one approach of satisfying a particular specification.

Need to Aggregate on the Last Month for a Grouped Report

I have a dataset that looks like the following:
| Location | Category |Item Name | Month | QTY |
| -------- | -------- | -------- | -------- | --- |
| NY | Hardware | Screw | Jan 2017 | 100 |
| NY | Hardware | Screw | Feb 2017 | 50 |
| NY | Hardware | Screw | Mar 2017 | 75 |
| NY | Hardware | Bolt | Jan 2017 | 30 |
| NY | Hardware | Bolt | Feb 2017 | 90 |
| NY | Hardware | Bolt | Mar 2017 | 50 |
| CA | Hardware | Screw | Jan 2017 | 100 |
| CA | Hardware | Screw | Feb 2017 | 50 |
| CA | Hardware | Screw | Mar 2017 | 75 |
| CA | Hardware | Bolt | Jan 2017 | 30 |
| CA | Hardware | Bolt | Feb 2017 | 90 |
| CA | Hardware | Bolt | Mar 2017 | 50 |
My report needs to look like the following:
| Hardware | Screw | Bolt |
|Current Month Total | 150 | 100 |
|Yearly Total | 450 | 340 |
I need a way to limit the current month total to ONLY the current month but aggregate the values for the yearly total. I've tried using LAST in the aggregate but you can't. I've tried the following for the current month total aggregate. My Date value is the 1st day of the month and my parameter is the last day of the month, so I needed a way to match the 2 that is why there is the date addition. The jist is to try and match the current month, which is a parameter to the date column:
=iif( DateAdd(dateinterval.Day,-1,DateAdd(dateinterval.Month,1,Fields!Sale_DATE.Value)) = Parameters!ReportingDate.Value, iif(isnothing(sum(Fields!Total.Value)),"",sum(Fields!Total.Value)),sum(0))
but it only works if the query that returns the dataset returns ONLY the current month. If the query returns all of the months in the year it shows 0's. I need a way to filter the cells so they aggregate the values correctly
If I limit my report to only the current month I can't get the yearly aggregate and if I select all of the months I can't get the current month total.
You can do this using the built-in grouping functions without any fancy expressions.
Add a row group by month. Filter the row group to the current month. Add a column group by Item Name. Add a row outside and below the row group to get your yearly totals. The expressions will all simply be a sum of the Qty. The report will take care of summing the values within each group scope.
Actually, using grouping as suggested alone doesn't work. I was already using groups in my matrix. The issue is that I needed a different form of grouping within the same column and I needed to restrict the grouping to different date ranges. My problem was that I had the SUM in the wrong position in my formula.
The expressions for the yearly total should have been:
=Sum(IIF(Fields!ItemName.Value="Screw",Fields!QTY.Value,0))
=Sum(IIF(Fields!ItemName.Value="Bolt",Fields!QTY.Value,0))
Then, based on the dataset, if the current month is "Mar 2017" I can build the expression for Month Total as
=Sum(IIF(Fields!ItemName.Value="Screw" and Fields!Month.Value="Mar 2017",Fields!QTY.Value,0))``
=Sum(IIF(Fields!ItemName.Value="Bolt" and Fields!Month.Value="Mar 2017",Fields!QTY.Value,0))
These examples are using my sample dataset in this question. Since I couldn't hard code dates, wanted to use parameters, and needed to do a calculation this is what I ACTUALLY used:
SUM(iif(DateAdd(dateinterval.Day,-1,DateAdd(dateinterval.Month,1,Fields!REL_DATE.Value))= Parameters!ReportingDate.Value,
Fields!Total_OnTime.Value,0))/SUM(iif(DateAdd(dateinterval.Day,-1,DateAdd(dateinterval.Month,1,Fields!REL_DATE.Value))= Parameters!ReportingDate.Value,
Fields!Total.Value,0))
Hope this helps someone else.

Different value counts on same column using LIKE

I have a database like below
+------------+---------------------------------------+--------+
| sender | subject | day |
+------------+---------------------------------------+--------+
| Darshana | Re: [Dev] [Platform] Build error | Monday |
| Dushan A | (MOLDOVADEVDEV-49) GREG Startup Error | Monday |
+------------+---------------------------------------+--------+
I want to get the result using the above table. It should check if the subject contains the given word then add one to the that word column for a given day.
|Day | "Dev" | "startup"|
+---------+------------+----------+
| Monday | 1 | 2 |
| Friday | 0 | 3 |
I was thought of using DECODE function but I couldn't get the expected result.
You can do this with conditional aggregation:
select day, sum(subject like '%Dev%') as Dev,
sum(subject like '%startup%') as startup
from table t
group by day;

Average Of Column Counting Duplicates Once - PowerPivot + DAX

I have a column in PowerPivot that I would like to get the average of. However I only want the rows included that are the only instance of a value or the first instance of a duplicate value in another column. Is this Possible with DAX?
Simply put I need the column average of unique rows, determining uniqueness from another column.
Probably to old to assist, but for those that stumble across:
You would need to create two measures. The first would sum whatever it is you are trying to average by the distinct values in the other column.
| id | squilla |
| 01 | 100 |
| 01 | 110 |
| 02 | 90 |
| 03 | 100 |
| 03 | 90 |
So id=1 has total squilla of 210, id=2 spend of 90, and id=3 spend of 190. The distinct average (where id is the identifier) is 163.333
To do this in powerpivot, first create a measure that sums the squilla by id: Measure1:=CALCULATE(SUM('yourTable'[squilla]),VALUES('yourTable'[id]))
And the second to average it across id:
Measure2:=AVERAGEX(DISTINCT('yourTable'[id]),[Measure1])
My understanding of the OP's question looks something like this:
| id | age |
| -- | --- |
| 1 | 20 |
| 1 | 20 |
| 2 | 50 |
| 3 | 35 |
| 3 | 35 |
In this case, a summed average as suggested by aesthetic_a (40 + 50 + 70)/3 would not be appropriate.
However an averaged average ((40/2) + (50/1) + (35/2))/3 would be a solution to determine the distinct average grouped by id.
Measure:=AVERAGEX(VALUES(table[id]), CALCULATE(AVERAGE(table[age])))