I have an order_table in which customer order information is saved.
- With each month's billing cycle for a customer, a new order_key (row) is added in the order_table
- billing_cycle_date_time field records the date time of their monthly billing cycle. In this case, I need to calculate date based on their 1st billing_cycle_date_time value only (in order words, billing_cycle_date_time value from the very 1st row only)
Trying to find out the following in 3 separate queries:
- Get customers who are in 2nd month billing cycle on day 2 (as of
today) - Get customers who are in 3rd month billing cycle on day 2 (as
of today) - Get customers who are in 2nd month billing cycle and 7 days
before end of second month (as of today)
My Sample Data
+-----------+-------------+-------------------------+------------+
| order_key | customer_id | billing_cycle_date_time | order_type |
+-----------+-------------+-------------------------+------------+
| 1234 | 11 | 2020-07-01 5:36:11 | online |
| 1289 | 11 | 2020-07-28 9:20:10 | online |
| 1100 | 14 | 2020-05-26 8:11:08 | online |
| 1150 | 14 | 2020-06-24 5:05:02 | online |
| 1267 | 14 | 2020-07-24 7:01:03 | online |
| 1567 | 15 | 2020-09-01 5:36:11 | online |
| 1678 | 15 | 2020-10-28 9:20:10 | online |
| 1690 | 15 | 2020-11-28 5:17:04 | online |
+-----------+-------------+-------------------------+------------+
Desired output when Aug 3, 2020 is today's date [i.e. Customers who are in 2nd month billing cycle on day 2 (as of today)]
+-------------+-----------------------------+------------------------------------+
| customer_id | MIN_billing_cycle_date_time | Calculated_billing_cycle_date_time |
+-------------+-----------------------------+------------------------------------+
| 11 | 2020-07-01 5:36:11 | 2020-08-03 5:36:11 |
+-------------+-----------------------------+------------------------------------+
Desired output when July 28, 2020 is today's date [customers who are in 3nd month billing cycle on day 2 (as of today)]
+-------------+-----------------------------+------------------------------------+
| customer_id | MIN_billing_cycle_date_time | Calculated_billing_cycle_date_time |
+-------------+-----------------------------+------------------------------------+
| 14 | 2020-05-26 8:11:08 | 2020-07-28 8:11:08 |
+-------------+-----------------------------+------------------------------------+
Desired output when Oct 25, 2020 is today's date [customers who are in 2nd month billing cycle and 7 days before end of second month (as of today)]
+-------------+-----------------------------+------------------------------------+
| customer_id | MIN_billing_cycle_date_time | Calculated_billing_cycle_date_time |
+-------------+-----------------------------+------------------------------------+
| 15 | 2020-09-01 5:36:11 | 2020-10-25 5:36:11 |
+-------------+-----------------------------+------------------------------------+
Existing error: I am able to write 3 sql queries but condition where I am performing calculation with billing_cycle_date_time and comparing with today's date is throwing error
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Here are my 3 queries:
query 1
-- query to get customers who are in 2nd month billing cycle on day 2 (as of today)
SELECT
customer_id,
billing_cycle_date_time AS MIN_billing_cycle_date_time,
DATEADD(dd,2,DATEADD(month, 1, MIN(billing_cycle_date_time))) as Calculated_billing_cycle_date_time,
count(order_key) as count
from
order_table
where
order_type ='online'
--this criteria below is giving ERROR
and CAST(DATEADD(dd,2,DATEADD(month, 1, MIN(billing_cycle_date_time))) AS DATE) = CAST(GETDATE() AS DATE)
group by order_key
--this criteria below is important since it defines number of months in a billing cycle (each row means one month)
Having COUNT(order_key) = 2
query 2
-- query to get customers who are in 3nd month billing cycle on day 2 (as of today)
SELECT
customer_id,
billing_cycle_date_time AS MIN_billing_cycle_date_time,
DATEADD(dd,2,DATEADD(month, 2, MIN(billing_cycle_date_time))) as Calculated_billing_cycle_date_time,
count(order_key) as count
from
order_table
where
order_type ='online'
--this criteria below is giving ERROR
and CAST(DATEADD(dd,2,DATEADD(month, 1, MIN(billing_cycle_date_time))) AS DATE) = CAST(GETDATE() AS DATE)
group by order_key
--this criteria below is important since it defines number of months in a billing cycle (each row means one month)
Having COUNT(order_key) = 3
query 3
-- query to get customers who are in 2nd month billing cycle and 7 days before end of second month (as of today)
SELECT
customer_id,
billing_cycle_date_time AS MIN_billing_cycle_date_time,
DATEADD(dd,-7,DATEADD(month, 2, MIN(billing_cycle_date_time))) as Calculated_billing_cycle_date_time,
count(order_key) as count
from
order_table
where
order_type ='online'
--this criteria below is giving ERROR
and CAST(DATEADD(dd,2,DATEADD(month, 1, MIN(billing_cycle_date_time))) AS DATE) = CAST(GETDATE() AS DATE)
group by order_key
--this criteria below is important since it defines number of months in a billing cycle (each row means one month)
Having COUNT(order_key) = 2
Can anyone guide me, please?