Group by day even if data is missing - date

I want to group results by day: max value for each of the the last 30 days. I came up with :
select max(value), DATE(time) from table where time>DATE('now', '-30 days') group by DATE(time);
But this only gives results for dates with data. I want null or 0 for the dates without data. Is that possible?

Related

Showing this year and last year's data in one row in PostgreSQL

I have a table of sales grouped by week. I want to write a query that creates a new table giving the sales of the week in question AND the sales of that item from this time last year, but my attempts either give blank cells for the this-time-last-year (TTLY) values or duplicates.
I've tried writing a subquery that takes the date, subtracts 52 weeks, and shows the value for that week, then joining that subquery to my main query.
However, that subquery isn't working: the query shows the date of a year ago correctly, but doesn't then pull the SALES for that TTLY week, only the current week.
with ttyl as
(select
date::date as date,
(sales.date - interval '52 weeks') as date_ttly,
ID,
value
from sales
where country = 'uk' and date > '2019-08-01' and ID = '12345678')
In this example the subquery generates the previous year's date in the date_ttly column but pulls 2019 data in the value column.
All the WHERE conditions are just temporary so as to make building the query easier.
Thank you!
Assuming that the sales are grouped by date and by country only, a join on the same table should work:
SELECT sales1.id,
sales1.date,
sales1.value,
sales2.date,
sales2.value
FROM sales AS sales1
JOIN sales AS sales2 ON sales1.date - interval '52 weeks' = sales2.date
AND sales1.country = sales2.country
However, this also assumes that your date is always the same day of the week, e.g. Monday.

how can i fetch recored week wise from between two dates in postgresql?

I am using this query for fetching the data day wise
SELECT
(count( server_time::timestamp::date)) ,
server_time::timestamp::date
FROM
complaint_details_v2
WHERE
server_time between '2018/10/03' and '2018/10/11'
GROUP BY
server_time::timestamp::date
ORDER BY
server_time ASC
but I want to alter the above query week wise instead of day wise between two dates.
you can group the dates into sevens by doing date arithmetic.
SELECT
(count( server_time::timestamp::date)) ,
min(server_time::timestamp::date) as "week starting"
FROM
complaint_details_v2
WHERE
server_time between '2018/10/03' and '2018/10/11'
GROUP BY
floor((server_time::timestamp::date - '2018/10/03'::date)/7)
ORDER BY
2 ASC
another alternative is grouping expression date_trunc(week,server_time) but that binds you to ISO weeks

Count Until A Specific Value?

Say you've got a table ordered by the date that captures the speed of vehicles with a device in them. And imagine you get 30 updates per day for the speed. It's not always 30 per vehicle. The data will have the vehicle, the timestamp, and the speed.
What I want to do is be able to count how many days have passed since the vehicle last went over 10 mph in order to find inactive vehicles. Is something like that possible in postgresql?
*Or is there a way to get back the row number of the table if it's sorted where the speed goes past 10, and then select the date in that row number to subtract the current date from the date listed?
SELECT DISTINCT ON (vessel) vessel, now() - date
FROM your_table
WHERE speed > 10
ORDER BY vessel, date DESC
This will tell you, for every vehicle, how long ago its speed field was last over 10.
SELECT vessel, now() - max(date)
WHERE speed > 10
FROM your_table
GROUP BY vessel;

Last 7 days data calculation in Tableau

I am having an issue in calculating last week data in Tableau. Below is my scenario:
In my dashboard, I have a slider which selects the date. In my table, I have a list of users where I will be showing each of them's call records. One column will have last week records and one will have total records.
For Total records, there is not an issue. But for finding last week's count, I need to have a calculated field, that needs to subtract 7 days from the date selected and then give out the number of records for each user.
Say I have selected date as 25-04-2017, then my table should show all the records until 25-04-2017 in one column and other should show data from 18-04-2017 till 25-04-2017.
You can filter it with Relative days. When adding your dimension (date type) to the Filters list the picture below will appear.
Now you can click on the relative date and to choose the best option for you. You can see it in the picture below.
Create a Date parameter for your user to select. Using the Superstore data set, I created a calc field for last 7 days sales:
if datediff('day',[Order Date],[date]) <= 7 and datediff('day',[Order Date],[date]) >= 0 then [Sales] end
And sales up to date:
if datediff('day',[Order Date],[date]) >= 0
then [Sales] end
See attached example: https://www.dropbox.com/s/nqdp9zj74jay72d/170427%20stack%20question.twbx?dl=0
I was able to find the solution for my issue.
I created a boolean field as Max7Days with the formula as below:
DATEDIFF('day', [Date] , {MAX([Date])} ) <= 7
And created another that would count the number of records for the last 7 days if the condition was true as per the below formula:
CASE [Max7Days]
WHEN TRUE
THEN
[Number of Records]
END

Redshift count number of Mondays in a given time range

I want to use Redshift to count the number of Mondays in a given time range. I've tried using date_part, which returns the day of the week. I can't use a simple count as there are multiple instances on the same day.
if you have dates table reference you can use the following code
select count(distinct my_table.date)
from my_table
where
date_part(dow,my_table.date)=1
and my_table.date between '2015-01-01' and '2016-01-01'
in this case the query will count all Mondays during 2015,
you can change the dates range the the day week .
date_part(dow,my_table.date)=1 -- Monday
date_part(dow,my_table.date)=2 -- Tuesday
and so on
if you don't have dates table , you should create Cartesian product