Dynamic query using bigquery and data studio - google-bigquery

I want to take out data for every date range in Data Studio without the need to change date range selectors in my BigQuery all the time. However, not sure if it is even possible to do so. The reasons I do this is to make sure that the queried data is only for 30 days, as later it do some kind of segmentation using that 30 days data.
Then I figured out that the Data Studio can use dynamic_date, however this way will never produce any datatable (datatable will be used to do other queries from it). Is it possible to do dynamic_date in BigQuery instead? like retrieving data from BigQuery using a date range not previously defined in the query.
From my point of view, code should be like :
SELECT
ID,
FROM `table`
WHERE DATE(Timestamp) between $DS_START_DATE and $DS_START_DATE + INTERVAL 30 DAY)
or
WHERE DATE(Timestamp) >= #DS_START_DATE

I believe in pure Bigquery you can use DECLARE clause for that purpose, defining variables of the specified type:
declare DS_START_DATE date default "2020-03-03";
declare DS_END_DATE date default "2020-03-04";
WITH sample AS (
SELECT '10001' AS id, cast('2020-03-01' AS timestamp) as date_id UNION ALL
SELECT '10002', cast('2020-03-02' AS timestamp) UNION ALL
SELECT '10003', cast('2020-03-03' AS timestamp) UNION ALL
SELECT '10004', cast('2020-03-04' AS timestamp) UNION ALL
SELECT '10005', cast('2020-03-05' AS timestamp) UNION ALL
SELECT '10006', cast('2020-03-06' AS timestamp)
)
select id, date_id from sample
where date(date_id) between DS_START_DATE and DS_END_DATE
Alternatively, you can take a look at parameterized queries, however as I mentioned in the comment, they are not supported in classic BigQuery web UI.

Related

How can I generate a random timestamp appended to getdate() in SQL Server?

The following sql select generates the current date and time:
select
Getdate() AS DueDate
which looks like:
2021-02-06 10:16:35.340
I'd like to use getdate() (or an equivalent alternative) to continue getting the current date but I'd like to randomize the time component. I am having trouble finding a workable solution within a select statement.
Any suggestions?
here is another way if you need to have random time for each row:
SELECT
DATEADD(SECOND ,RAND(CHECKSUM(NEWID())) * 86400,CONVERT(datetime,CONVERT(varchar(8),GETDATE(),112)))
FROM tablename
You can simply add another DATEADD around your expression. For example, using INFORMATION_SCHEMA:
SELECT DATEADD(MILLISECOND, RAND(ROW_NUMBER()OVER(ORDER BY C.ORDINAL_POSITION)) * 10000000, GETDATE())
FROM INFORMATION_SCHEMA.COLUMNS AS C
You can play around with the RAND() and ROW_NUMBER() functions to get the result you want. If you have a primary key with lots of gaps, that helps to randomize.

How to SELECT between two dates in SQL Server

I need to give a select count in a table using the range of two dates, however my dates are in "yyyy-MM-dd" format, and when I execute the select SQL it returns the following error:
Converting a varchar data type to a datetime data type resulted in a value out of range
Here is the query I'm using:
SELECT COUNT(*) AS score
FROM requests
WHERE date_em BETWEEN '2019-04-01' AND '2019-04-30'
Table structure
date_em = DATETIME
Can someone help me?
My preferred method is:
SELECT COUNT(*) AS score
FROM requests
WHERE date_em >= '2019-04-01' AND
date_em < '2019-05-01'
This handles the time component and will make use of an index.
The following also works well in SQL Server:
SELECT COUNT(*) AS score
FROM requests
WHERE CONVERT(date, date_em) BETWEEN '2019-04-01' AND '2019-04-30'
In SQL Server, this will also use an index (if available). However, normally functions on columns preclude the use of an index, so I'm more hesitant about this approach.
have you try this
SELECT COUNT(*) AS score FROM requests WHERE date_em BETWEEN '2019-04-01 00:00' AND '2019-04-30 23:59'
add 00:00 and 23:59 to make it look like a DateTime.

how change date format in sql

