I would like to extract the month and day from a date - sql

My code
SELECT
PARSE_DATE('%Y%m%d', CAST(date AS STRING)) AS date,
EXTRACT(DAY from date) AS day_of_month,
EXTRACT(MONTH from date) AS week_of_year,
channelGrouping, deviceCategory, sessions, conversions,
I would like to turn the date's in my column which is in the form 20170101 into 2017-01-01.
Then I would like to extract the month and day from this. However I keep getting an error:
No matching signature for function EXTRACT for argument types: DATE_TIME_PART FROM INT64. Supported signatures: EXTRACT(DATE_TIME_PART FROM DATE); EXTRACT(DATE_TIME_PART FROM TIMESTAMP [AT TIME ZONE STRING]); EXTRACT(DATE_TIME_PART FROM DATETIME); EXTRACT(DATE_TIME_PART FROM TIME) at [1:60]

Below few options for BigQuery Standard SQL - to avoid multiple pre-parsing
Option 1
SELECT
date,
EXTRACT(DAY FROM date) AS day_of_month,
EXTRACT(WEEK FROM date) AS week_of_year,
channelGrouping, deviceCategory, sessions, conversions,
FROM `project.dataset.table`,
UNNEST([PARSE_DATE('%Y%m%d', CAST(date AS STRING))]) date
Option 2
SELECT
date,
EXTRACT(DAY FROM date) AS day_of_month,
EXTRACT(WEEK FROM date) AS week_of_year,
channelGrouping, deviceCategory, sessions, conversions,
FROM (
SELECT * REPLACE(PARSE_DATE('%Y%m%d', CAST(date AS STRING)) AS date)
FROM `project.dataset.table`
)

