How to Calculate avg no of records added per day in BigQuery.? - sql

I have a table in BigQuery having a column Published_date with a datatype of "Timestamp". I want to calculate avg no of rows added per day (for a specific month) in that table. I have the following query
SELECT AVG(Num_Rows)
FROM (SELECT [Day]=DAY( Published_Date ), Num_Rows=COUNT(*)
FROM `mytable`
WHERE Published_Date BETWEEN '20190729' AND '20190729 '
GROUP BY DAY( Published_Date ) ) AS Z
But its generating the following error
Could not cast literal "20190729" to type TIMESTAMP
How should I deal with timestamp because I only need the date from timestamp column?

I want to calculate avg no of rows added per day (for a specific month) in that table
Below example for BigQuery Standard SQL
#standardSQL
SELECT AVG(Num_Rows) AS avg_rows_per_day
FROM (
SELECT DATE(Published_Date) AS day, COUNT(*) AS Num_Rows
FROM `project.dataset.mytable`
WHERE DATE(Published_Date) BETWEEN '2019-07-01' AND '2019-07-31'
GROUP BY day
)

Use explicit conversion:
WHERE Published_Date BETWEEN TIMESTAMP('2019-07-29') AND TIMESTAMP('2019-07-29')
Note that you have a column called "_date", but the error is saying that the value is a timestamp. I find this confusing. We use a convention of using _ts in columns that are timestamps (and _dt for datetime and _date for date).
Why is this important? The timestamp is UTC. So you might need to be careful about timezones and time components -- which is not obvious in a column called Publish_Date.

Related

how to select all entries having date 25-11-20 in oracle 11g?

sql table
here in the table above named carpooling contains a column name start_on which has date time as timestamp i have to write a query to select all the rows having date as 25-11-20 using to_char and to_date.
You write a timestamp literal like this:
timestamp '2020-11-25 00:00:00'
so the full filtering condition will be
where start_on >= timestamp '2020-11-25 00:00:00'
and start_on < timestamp '2020-11-26 00:00:00'
Note that dates and timestamps are different in Oracle, and dates include times down to the second (this is for historical reasons - originally there was only the date type, and timestamp was added much later).
Use the TRUNC function, along with date and interval literals:
SELECT *
FROM CARPOOLING
WHERE START_ON BETWEEN DATE '2020-11-25'
AND (DATE '2020-11-26' - INTERVAL '0.000001' SECOND)
You can simply use to_date, but it's recommended to remove the time when comparing the dates. Otherwise, rows having the same date, but a different time will not be selected. Removing the time can be done using TRUNC.
So you can do something like this:
SELECT * FROM carpooling
WHERE TRUNC(start_on) = TO_DATE('2020-11-25','yyyy.mm.dd');
If you don't want to check the 25th of November 2020, but another data, change the date to match your goal.

How to run BigQuery for Selection of last month Records?

I am trying to get records from BigQuery Table for last month. I found that my column is in TimeStamp format, that's why it is giving an error.
No matching signature for operator BETWEEN for argument types: TIMESTAMP, DATE, DATE. Supported signature: (ANY) BETWEEN (ANY) AND (ANY) at [4:21]
Table Structure
Query
SELECT user_mobile,count(*) as total_customer FROM `Project.Dataset.Table` cr
where cr.DATE BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 1 MONTH) AND CURRENT_DATE()
group by user_mobile
having count(*) >=1;
Please guide how can I use timestamps in my query to get my required results. Thank you.
Use CURRENT_TIMESTAMP() and TIMESTAMP_SUB() instead:
SELECT user_mobile,count(*) as total_customer
FROM `Project.Dataset.Table` cr
where cr.created_at BETWEEN TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) AND CURRENT_TIMESTAMP()
group by user_mobile
having count(*) >=1;
or convert created_at to date with DATE(cr.created_at)

Oracle SQL: How to modify query in order to get only results within a certain timeframe?

