I have a timeseries with a value column.
I want to get a cumulative count of values per days.
For example if I have for day 1 following points :
(timestamp1, value1) (timestamp2, value2) (timestamp3, value3)
I want to have a graph with :
zero value displayed for day 1 00:00 to timestamp1
1 value displayed for timestamp1 to timestamp2
2 value displayed for timestamp2 to timestamp3
3 value displayed for timestamp3 to day2 00:00
zero value displayed for day 2 00:00 to first value of day2
and so on
I could do such a request :
select count(value) from series where time = today group by time($interval)
But I won't get expected result as "group by" doesn't do a cumulative count a values per day but a cumulative count of value per $interval.
And if I do :
select count(value) from series where time = today group by time(today)
I will only have 1 count value per day.
How can I do it?
Not sure I understand, but could this be a solution?
select count(value) from series where time = today group by time(24h)
Related
I don't think this problem is solvable but I figured I would ask.
Data:
DateDim table with 1 row for every date. Columns = date
Sales table with 1 row for every sale. Important columns = id, sales_date
tables relate on date = sales_date
Plot:
I am plotting count of sales for each date, with date on the x-axis and count([id]) on the y-axis.
Problem:
I need a dynamic x-axis. If I filter date for less than a month I need days on the X-axis, but if I filter greater than a month I need month-year on the x-axis.
I accomplished this with daterunc function.
If endDate - startDate < 30
then daterunc('day', [date])
else if endDate - startDate >= 30
then daterunc('month', [date])
end
This works nice except for one issue: missing values. If I use the date as the axis, I can right click the date and select "show missing values" which will correctly plot 0 on days with no sales.
But the calculation above does not have a 'show missing values' option. So the plot does not show zeros when I use the calculation.
Is it possible to modify the above calculation to show missing values?
I'm trying to retrieve the sum of same values that has the same timestamp.
My query is
SELECT value FROM dashboards WHERE time >= '2021-03-07T00:00:00Z' AND time <= '2021-03-09T00:00:00Z'
My returned values are
time value
---- -----
2021-03-07T00:00:00Z 1
2021-03-07T00:00:00Z 1
2021-03-07T00:00:00Z 1
2021-03-08T00:00:00Z 2
2021-03-08T00:00:00Z 2
2021-03-08T00:00:00Z 2
2021-03-09T00:00:00Z 3
2021-03-09T00:00:00Z 3
2021-03-09T00:00:00Z 3
How can I change my query the result will be
time sum
---- -----
2021-03-07T00:00:00Z 3
2021-03-08T00:00:00Z 6
2021-03-09T00:00:00Z 9
SELECT SUM(value) FROM dashboards WHERE time >= '2021-03-07T00:00:00Z' AND time <= '2021-03-09T00:00:00Z' GROUP BY time(1h) FILL(none)
GROUP BY time(1h) - group results by time column with interval of 1h
FILL(none) - ignore empty results
I have a google sheet that keeps track of when an Id will go offline and when it will be back. Each row in this sheet has the Id of the object, the date it goes offline, the date it comes back, and a boolean for whether to count it or not.
On Sheet2 I want to keep track of how many days the object was offline each month for the past year.
Example Data
Id StartDate EndDate Bool
999999 1/5/2020 1/10/2020 TRUE
999999 12/25/2020 1/4/2020 TRUE
888888 10/1/2019 1/20/2020 TRUE
888888 1/21/2020 1/30/2020 TRUE
Sheet2
Id 2/1/2020 1/1/2020 12/1/2019 11/1/2019 10/1/2019 9/1/2019
999999 15 15 15 15 15 15
888888 111 111 111 111 111 111
Desired Output
Id 2/1/2020 1/1/2020 12/1/2019 11/1/2019 10/1/2019 9/1/2019
999999 0 9 7 0 0 0
888888 0 29 31 30 31 0
Google Sheet Example
To find the amount of days a product has been offline I've made this
=SUM(QUERY(Sheet1!$A:$D,CONCATENATE("select datediff(C,B) where D=TRUE and A=",$A2)))
However it does not check whether the days are within the month, I was considering having if statements in the datediff function, something which would work like this,
select datediff(ifs(StartDate<1stOfTheMonth,1stOfTheMonth,StartDate>1stOfTheMonth and StartDate<EndOfMonth,StartDate,StartDate>EndOfMonth,EndOfMonth),ifs(EndDate>EndOfMonth,EndOfMonth,EndDate<EndOfMonth and EndDate>StartOfMonth,EndDate,EndDate<1stOfTheMonth,1stOfTheMonth)) where ...
If the start date is before the month begins, it counts days from the start of the month, if the start date is during the month, it counts days from the start date, if the start date is after the month, it returns 0 days because it counts from the end of the month to the end of the month.
If the end date is after the end of the month, it counts to the end of the month, if the end date is during the month, it counts to the end date, if the end date is before the month it returns 0 days because it counts from the start of the month to the start of the month.
I would expect it to work but I don't know how I could implement it into the query function.
Create a fresh tab on your sample sheet, Place this formula in A1, then format the top row as Format>Number>More Formats>Custom Number format:
mmm yyyy
=ARRAYFORMULA(TRANSPOSE(QUERY(VLOOKUP(SEQUENCE(COUNTA(Sheet1!B2:B)*(MAX(Sheet1!C2:C-Sheet1!B2:B)+1),1,0)/(MAX(Sheet1!C2:C-Sheet1!B2:B)+1)+2,{ROW(Sheet1!B2:B),Sheet1!A2:A,IF((Sheet1!B2:B+SEQUENCE(1,MAX(Sheet1!C2:C-Sheet1!B2:B)+1,0)<=Sheet1!C2:C)*Sheet1!D2:D,IFERROR(EOMONTH(Sheet1!B2:B+SEQUENCE(1,MAX(Sheet1!C2:C-Sheet1!B2:B)+1,0),-1)+1),)},MOD(SEQUENCE(COUNTA(Sheet1!B2:B)*(MAX(Sheet1!C2:C-Sheet1!B2:B)+1),1,0),MAX(Sheet1!C2:C-Sheet1!B2:B)+1)*{0,1}+{2,3}),"select Col2,Count(Col2) where Col2 is not null group by Col2 pivot Col1 order by Col2 desc label Col2'ID'")))
I have 2 tables
Table-1 = Order details
Table-2 = Production details.
Explanation of color inside table:
Yellow color = Output Qty week wise and product wise.
Green color = My expectation. Example- The second order of shirt(Qty-10) delivery date is 14 Jan & there are 2 more orders (order num 1 & 4) of shirt which have delivery earlier than 14 Jan. So the finish week will be 4 as the order num 1 & 4 (total Qty 6) will be produced till week 2 as per the Table-2 (total Qty =7 (3+4).
Thanks to help me write the formula in E 2 to E6 cells.
Table1:
Table2:
Work out the sum of quantities for the same product and dates including this one using sumifs.
Compare it to the cumulative sum of the numbers produced for this product using match.
=ArrayFormula(match(true,sumifs(C$2:C$6,B$2:B$6,B2,D$2:D$6,"<="&D2)<=sumif(column(H:K),"<="&column(H:K),index(H$3:K$4,match(B2,G$3:G$4,0),0)),0))
I'm assuming for the time being that you couldn't have two rows with the same product and delivery date. If this could happen, you could refine the formula for the situation where (say) the first delivery could be sent in week 2 but the next delivery would be in week 3.
I can't seem to figure this one out. I'm trying to get the standard deviation of a column for the past 20 days. Here is what I have
Model.where('date < ?','2013-03-25')
.group('date')
.order('date DESC')
.limit(20)
.select('stddev_samp(percent_change) as stdev')
However all I'm getting is 20 entries of Nil. I was expecting 1 entry of the standard deviation.
After switching the stddev_samp to sum, I see that I'm getting nil because you can't have a standard deviation on 1 entry. I.e. It is not grouping the 20 as I expected, but calculating standard deviation on each date.
So my question is, how do I get stddev of the last 20 days? I know it's possible to simply choose select percent_change and then calculate the standard deviation in ruby, but I assume that the aggregate function stddev_samp should be usable in this case.
I am using rails 3.2 and Postgresql 9.2
I'm not a Ruby guy so I'll explain it in normal SQL:
What you're doing is:
SELECT stddev_samp(percent_change) as stdev
FROM tbl
WHERE date < '2013-03-25'
GROUP BY date
ORDER BY date DESC
LIMIT 20;
This calculates the deviation for each day seperately, not for the sum of them, and when you try to get the deviation of only one element you get NULL.
Removing the GROUP BY would fix it but also would return the result for the whole table not just last 20 entries so we need a subquery:
SELECT stddev_samp(percent_change) as stdev
FROM
(SELECT percent_change
FROM tbl
WHERE date < '2013-03-25'
ORDER BY date DESC
LIMIT 20) AS q
No need to 'Group By', 'Order by' or sub-selects. Just get the records for the last 20 days and run the aggregate function on them.
Ruby:
Model.where('date >= ?', Date.today - 20.days).select('stddev_samp(percent_change) as stdev').first['stdev']
SQL:
select stddev_samp(percent_change) as stdev
from <table>
where date >= now() - interval 20 day;
If you want to use the LAST 20 RECORDS, not last 20 days:
Ruby:
Model.order('date desc').limit(20).select('stddev_samp(percent_change) as stdev').first['stdev']
SQL:
select stddev_samp(percent_change) as stdev
from <table>
order by date desc
limit 20;
you don't need the group by since you don't want one value for each date.
also your limit might not work if you have multiple values for a date or have a date missing
try this:
SELECT stddev_samp(percent_change) as stdev
FROM
(SELECT percent_change
FROM tbl
WHERE date > now() - interval '20 days') AS q