postgresql count all entries created on a particular date? - sql

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'

Related

Filtering data who time column is in milliseconds

I am trying to filter data of Salesforce API and the date column that I am trying to filter has time in milliseconds.
Given below is what I would normally use to filter date column but this does not work since the date column is in milliseconds.
SELECT Id, LastModifiedDate FROM table where LastModifiedDate > current_date - 1
But this does not work in this case and I believe it is because the time column is in milliseconds..
I was able to figure this out
SELECT Id, LastModifiedDate FROM table where SystemModStamp >= LAST_N_DAYS:1
You can follow the below format
SELECT Id, LastModifiedDate FROM account where SystemModStamp >= 2021-08-01T11:00:00Z and SystemModStamp <= 2021-09-01T11:00:55Z

Select entries created after a date, which have big integer timestamp

The table has a created_date column which has big integer as time stamp values. One of the time stamp looks like this 1596007131121. How can I query this?
select count(*) from user where created_date: date >='2020-08-30';
I need to query this.
You can convert that to a proper timestamp using the to_timestamp() function:
select *
from the_table
where to_timestamp(created_date/1000::bigint) >= date '2020-08-30';
But I would highly recommend to convert that column to a proper timestamp column.
I think you want:
select '1970-01-01'::timestamp + (created_date / 1000) * interval '1 second'
If you want this in a where clause, then use:
where created_date >= extract(epoch from '2020-08-30') * 1000
This has the nice feature that you can use an index.

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

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.

SELECT query with LIKE fails

I'm using a SQL SELECT query to bring back all rows from a specific date.
The column I'm using is called TimeStamp (datetime)
(An example of data from this column = 01/02/2018 07:55:55)
What I would like is to return all rows from a specific date eg 24/06/2019
I have tried
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02/01/2018 07:55:55'
which returns one row (which is correct as there is only one row containing this data)
If I then try
SELECT top 20 TimeStamp
from Report
where TimeStamp LIKE '02/01/2018%'
I get no results, I have also tried escaping the forward slashes
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02\/01\/2018%'
Most databases support a string function called left(). If I assume that your "timestamp" is a string, then:
where left(timestamp, 10) = '01/02/2018'
However, it should be stored as a date or date/time. If so, then you can do:
where timestamp >= '2018-02-01' and
timestamp < '2018-02-02'
Note the use of standard formatted dates (YYYY-MM-DD). That is the way most databases implement date literals.
In SQL Server, you can also use:
where convert(date, timestamp) = '2018-02-01'
Both this and the previous version will use an index on timestamp, so both are reasonable solutions.
this should work
SELECT TimeStamp FROM report where convert(Date, TimeStamp) = '2019-06-24'
or select timestamp from report where timestamp between '2019-06-24' and '2019-06-25'. This will get you everything between 2019-06-24 00:00:00 and 2019-06-25 00:00:00 thus all records with date 2019-06-24
Convert timestamp value to date.
SELECT TimeStamp
FROM report
WHERE CAST(TimeStamp AS DATE) = '2019-06-24'

SQL Select Where date record created

Is there a way in SQL to filter a query based on the date a specific record was added if I don't have a field that has the date it was created on.
Something like this:
Select * from Table where Table.recorddate = '2013-01-01'
I think that what you are trying to achieve is not possible. If it's really necessary, just add the field. It'd be easier for you that way.
Use UTC format:
SELECT * FROM Table WHERE recorddate = '2014-01-01'
Note that UTC is YYYY-MM-DD. So Today's date would be:
SELECT * FROM Table WHERE recorddate = '2014-05-19'
If your date field also includes a time stamp you can use a between:
SELECT * FROM Table
WHERE recorddate BETWEEN '2014-05-19 00:00:00' AND '2014-05-19 23:59:59'