i created query!
SELECT install_time, Count(id)
FROM (SELECT DISTINCT install_time, id
FROM mytab
where event='run'
and install_time>='09.01.2017' and install_time<='09.05.2017')
GROUP BY install_time
but install_time has that format like 09-05-2017 5:34:23
but i need this format without hours,minutes and seconds
09-05-2017
How change this date format?
First, you don't need a subquery.
Second, date/time functions vary by database. Here is one method that assumes a date() function for removing the time component:
SELECT DATE(install_time), COUNT(DISTINCT id)
FROM mytab
WHERE event = 'run' AND
install_time >= '2017-09-01' AND install_time < '2017-09-06'
GROUP BY DATE(install_time)
ORDER BY DATE(install_time);
Note that I also changed the date format to a standard format -- this works in most databases. And, I changed the second comparison for install_time to take the time component into account.
Here are some equivalents to the date() function (which works in MySQL, SQLite, BigQuery, and DB2 at least):
SQL Server: cast(install_time as date)
Postgres: date_trunc('day', install_time)
Oracle: trunc(install_time)
Teradata: cast(install_time as format 'YYYYMMDD')

How to get difference between two rows for a column field? When the column is a timestamp

I found this answer regarding the possibility to get the difference between two rows for a column field ( https://stackoverflow.com/questions/634568/how-to-get-difference-between-two-rows-for-a-column-field#= )
My question is, how can I make the same thing, when the Value is a Timestamp? I am using SQL Server 2012.
The table looks like in the picture below
Basically, what I want to do is to get the difference between two consecutive timestamps to see after how much time, a new DMC went through the process
Hope it's more clear right now. I'm pretty new at this.
Thank you in advance.
Since you are on SQL Server 2012, you can now use LAG/LEAD to get a value of a column from the previous/next row. Then you just calculate the difference as you would between two columns on the same row. Something like this:
create table YOUR_TABLE(ID integer, DT datetime);
insert into YOUR_TABLE
select 2, '02/02/2016 12:00:00' union all
select 3, '02/05/2016 12:00:00' union all
select 4, '02/06/2016 12:00:00' union all
select 5, '02/07/2016 12:00:00'
;
select
ID,
DT,
datediff(day, DT, lead(DT) over (order by ID)) as DIFF
from your_table;

How to get one day ahead of a given date?

Suppose I have a date 2010-07-29. Now I would like to check the result of one day ahead. how to do that
For example,
SELECT *
from table
where date = date("2010-07-29")
How to do one day before without changing the string "2010-07-29"?
I searched and get some suggestion from web and I tried
SELECT *
from table
where date = (date("2010-07-29") - 1 Day)
but failed.
MySQL
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATE_SUB('2010-07-29', INTERVAL 1 DAY)
AND '2010-07-29'
Change DATE_SUB to DATE_ADD if you want to add a day (and reverse the BETWEEN parameters).
SQL Server
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATEADD(dd, -1, '2010-07-29')
AND '2010-07-29'
Oracle
SELECT *
FROM TABLE t
WHERE t.date BETWEEN TO_DATE('2010-07-29', 'YYYY-MM-DD') - 1
AND TO_DATE('2010-07-29', 'YYYY-MM-DD')
I used BETWEEN because the date column is likely DATETIME (on MySQL & SQL Server, vs DATE on Oracle), which includes the time portion so equals means the value has to equal exactly. These queries give you the span of a day.
If you're using Oracle, you can use the + and - operators to add a number of days to a date.
http://psoug.org/reference/date_func.html
Example:
SELECT SYSDATE + 1 FROM dual;
Will yield tomorrow's date.
If you're not using Oracle, please tell use what you ARE using so we can give better answers. This sort of thing depends on the database you are using. It will NOT be the same across different databases.
Depends of the DateTime Functions available on the RDBMS
For Mysql you can try:
mysql> SELECT DATE_ADD('1997-12-31',
-> INTERVAL 1 DAY);
mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
-> '1997-12-02'
If youre using MSSQL, you're looking for DateAdd() I'm a little fuzzy on the syntax, but its something like:
Select * //not really, call out your columns
From [table]
Where date = DateAdd(dd, -1, "2010-07-29",)
Edit: This syntax should be correct: it has been updated in response to a comment.
I may have the specific parameters in the wrong order, but that should get you there.
In PL SQL : select sysdate+1 from dual;