display data month wise in bigquery - google-bigquery

select date_add('2015-01-15',(7-dayofweek('2015-01-15')),"day"),
date_add('2015-01-15',(14-dayofweek('2015-01-15')),"day")
output of this query is
Row f0_ f1_
1 2015-01-17 00:00:00 UTC 2015-01-24 00:00:00 UTC
but i want to display the data as
sno date sum
1 2015-01-01 00:00:00 UTC 20(first month sum)
2 2015-02-01 00:00:00 UTC 30 (second month sum)

The STRFTIME_UTC_USEC has rich formatting of dates, the code for month names is %b, i.e.
select strftime_utc_usec(date_add('2015-01-15',
(7-dayofweek('2015-01-15')),"day"), "%Y-%b-%e"),
strftime_utc_usec(date_add('2015-01-15',
(14-dayofweek('2015-01-15')),"day"), "%Y-%b-%e")
results in
Row f0_ f1_
1 2015-Jan-17 2015-Jan-24

As an addendum, if you want to display the data from each month in multiple rows, you can use GROUP BY. There's an example here: Using a timestamp function in a GROUP BY.

Related

Calculating difference (or deltas) between current and previous row with clickhouse

It would be awesome if there was a way to index rows during a query.
Is there a way to SELECT (compute) the difference of a single column between consecutive rows?
Let's say, something like the following query
SELECT
toStartOfDay(stamp) AS day,
count(day ) AS events ,
day[current] - day[previous] AS difference, -- how do I calculate this
day[current] / day[previous] as percent, -- and this
FROM records
GROUP BY day
ORDER BY day
I want to get the integer and percentage difference between the current row's 'events' column and the previous one for something similar to this:
day
events
difference
percent
2022-01-06 00:00:00
197
NULL
NULL
2022-01-07 00:00:00
656
459
3.32
2022-01-08 00:00:00
15
-641
0.02
2022-01-09 00:00:00
7
-8
0.46
2022-01-10 00:00:00
137
130
19.5
My version of Clickhouse doesn't support window-function but, on looking about the LAG() function mentioned in the comments, I found neighbor(), which works perfectly for what I'm trying to do
SELECT
toStartOfDay(stamp) AS day,
count(day ) AS events ,
(events - neighbor(events, -1)) as diff,
(events / neighbor(events, -1)) as perc
FROM records
GROUP BY day
ORDER BY day

Is 'YYYYQ' a valid DATETIME format for SQL? And if so, how do I make it with my data?

I have some tables in a postgres that have a column for year and a column for quarter (both stored as bigint). I need to be able to combine those together in the output of a query in the form of 'YYYYQ' (not the hard part) AND have the datatype of that field be datetime (<--the hard part).
The only query I have attempted that didn't fail was -
SELECT to_date((year::VARCHAR + quarter::VARCHAR),'YYYYQ') AS Stuff
FROM company.products
And while the output is in DATETIME format, there is no Quarter info in it.
Sample -
stuff
2011-01-01
2011-01-01
2012-01-01
2012-01-01
2012-01-01
Is it even possible to create output that has the format 'YYYYQ' AND is in DATETIME format? And if so, how?
From the PostgreSQL docs (emphasis mine):
In to_timestamp and to_date, weekday names or numbers (DAY, D, and related field types) are accepted but are ignored for purposes of computing the result. The same is true for quarter (Q) fields.
You can save the date of the 1st day of the quarter. Multiply the recorded quarter -1 by 3.
SELECT to_date('2021','YYYY') + interval '6 month';
?column?
---------------------
2021-07-01 00:00:00
SELECT to_char(to_date('2021','YYYY') + interval '6 month','YYYYQ');
to_char
---------
20213
SELECT q,
to_char(to_date('2021','YYYY') + interval '3 month'*(q-1),'YYYYQ') as YYYYQ,
to_date('2021','YYYY') + interval '3 month'*(q-1) as d
FROM generate_series(1,4) f(q);
q | yyyyq | d
---+-------+---------------------
1 | 20211 | 2021-01-01 00:00:00
2 | 20212 | 2021-04-01 00:00:00
3 | 20213 | 2021-07-01 00:00:00
4 | 20214 | 2021-10-01 00:00:00

Multiple day on day changes based on dates in data as not continuous

See table A. There are number of sales per date. The dates are not continuous.
I want table B where it gives the sales moves per the previous date in the dataset.
I am trying to do it in SQL but get stuck. I can do an individual day on day difference by entering the date but I want one where I don't need to enter the dates manually
A
Date Sales
01/01/2019 100
05/01/2019 200
12/01/2019 50
25/01/2019 25
31/01/2019 200
B
Date DOD Move
01/01/2019 -
05/01/2019 +100
12/01/2019 -150
25/01/2019 -25
31/01/2019 +175
Use lag():
select t.*,
(sales - lag(sales) over (order by date)) as dod_move
from t;

Transpose and fill missing dates in date range

First, my main issue, I want to do is to check how many users that had an active product on a given date.
My data looks like this:
UserID ActiveFrom ActiveTo
1 2019-02-03 2019-03-05
2 2019-04-01 2019-04-30
1 2019-03-06 2019-04-04
3 2019-05-01 2019-05-31
I think the solution could to select all the ActiveFrom and union with ActiveFrom, and then fill in the missing dates so that it looked something like this:
UserID ActiveOnDate
1 2019-02-03
1 2019-02-04
1 2019-02-05
And so on
Then I could count all the UserID for each date. But i can’t find a query that fills out the missing dates in the date range. And I also don’t know if this is the “easiest” solution. Any ideas?
If your Dates have the Date datatype (and not VARCHAR for exemple), you can use the BETWEEN sql operator
https://sql.sh/cours/where/between
SELECT count(*) FROM user WHERE [dateToTest] BETWEEN ActiveFrom AND ActiveTo;

How can I feed extract or date_part with a dynamic query-derived value in Redshift?

I'm trying to modify a query to extract day, week, month, etc from a date where the field in "extract(field from datetime)" is provided by query values.
This is for Redshift. The simplified example I'm working on is below. Trying to create a summary for various date buckets without repeating the same query over and over.
SELECT
visit_start_date,
period_type,
extract(period_type from visit_start_date) as Period
FROM f_visit_dates
cross join d_period_type
Input table named f_visit_dates
with single date column visit_start_date and two rows:
visit_start_date
----------------
'2017-03-14'
'2018-05-06'
Input table named d_period_type contains five rows and one varchar(10) column:
period_type
-----------
'day'
'week'
'month'
'quarter'
'year'
Output should look like the following:
visit_start period_type period
----------- ----------- ------
2017-03-14 day 73
2017-03-14 week 11
2017-03-14 month 3
2017-03-14 quarter 1
2017-03-14 year 2017
2018-05-06 day 126
2018-05-06 week 18
2018-05-06 month 5
2018-05-06 quarter 2
2018-05-06 year 2018
I should add that the above as written doesn't work, obviously. I've also tried supplying the value through a "WITH" clause but that also doesn't work.