Easy SQL Challenge: Filtering Rows Using DATE()

365 Days of Coding: Day 25

Another successful sql challenge today. Handling dates are rather very important topics in SQL.

Today’s challenge: Given a table called “payment”, I had to find the total daily revenue for the period of June, 2020.

Table payment:

col_namecol_type
payment_idinteger
customer_idsmallint
staff_idsmallint
rental_idinteger
amountnumeric
payment_tstimestamp with time zone
Table “payment”

Expected Solution:

dtsum
2020-06-1457.84
2020-06-151376.52
Expected solution

My code:

SELECT DATE(payment_ts) AS dt, sum(amount) AS "sum"
FROM payment
WHERE payment_ts >= date '2020-06-01'
AND payment_ts < date '2020-07-01'
GROUP BY dt
ORDER BY dt;

Another alternative to filter on the dates is as follows:

SELECT DATE(payment_ts) AS dt, sum(amount) AS "sum"
FROM payment
WHERE payment_ts >= to_date('2020/06/01', 'yyyy/mm/dd') 
AND payment_ts < to_date('2020/07/01', 'yyyy/mm/dd')
GROUP BY dt
ORDER BY dt;

Leave a comment