I use this statement in Oracle SQL Developer
select to_char(time,'DD/MM/YY hh24'),count(column) as xyz from table
where to_char(time,'DD/MM/YY')>= '08/04/21'
and to_char(time,'DD/MM/YY')<= '09/04/21'
and column='xyz'
group by to_char(time,'DD/MM/YY hh24')
order by to_char(time,'DD/MM/YY hh24');
What I expect is a result/table in which the result is ordered by time in ascending order (starting with the earliest hour on 08/04/21 and ending with the latest on 09/04/21. I would expect only entries for days 08/04/21 and 09/04/21. Instead, I get a result where also other dates are included like 09/02/21 or 08/12/20.
How can I modify my query?
You are converting your native date values to strings (with two-digit years!) and then comparing those strings. The string '08/12/20' is 'less than' the string '09/04/21'.
Compare your dates with other dates, which is easier as literals:
select to_char(trunc(time, 'HH'), 'DD/MM/YY HH24'), count(column) as xyz
from table
where time >= date '2021-04-08'
and time < date '2021-04-10'
and column='xyz'
group by trunc(time, 'HH')
order by trunc(time, 'HH');
I've used trunc() to remove/zero the minute and seconds parts, which means you can then group and order by that value; and just convert to a string for display at the last moment.
I've also converted to_char(time,'DD/MM/YY')<= '09/04/21' to time < date '2021-04-10' rather than time < date '2021-04-09'as your version include all data from the 9th; which may or may not be what you intended - you might have been trying to get a single day.
db<>fiddle demo
Assuming that time is of data type date, you don't want to do a to_char on it in your where clause or in your order by. As written, you're doing string comparisons rather than date comparisons so you're getting rows where the to_char(time string sorts alphabetically between the two values not rows where the date is between the two dates. Compare against date literals or do explicit to_date calls on your string literals
My wager is that you really want something like this
select trunc(time, 'HH24'),count(column) as xyz
from table
where time >= date '2021-08-04'
and time <= date '2021-09-04'
and column='xyz'
group by trunc(time, 'HH24')
order by trunc(time, 'HH24');

postgresql count all entries created on a particular date?

I want to count all entries created on a particular date in postgresql. The table has a created_date field which of type time stamp without time zone , One of the entries in created_date looks like this 2020-08-18 12:26:22.641.
select count(*) from table where created_date='2020-08-18*'
This is what i try but that does not work.
How can i count is there something like contain or regex match for this scenario??
Thanks!!
The most efficient way is to use a range condition:
select count(*)
from the_table
where created_date >= date '2020-08-18'
and created_date < date '2020-08-19';
Another option is to cast the timestamp to a date value:
select count(*)
from the_table
where created_date::date = date '2020-08-18'

Truncate SQL date-time to date

I've been trying to write a query to get the counts of entries to an Oracle database on specific days. The issue is that the last modified date in the database is in the format yyyy-mm-dd hh:mm:ss and I need it in the format yyyy-mm-dd so it counts the occurrences per day, not per second. When I truncate and cast the date, the query has been returning the format yyyy-mm-dd 00:00:00 and still not aggregating the counts per day. My query is shown bellow. I've been running variations of it.
SELECT COUNT(*), TRUNC(CAST(LASTMODIFIED AS DATE))
FROM LEADEXTENSIONS
GROUP BY LASTMODIFIED
ORDER BY LASTMODIFIED DESC
Oracle does not have a DATETIME data type; it has DATE or TIMESTAMP and both always have year, month, day, hour, minute and second components (TIMESTAMP can, optionally, also have fractional seconds and time zone components).
Given this CAST( lastmodified AS DATE ) is not going to do anything as you will cast the value from a DATE data type to a DATE data type.
What you need to do is TRUNCate the DATE value, which will set the hour, minute and second components of the DATE to midnight (00:00:00) and then use this in both the SELECT and GROUP BY clauses:
SELECT COUNT(*),
TRUNC( lastmodified )
FROM leadextensions
GROUP BY TRUNC( lastmodified )
ORDER BY TRUNC( lastmodified )
Your problem is the GROUP BY. You need to repeat the expression in Oracle:
SELECT COUNT(*), TRUNC(CAST(LASTMODIFIED AS DATE))
FROM LEADEXTENSIONS
GROUP BY TRUNC(CAST(LASTMODIFIED AS DATE))
ORDER BY TRUNC(CAST(LASTMODIFIED AS DATE)) DESC;
Although you are truncating the date in the SELECT, your version is still using the original value for the GROUP BY.