Date filter for GCP - sql

I have a date column with past, present & future dates. I want to exclude all past dates, how can I do this in GCP ?
I tried using below -
Select*
from Table
where Date > Current_Date()
However I get an error
Please help.

You can try the below query for your requirement:
WITH sample_data AS (
SELECT DATE '2022-11-10' AS Sample_Date union all
SELECT DATE '2023-01-24' AS Sample_Date union all
SELECT DATE '2023-01-25'AS Sample_Date
)
Select * from sample_data where Sample_Date >= Current_Date();
Result
If this query does not satisfy your requirements, can you share the error message you are facing, sample input and sample output?

Related

SQL No of count month wise

I have a data set as below,
data is basically year and month YYYYMM, I need to bring a count of months eg 202001 is appearing 3 times, hence the count should be Nov 3 ( Desired output is shared below )
I'm unable to start to bring out the desired output, help would be much appreciated.
(Temp tables are not allowed to be created in the servers)
Please find the link for sample data link
Help would be much appretiated.
You can use to_date() to convert your number to a proper date, then group by that date:
select to_date(due_date_key::text, 'yyyymm') as due_date,
count(*)
from t
group by due_date;
The "due_date" column is a proper date, you can use the to_char() function to format it differently:
select to_char(due_date, 'yyyy') as year,
to_char(due_date, 'Mon') as output,
count
from (
select to_date(due_date_key::text, 'yyyymm') as due_date,
count(*)
from t
group by due_date
) t
order by due_date;
Online example

Get DISTINCT IDs for DISTINCT dates in BigQuery?

I'm trying to get DISTINCT IDs for DISTINCT dates in BigQuery to build a report.
The date field is the kay partition field.
I tried to start with something looking like
SELECT DISTINCT(Id) FROM `project.dataset.table`
WHERE DATE(KeyPartitionDate)
BETWEEN '2017-01-01' AND '2020-06-01'
But this only gives me the different Ids between those 2 dates.
Now when it comes to query and check for every month of the year the DISTINCT(Ids), I have no clue how to do it.
I tried using DISTINCT and GROUP By for the Date but that doesn't seem to be the right path...
Any idea ?
If you are looking for number of distinct IDs for each month then this might help you.
SELECT DATE_TRUNC(DATE(KeyPartitionDate), MONTH) as month,COUNT(DISTINCT Id) as num_of_Ids
FROM `project.dataset.table`
WHERE DATE(KeyPartitionDate) BETWEEN '2017-01-01' AND '2020-06-01'
GROUP BY DATE_TRUNC(DATE(KeyPartitionDate), MONTH)
#standardSQL
SELECT DISTINCT
DATE_TRUNC(DATE(KeyPartitionDate), MONTH) year_month,
id
FROM `project.dataset.table`
WHERE DATE(KeyPartitionDate)
BETWEEN '2017-01-01' AND '2020-06-01'

Select records after specific date in SQL

I have a column in my database called "begin_date".
I am trying to select records where the begin_date are greater than a specific date. I put
select * from Table_Name
where begin_date >= '1/1/2014'
However, it returns error message "String to date conversion error".
I am not sure how to modify the query to make it work?
Thanks!
Try using ISO (8601) standard date formats:
select *
from Table_Name
where begin_date >= '2014-01-01';
select *
from table
where data >= '01/01/2014'

Query for dates which are not present in a table

Consider a table ABC which has a column of date type.
How can we get all the dates of a range (between start date and end date) which are not present in the table.
This can be done in PLSQL.I am searching a SQL query for it.
You need to generate the arbitrary list of dates that you want to check for:
http://hashfactor.wordpress.com/2009/04/08/sql-generating-series-of-numbers-in-oracle/
e.g.:
-- generate 1..20
SELECT ROWNUM N FROM dual
CONNECT BY LEVEL <= 20
Then left join with your table, or use a where not exists subquery (which will likely be faster) to fetch the dates amongst those you've generated that contains no matching record.
Assuming that your table's dates do not include a time element (ie. they are effectively recorded as at midnight), try:
select check_date
from (select :start_date + level - 1 check_date
from dual
connect by level <= 1 + :end_date - :start_date) d
where not exists
(select null from mytable where mydate = check_date)
Given a date column in order to do this you need to generate a list of all possible dates between the start and end date and then remove those dates that already exist. As Mark has already suggested the obvious way to generate the list of all dates is to use a hierarchical query. You can also do this without knowing the dates in advance though.
with the_dates as (
select date_col
from my_table
)
, date_range as (
select max(date_col) as maxdate, min(date_col) as mindate
from the_dates
)
select mindate + level
from date_range
connect by level <= maxdate - mindate
minus
select date_col
from the_dates
;
Here's a SQL Fiddle
The point of the second layer of the CTE is to have a "table" that has all the information you need but is only one row so that the hierarchical query will work correctly.

Time difference between two times in the same table

(Please note that I have seen a similar question on StackOverflow recently, however I can not find it anywhere - if anyone can find it please post the link)
I have a table that has a datetime field in it. For example:
Table data
DateTime date;
int number;
I need to find the difference between date in a row and date in the next row. I have tried with a query:
select date as current_date, date - (select top 1 date where date > current_date from data) as time_difference from date
however this won't work, because of "current_date". Is there a way I can do this with one query?
SELECT date AS current_date, next_date - date AS difference
FROM mytable mo
CROSS APPLY
(
SELECT TOP 1 date AS next_date
FROM mytable mi
WHERE mi.date > mo.date
ORDER BY
date
) q