I want to know the week number of a month for a date in bigquery standard sql.In PostgreSQL if I write:
select To_char(current_date, 'YYYY-MM-W')<br>
It works for the date '25-04-2018' as 2018-04-4.
Here 2018 is the year, 04 is the month and 4 is the fourth week of the month in which the date falls.
I want something similar in bigquery standard sql.
If I write:
select format_date("%Y-%m",current_date())
It gives only 2018-04
I also want to know the week number of month.
Thank you in advance.
Here is solution (defining a UDF that you can use in a query) along with an example.
CREATE TEMP FUNCTION DateWithWeekOfMonth(date DATE) AS (
CONCAT(
FORMAT_DATE('%Y-%m-', date),
CAST(DIV(EXTRACT(DAY FROM date), 7) + 1 AS STRING)
)
);
SELECT date, DateWithWeekOfMonth(date)
FROM (
SELECT DATE '2018-04-01' AS date UNION ALL
SELECT DATE '2018-04-07' UNION ALL
SELECT DATE '2018-04-08' UNION ALL
SELECT DATE '2018-04-30'
);
Related
I am currently working through this. Here's some relevant documentation
I have a time date column in { 2020-11-14 16:04:15 UTC } format. I want to end up with a column with an integer 1-7 corresponding to the day of the week. 1 = Sunday and 7 = Saturday.
Currently, I have this that works for a specific date.
SELECT EXTRACT(DAYOFWEEK FROM DATE '2013-12-25') AS the_day
How could I apply this function to a whole column?
thank you for any help.
SQL BigQuery
Below is the example.
WITH
org_table AS (
SELECT DATE('2013-12-25') as org_col
UNION ALL SELECT '2013-12-26'
UNION ALL SELECT '2013-12-28'
UNION ALL SELECT '2013-12-29'
)
SELECT
org_col,
EXTRACT(DAYOFWEEK FROM org_col) AS the_day
FROM org_table
ORDER BY org_col
;
p.s.
To apply with your existing query, replace org_table.
WITH
org_table AS (
SELECT `your_column` as org_col
FROM `your_table`
)
...
Adapted from the documentation of date functions 'Extract' example 2.
https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions?hl=ar-YE#extract
SELECT
date,
EXTRACT(DAYOFWEEK FROM date) AS the_day
FROM UNNEST(GENERATE_DATE_ARRAY('2015-12-23', '2016-01-09')) AS date
ORDER BY date;
I have date range eg. '2021-01-05' and '2021-02-10'. Two months January and February.
Need resaults:
Months
------
1
2
You want to iterate through the months. This is done with a recursive query in SQL:
with months (month_start_date) as
(
select trunc(:start_date, 'month') from mytable
union all
select month_start_date + interval '1' month
from months
where month_start_date < trunc(:end_date, 'month')
)
select
extract(year from month_start_date) as year,
extract(month from month_start_date) as month
from months
order by month_start_date;
You can use EXTRACT function that Oracle has to achieve this. In your case it should look something like:
SELECT EXTRACT (month FROM date_column) as "Months"
For more information about this function you can check out documentation here.
Note: this question is for both SQL and ORACLE and we do not have permissions for creation of temp table or stored procedures.
The database has two tables.
One table has a field of End Dates of Months along with a text field which identifies the "Fiscal Month" label.
Second table has dates by day (mm/dd/yyyy) with numeric data associated.
We need to retrieve the second table data (summing the numerics) grouping by the associated "Fiscal Month" found in table One.
Within one query or using CTE or a better solution, how to perform some kind of lookup on Table One to retrieve the Fiscal Month that the mm/dd/yyy date in Table two should be grouped on.
Table 1 (Fiscal Month End Dates)
2015-05-29 - Fiscal Month is 'May2015'
2015-06-30 - Fiscal Month is 'Jun2015'
2015-07-31 - Fiscal Month is 'Jul2015'
Table 2 (mm/dd/yyyy) which needs to be summed and grouped by Fiscal Month
2015-05-29 should be grouped on 'May2015'
2015-06-30 should be grouped on 'Jun2015'
So the approach I have used is to create a range of dates for a particular fiscal month.
Since you have the last dates of each fiscal month, you can get the previous one as the end of the previous fiscal month. Of course this is with a starting hard-limit because the first month in the fiscal month table will not have the previous month date (I used 1st of Jan of the year).
Then when you join it with your daily data, you can use these two dates to determine which fiscal month the data belongs to.
To get the previous fiscal month end date, we can use the LAG analytic function using the month to order the rows.
The rest of the query is pretty straightforward.
WITH
fiscal_end_dates
AS
(SELECT TO_DATE ('2015-05-29', 'YYYY-MM-DD') AS last_date FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-06-30', 'YYYY-MM-DD') AS last_date FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-07-31', 'YYYY-MM-DD') AS last_date FROM DUAL),
daily_data
AS
(SELECT TO_DATE ('2015-05-29', 'YYYY-MM-DD') AS data_date,
10 AS some_value
FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-05-30', 'YYYY-MM-DD') AS data_date,
14 AS some_value
FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-06-20', 'YYYY-MM-DD') AS data_date,
34 AS some_value
FROM DUAL
UNION ALL
SELECT TO_DATE ('2015-07-04', 'YYYY-MM-DD') AS data_date,
34 AS some_value
FROM DUAL),
fiscal_date_range
AS
(SELECT last_date,
NVL (
LAG (last_date, 1) OVER (ORDER BY EXTRACT (MONTH FROM last_date)),
TO_DATE (EXTRACT (YEAR FROM last_date) || '-01-01', 'YYYY-MM-DD')
)
AS prev_month_fiscal_end_date,
INITCAP (TO_CHAR (last_date, 'MON')) || EXTRACT (YEAR FROM last_date)
AS fiscal_month
FROM fiscal_end_dates)
SELECT dd.*,
fdr.fiscal_month
FROM daily_data dd,
fiscal_date_range fdr
WHERE dd.data_date > fdr.prev_month_fiscal_end_date
AND dd.data_date <= fdr.last_date;
This is the result (I took the liberty of adding a few more rows in your daily data table just to show the query working)
DATA_DATE SOME_VALUE FISCAL_MONTH
5/29/2015 10 May2015
5/30/2015 14 Jun2015
6/20/2015 34 Jun2015
7/4/2015 34 Jul2015
All you need to do now is to use the result set and perform your grouping and aggregation.
SQL: how do you fetch a first day of the month?
Hi,
I need to fetch a first day of every month from tbl.date from column start_date.
11/1/2017
12/1/2017
1/1/2018
2/1/2018
ETC.
Thanks
One solution in ANSI SQL looks like this:
select dte - (1 - extract(day from dte)) * interval '1 day'
In SQL Server, this would be:
select dateadd(day, 1 - day(dte), dte)
Similar functionality is available in almost any database.
If you just want the rows that are first days of the month, then:
where extract(day from dte) = 1
In SQL Server, this would be:
where day(dte) = 1
Of course, the syntax might be different depending on the database.
Another solution, using Postgres, in case you need to select the first row for each month, if there's no guarantee that that will be the first day of the month:
CREATE TABLE dates (
id BIGSERIAL PRIMARY KEY,
date TIMESTAMP NOT NULL
);
INSERT INTO dates(date)
VALUES ('2017-05-05'::timestamp), ('2017-05-02'::timestamp), ('2017-05-30'::timestamp), ('2017-05-25'::timestamp), ('2017-05-15'::timestamp), ('2017-05-01'::timestamp), ('2017-06-10'::timestamp), ('2017-06-02');
SELECT DISTINCT ON(EXTRACT(MONTH FROM date), EXTRACT(YEAR FROM date)) date
FROM dates
ORDER BY EXTRACT(MONTH FROM date), EXTRACT(YEAR FROM date), date ASC;
Results:
date
--------------------
2017-05-01T00:00:00Z
2017-06-02T00:00:00Z
Another solution, using Sql server
declare #FirstDOM date, #LastDOM datetime
set #FirstDOM = (select dateadd(d,-1,dateadd(mm,datediff(m,0,getdate()),1 )))
SELECT CONVERT( varchar, #FirstDOM,101);
I need to select recods from oracle table for the current calendar week based on a date datatype field. Currently I am doing like this:
select * from my_table where enter_date > sysdate -7
If today is Thursday, this query will select records seven days from today which infiltrates to last week on Thursday. Is there a way to select records for the current calendar week dynamically?
If your week starts on a Monday then:
select ...
from ...
where dates >= trunc(sysdate,'IW')
For alternative definitions of the first day of the week, trunc(sysdate,'W') truncates to the day of the week with which the current month began, and trunc(sysdate,'WW') truncates to the day of the week with which the current year began.
See other available truncations here: http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions255.htm#i1002084
to_char(sysdate, 'd') returns day of week in [1..7] range; so try using
select *
from my_table
where enter_date >= trunc(sysdate) - to_char(sysdate, 'd') + 1
Here is the SQLFiddel Demo
Below is the query which you can try
select Table1.*
from Table1
where dates > sysdate - TO_CHAR(SYSDATE,'D')