I have a table with 2 columns: date (date) and value(int). How can I write a query to output the date and either 'Up', 'Down', or 'Same' by comparing the previous date's value to the current date's value. (e.g. if 12/01/2016 has a value of 100 and 12/02/2016 has a value of 200 then the output should be 12/02/2016 Up.
http://www.mysqltutorial.org/mysql-tips/mysql-compare-calculate-difference-successive-rows/
See this page. Nice tutorial exactly as per your needs.
Related
I made a spreasheet to follow the growth of my income. It include a script that save periodically what I've earned. Column A is DATES, Column B is how much.
I want to calculate my daily average profit.
So I went to substract yesterday to today and then make an average of all that data. But there is a problem along the way because my script is saving data many times a day (to ensure I get data, and to tack more precisely).
I would like to first calculate an average profit for each day individually (C column on screenshot)
Then calculate the variation (D column on screenshot)
Then average the variation. (E column on screenshot)
As my data are growing each day, I'm looking for a flexible way to do it. I think QUERY may be a way but I don't know how to do that. A script would work too I guess. Maybe with the FILTER formula...
What's your thoughts on that ?
Cheers,
NipthiAe.
To calculate the daily profits you shouldn't make an average of the portfolio on each time. For example, if in a single day you have
Portfolio
1
2
3
4
The average would be 2.4 but that's not the day's profits. To calculate a day's profits you need to subtract the last value of the day with the last value of the day before. In this case, if the last value was 0, the answer is 4.
Here is how I would do this.
Solution
Step 1: Find the value of the protfolio when closing the day
What we need to do is find the latest value of each day. The simplest way of doing so is to find the last entry (using the datetime) of the day. So to get the last datetime we can use (in my case added it to D2):
=query({A3:A, arrayformula(int(A3:A))}; "select max(Col1) where Col1 is not null group by Col2 label max(Col1) ''"; 0)
Let's unpack this formula:
{A3:A, arrayformula(int(A3:A))}
It's a 2 column array. The first column are the datetimes. The second column are only the dates. That relies on the fact that int(datetime) returns the number representing the day. Add arrayformula to get it for each datetime and you get the day for each one.
Then the query:
select max(Col1)
where Col1 is not null
group by Col2
order by max(Col1)
label max(Col1) ''
The trick here is to get the biggest (latest) datetime within a day. This can be achieved by selecting max(datetime) and grouping by date. Then we can add a filter for empty cells (where Col1 is not null) and order the result to ensure the correct ordering. Also remove the default label by adding label max(Col1) '' at the end.
Step 2: Get the last value
Now that we have the last datetime of the day we can use vlookup to get the value (on E3):
=arrayformula(if(D3:D<>""; vlookup(D3:D; A3:B; 2; false); ""))
As you can see it's a simple vlookup that was wrapped in an if to prevent getting an error on empty cells. arrayformula allows us to do it for each row in one formula.
Step 3: Getting the daily profit
Now that we have the latest value we only need to subtract the row with the previous one (on F3):
=arrayformula(if(E3:E100<>""; E3:E-offset(E3:E; -1; 0); ""))
offset allows us to use this formula without having to specify the number of rows. As always, arrayformula allows us to do it for all rows in a single formula and we filter empty rows.
Step 3: Total average
We can finally take the average:
=average(F3:F)
If you didn't start from 0, you should change it to start at the next value (F4) since the first value will be the accumulated until that point.
References
QUERY (Google Editors Help)
VLOOKUP (Google Editors Help)
ARRAYFORMULA (Google Editors Help)
OFFSET (Google Editors Help)
IF (Google Editors Help)
INT (Google Editors Help)
You can do it with 3 formulas:
First (query) in D2:
To get the average per day
=query({A2:B},
"Select Col1 ,count(Col1), avg(Col2) where Col1 is not null
group by Col1 Order By Col1 asc
label Col1 'Date', count(Col1) 'Days', avg(Col2) 'Average/day'")
Second in G4:
To get the differences between the averages obtained by the Query:
=arrayformula(if(isblank(E4:E6),"",filter(E4:E6,E4:E6>-1)-filter(E3:E6,E3:E6>-1)))
Third (above the query) in F1:
To get the average of the variations:
="Variation Avg: "& DOLLAR(round(AVERAGE(F3:F),2))
I am having the table and i want to fetch rows based on 'rb_rm_id' column between the 2 dates 'rb_chkin' and 'chkout'. But, It gives me 0 result even if the values are present. Please help me. Thanks in Advance.
I have Tried the Following Mysql Query but, failed:
SELECT *
FROM room_booking
WHERE `rb_rm_id` = 2 AND
(`rb_chkin` >= '2019-08-05' AND `rb_chkin` <= '2019-08-06') AND
(`rb_chkout` >= '2019-08-05' AND `rb_chkout` <= '2019-08-06');
The Table image is here [1]: https://imgur.com/eQZ1I0C "Table"
Question is not clear and also your mysql table doesn't contains any value in that range. There is no check in value in your given range.
try this one
SELECT *
FROM room_booking
WHERE `rb_rm_id` = 2 AND
(`rb_chkin` <= '2019-08-05' AND `rb_chkout` >= '2019-08-06');
Your query says:
Give me any rows where rb_chkin and rb_chkout both are between 2019-08-05 and 2019-08-06 and the rb_rm_id is 2.
There is no such row in your table.
What you probably want is to detect whether a room is booked during the given date range. For a room to be, at least partially, occupied there can three case:
The check-in date falls within the date range.
The check-out date falls within the date range.
The check-in date is before the date range, and the check-out is after the date range.
If either one of these three conditions is met the room has a booking during the given date range. The equivalent query would be:
SELECT *
FROM room_booking
WHERE `rb_rm_id` = 2 AND
(`rb_chkin` BETWEEN '2019-08-05' AND '2019-08-06') OR
(`rb_chkout` BETWEEN '2019-08-05' AND '2019-08-06') OR
(`rb_chkin` < '2019-08-05' AND `rb_chkout` > '2019-08-06');
So this returns all rows that have a booking for the given room in the given date range.
Please consider using better column names, which would make your code actually readable. Something like: rb_room_id, rb_customer_id, rb_check_in_date, rb_check_out_date. I really don't understand this need for obfuscation.
I'm trying to sum a net balance based on the earliest date in an SSRS report. In this case there are only 2 dates, but there can be more dates not more than 7 days.
Here's a sample of my data:
Here's what I'm trying to get with the earliest date of 10/26/15:
I've tried the following code, but not able to get this to work:
=Sum(IIf(DateDiff("d",Fields!SettleFullDate.Value,today())>=7
and DateDiff("d", Fields!SettleFullDate.Value, today())<7
and Fields!SETTLEBALANCE.Value>0), Fields!SETTLEBALANCE.Value, 0)
Update: I tried the code below and keep getting an error on the report. Could it be that I need to change the date field to an integer?
Thanks in advance for your help!
To compare the sum of values of two dates, the maximum and minimum in a set you can use the following equation
=Sum(iif(Fields!myDate.Value = Max(Fields!myDate.Value), Fields!myVal.Value, 0))
-Sum(iif(Fields!myDate.Value = MIN(Fields!myDate.Value), Fields!myVal.Value, 0))
This Sums all the values that match the maximum date in the dataset together, and sums all the values that match the minimum date in the dataset together, and takes one from the other.
It is irrespective of which dates you ask to be received, the above approach will work only against the records that you return to SSRS. So if you have a filter (WHERE clause) to return records between Date1 and Date2 this will still apply (Note - don't actually use 'Between' in the query)
Rather than using the maximum and minimum dates as listed here, you could also calculate a date similar to your original approach using
dateadd("d", -7, Fields!MySpecificDate.Value)
And insert that to the expression above.
Hopefully this is what you require - if not please let me know.
I have a matrix table which contains a day(currentdate value) as row group and month as a column group. The column group has two child columns one contains the data represents a value which falls on that particular day of that month and another is a running value. like wise the column values represents for current and previous month. Now the problem is for the month which doesn't have the date 31 contains the data which actually belongs to the particular months start date data. to understand how the data is populating for that particular row I added a column of date field. it shows the start date of the month.
I would like to know how to avoid this automatic data resetting.
Sample image of the issue
I am stuck with this issue for a while now any solutions are welcomed.
I have a table in MySQL and i am storing a timestamp.
Now lets say if i search on the basis of only date it works.
SELECT *
FROM table
WHERE timestamp > "2012-03-12";
this fetches all data where timestamp value is greater than 2012-03-12 00:00:00.
what i want is a query like this :
SELECT *
FROM table
WHERE timestamp > "10:20:09";
where all records with tie greater than 10:20:09 is fetched irrespective of the date.
Use this statement (it will get the time from your datetime column and compare it with the time)
SELECT *
FROM table
WHERE DATE_FORMAT(yourDatecolumn, '%H:%i:s') > '10:20:00'
Warning
This time format is for EU times (where 1pm = 13), if you want US format, use '%h:%i:s'