You need to re-parse the date for each extract():
SELECT PARSE_DATE('%Y%m%d', CAST(date AS STRING)) as date,
EXTRACT(DAY from PARSE_DATE('%Y%m%d',
EXTRACT(MONTH from PARSE_DATE('%Y%m%d', CAST(date AS STRING))) as week_of_year,
channelGrouping, deviceCategory, sessions, conversions,
However, if the date is already a number in YYYYMMDD format, why not just use arithmetic functions:
select mod(date, 100) as day,
mod(floor(date / 100), 100) as month

Related

Trying to get a DATEDIFF function to add a column to calculate days between start_date and end_date

Have tried to get this right so i can get # of days between starting and ending dates with are timestamps.
Keep getting errors like:
line 4:24: mismatched input 'as'. expecting: ',',
What am I missing?
SELECT
CAST(start_date AS DATE) AS start_date,
CAST(end_date AS DATE) AS end_date,
DATEDIFF (d,start_date AS DATE, end_date AS DATE)
FROM "test"."question_five_temp_table";
SELECT
CAST(start_date AS DATE) AS start_date,
CAST(end_date AS DATE) AS end_date,
DATEDIFF (day,start_date AS DATE, end_date AS DATE)
FROM "test"."question_five_temp_table";
SELECT
CAST(start_date AS DATE) AS start_date,
CAST(end_date AS DATE) AS end_date,
DATEDIFF (day,start_date, end_date)
FROM "test"."question_five_temp_table";
Amazon Athena is based on (the old) PrestoDB. It uses date_diff() rather than datediff():
SELECT CAST(start_date AS DATE) AS start_date,
CAST(end_date AS DATE) AS end_date,
DATE_DIFF(day, start_date, end_date)
FROM "test"."question_five_temp_table";

Big query error:Failed to parse input string

Using Standard sql query but getting subject mentioned error(Failed to parse input string).
#standardSQL
SELECT
date,
EXTRACT(DAY FROM date) AS day_of_week,
EXTRACT(WEEK FROM date) AS week_of_year,
FORMAT_DATE("%y-%m", date) AS yyyymm
FROM(
SELECT PARSE_DATE('%y%m%d', date) date, campaign
FROM `tech-team-staging-2019.DFW_GA_Data_v1_05122019.DFW_G_Analytics_Predicted_data_v1_05122019`
GROUP BY 1,2
)
Below is for BigQuery Standard SQL
Note: it is not clear what your data field look like - so below are the options
in case if your date field is a string with YYYY-MM-DD - you should use below
#standardSQL
SELECT
date,
EXTRACT(DAY FROM date) AS day_of_week,
EXTRACT(WEEK FROM date) AS week_of_year,
FORMAT_DATE("%Y-%m", date) AS yyyymm
FROM(
SELECT PARSE_DATE('%Y-%m-%d', date) date, campaign
FROM `tech-team-staging-2019.DFW_GA_Data_v1_05122019.DFW_G_Analytics_Predicted_data_v1_05122019`
GROUP BY 1,2
)
in case if it is - YY-MM-DD
#standardSQL
SELECT
date,
EXTRACT(DAY FROM date) AS day_of_week,
EXTRACT(WEEK FROM date) AS week_of_year,
FORMAT_DATE("%Y-%m", date) AS yyyymm
FROM(
SELECT PARSE_DATE('%y-%m-%d', date) date, campaign
FROM `tech-team-staging-2019.DFW_GA_Data_v1_05122019.DFW_G_Analytics_Predicted_data_v1_05122019`
GROUP BY 1,2
)
finally, if it is YYMMDD
#standardSQL
SELECT
date,
EXTRACT(DAY FROM date) AS day_of_week,
EXTRACT(WEEK FROM date) AS week_of_year,
FORMAT_DATE("%Y-%m", date) AS yyyymm
FROM(
SELECT PARSE_DATE('%y%m%d', date) date, campaign
FROM `tech-team-staging-2019.DFW_GA_Data_v1_05122019.DFW_G_Analytics_Predicted_data_v1_05122019`
GROUP BY 1,2
)
and yet one more - YYYYMMDD
#standardSQL
SELECT
date,
EXTRACT(DAY FROM date) AS day_of_week,
EXTRACT(WEEK FROM date) AS week_of_year,
FORMAT_DATE("%Y-%m", date) AS yyyymm
FROM(
SELECT PARSE_DATE('%Y%m%d', date) date, campaign
FROM `tech-team-staging-2019.DFW_GA_Data_v1_05122019.DFW_G_Analytics_Predicted_data_v1_05122019`
GROUP BY 1,2
)

Visits per Isoweek in big query

I am trying to pull visits per isoweek from big query.
however I am failing with the date transformation.
Could you support?
StandardSQL
SELECT count (visitid) as Sessions, date,
EXTRACT (ISOYEAR FROM date) AS isoyear
FROM `xxx_*`
WHERE _TABLE_SUFFIX BETWEEN '201806020' AND '20180630'
GROUP BY date
order by date DESC
Have you tried a query like this?
SELECT EXTRACT(ISOYEAR FROM date) as yyyy,
EXTRACT(ISOWEEK FROM DATE) as ww,
COUNT(*) as Sessions
FROM `xxx_*`
WHERE _TABLE_SUFFIX BETWEEN '201806020' AND '20180630'
GROUP BY yyyy, ww
ORDER BY MIN(date) DESC;

syntax error when cast in EXTRACT

I just try this and I got a syntax error with tdate (it a DATE type)
select *
from treatment
where EXTRACT(month FROM cast (date as TIMESTAMP) tdate) = EXTRACT(month FROM cast (date as TIMESTAMP) current_date)
is anyone can help me out..??
thanks!!!!
I do not think you need to bother casting either field since you should be able to extract the month directly from both. I would try:
select *
from treatment
where extract(month from tdate) = extract(month from current_date)
Is this what you're looking for instead:
select *
from treatment
where EXTRACT(month FROM cast (tdate as TIMESTAMP)) =
EXTRACT(month FROM cast (current_date as TIMESTAMP))
It should be cast(field as type) where you have cast(date as type).

Count distinct calendar weeks, months in Postgresql

I already know how to count how many distinct days I have in my DB :
SELECT
COUNT(DISTINCT DATE (TIME)) AS distinct_days
FROM table;
But when I tried to count distinct weeks or months, the only solution I found is super-slow...
For months:
SELECT
COUNT(DISTINCT CONCAT (EXTRACT(YEAR FROM TIME),EXTRACT(MONTH FROM TIME))) AS distinct_months
FROM table;
For weeks
SELECT
COUNT(DISTINCT CONCAT (EXTRACT(YEAR FROM TIME),EXTRACT(MONTH FROM TIME), EXTRACT(WEEK FROM TIME))) AS distinct_weeks
FROM table;
Do you have any idea(s) to optimize ?
(update) Notice:
COUNT(DISTINCT DATE_TRUNC('week', time)) AS distinct_weeks
and
COUNT(DISTINCT CONCAT (EXTRACT(YEAR FROM TIME),EXTRACT(MONTH FROM TIME), EXTRACT(WEEK FROM TIME))) AS distinct_weeks
don't have the same result (I want the second one) !
With COUNT(DISTINCT DATE_TRUNC('week', time)) you have 53 possibilities, and with COUNT(DISTINCT CONCAT (EXTRACT(YEAR FROM TIME),EXTRACT(MONTH FROM TIME), EXTRACT(WEEK FROM TIME))), possibly an infinity (e.g. 2014-01 week 1 is different of 2013-01 week 1)...
Finally, I found something at least twice faster :
for distinct weeks:
SELECT COUNT(distinct_weeks)
FROM (SELECT CONCAT(EXTRACT(YEAR FROM TIME),EXTRACT(MONTH FROM TIME),EXTRACT(WEEK FROM TIME)) AS distinct_weeks
FROM table
GROUP BY EXTRACT(YEAR FROM TIME),
EXTRACT(MONTH FROM TIME),
EXTRACT(WEEK FROM TIME)) t
for distinct months:
SELECT COUNT(distinct_months)
FROM (SELECT CONCAT(EXTRACT(YEAR FROM TIME),EXTRACT(MONTH FROM TIME)) AS distinct_months
FROM table
GROUP BY EXTRACT(YEAR FROM TIME),
EXTRACT(MONTH FROM TIME)) t
Maybe simply truncating the date is faster, because you don't need string conversion and concatenation then:
SELECT
COUNT(DISTINCT DATE_TRUNC('week', mytime)) AS distinct_weeks,
COUNT(DISTINCT DATE_TRUNC('month', mytime)) AS distinct_months,
COUNT(DISTINCT DATE_TRUNC('year', mytime)) AS distinct_years
FROM mytable;