Oracle Pivot Help based on Data - sql

I am trying use a oracle pivot function to display the data in below format. I have tried to use examples I found stackoverflow, but I am unable to achieve what I am looking.
With t as
(
select 1335 as emp_id, 'ADD Insurance New' as suuid, sysdate- 10 as startdate, null as enddate from dual
union all
select 1335 as emp_id, 'HS' as suuid, sysdate- 30 as startdate, null as enddate from dual
union all
select 1335 as emp_id, 'ADD Ins' as suuid, sysdate- 30 as startdate, Sysdate - 10 as enddate from dual
)
select * from t
output:
+--------+-------------------+-------------------+---------+-------------------+
| EMP_ID | SUUID_1 | SUUID_1_STARTDATE | SUUID_2 | SUUID_2_STARTDATE |
+--------+-------------------+-------------------+---------+-------------------+
| 1335 | ADD Insurance New | 10/5/2020 15:52 | HS | 9/15/2020 15:52 |
+--------+-------------------+-------------------+---------+-------------------+
Can anyone suggest to how to use SQL Pivot to get this format?

You can use conditional aggregation. There is more than one way to understand your question, but one approach that would work for your sample data is:
select emp_id,
max(case when rn = 1 then suuid end) suuid_1,
max(case when rn = 1 then startdate end) suid_1_startdate,
max(case when rn = 2 then suuid end) suuid_2,
max(case when rn = 2 then startdate end) suid_2_startdate
from (
select t.*, row_number() over(partition by emp_id order by startdate desc) rn
from t
where enddate is null
) t
group by emp_id
Demo on DB Fiddle:
EMP_ID | SUUID_1 | SUID_1_STARTDATE | SUUID_2 | SUID_2_STARTDATE
-----: | :---------------- | :--------------- | :------ | :---------------
1335 | ADD Insurance New | 05-OCT-20 | HS | 15-SEP-20

You can do it with PIVOT:
With t ( emp_id, suuid, startdate, enddate ) as
(
select 1335, 'ADD Insurance New', sysdate- 10, null from dual union all
select 1335, 'HS', sysdate- 30, null from dual union all
select 1335, 'ADD Ins', sysdate- 30, Sysdate - 10 from dual
)
SELECT emp_id,
"1_SUUID" AS suuid1,
"1_STARTDATE" AS suuid_startdate1,
"2_SUUID" AS suuid2,
"2_STARTDATE" AS suuid_startdate2
FROM (
SELECT t.*,
ROW_NUMBER() OVER ( ORDER BY startdate DESC, enddate DESC NULLS FIRST )
AS rn
FROM t
)
PIVOT (
MAX( suuid ) AS suuid,
MAX( startdate ) AS startdate,
MAX( enddate ) AS enddate
FOR rn IN ( 1, 2 )
)
Outputs:
EMP_ID | SUUID1 | SUUID_STARTDATE1 | SUUID2 | SUUID_STARTDATE2
-----: | :---------------- | :--------------- | :----- | :---------------
1335 | ADD Insurance New | 05-OCT-20 | HS | 15-SEP-20
db<>fiddle here

Related

Get users attendance entry and exit in one row SQL Server

I have a table with all entries for employees. I need to get all the working hours and the entry and exit time of the user in one record.
The table is like this:
How can I do that and also in case there is some missing entries or exit. Like one employee will have entry with no exit in some odd cases.
Assuming that the ins and outs line up (that is, are strictly interleaved), you can use lead() and some filtering:
select t.empId, convert(date, datetime) as date, datetime as timein,
next_datetime as timeout,
datediff(minute, datetime, next_datetime) / 60.0 as decimal_hours
from (select t.*,
lead(datetime) over (partition by empid order by datetime) as next_datetime
from t
) t
where entrytype = 'IN';
Note that this formats the duration as decimal hours rather than as a time. That part does not seem relevant to the actual question and just complicates the query.
This adds LEAD entrytype to make sure there is a corresponding OUT row. Also, it divides the date difference in minutes by 60.0 (added decimal)
select t.empId EmpID, cast(datetime as date) [Day], datetime [Timein], next_datetime [Timeout],
datediff(mi, datetime, next_datetime)/60.0 TotalHours
from (select t.*,
lead(datetime) over (partition by empid order by datetime) as next_datetime,
lead(entrytype) over (partition by empid order by datetime) as next_entrytype
from t
) t
where entrytype = 'IN'
and next_entrytype='Out';
Using Row_number to identify IN and OUT related to which employee:
SELECT EMPID, CAST([DATEUPDT] AS DATE) AS Date,
MAX(CASE WHEN ENTRYTYPE = 'IN' THEN CAST([DATEUPDT] AS TIME) END) AS TIMEIN,
MAX(CASE WHEN ENTRYTYPE = 'OUT' THEN CAST([DATEUPDT] AS TIME) END) AS TIMEOUT,
ABS(DATEDIFF(MINUTE, MAX(CASE WHEN ENTRYTYPE = 'OUT' THEN CAST([DATEUPDT] AS TIME) END), MAX(CASE WHEN ENTRYTYPE = 'IN' THEN CAST([DATEUPDT] AS TIME) END)))/60 AS DURATION
FROM
(
SELECT A.*,
ROW_NUMBER() OVER(PARTITION BY EMPID, [ENTRYTYPE] ORDER BY [DATEUPDT]) RN1
FROM EMPLOYEE_LOG A
) X
GROUP BY EMPID, RN1, CAST([DATEUPDT] AS DATE)
ORDER BY EMPID, RN1;
You can also "sessionize" in SQL Server - by using OLAP queries: With a counter that is at 1 when a new session begins and at 0 otherwise
WITH
input(id,empid,dttime,entrytype) AS (
SELECT 1,125,CAST('2020-08-13 08:10:00.000' AS DATETIME),'IN'
UNION ALL SELECT 1,157,CAST('2020-08-13 08:01:00.000' AS DATETIME),'IN'
UNION ALL SELECT 1,125,CAST('2020-08-13 15:21:00.000' AS DATETIME),'OUT'
UNION ALL SELECT 1,125,CAST('2020-08-13 15:24:00.000' AS DATETIME),'IN'
UNION ALL SELECT 1,125,CAST('2020-08-13 17:24:00.000' AS DATETIME),'OUT'
UNION ALL SELECT 1,157,CAST('2020-08-13 15:01:00.000' AS DATETIME),'OUT'
UNION ALL SELECT 1,125,CAST('2020-08-14 08:10:00.000' AS DATETIME),'IN'
UNION ALL SELECT 1,157,CAST('2020-08-14 08:01:00.000' AS DATETIME),'IN'
UNION ALL SELECT 1,125,CAST('2020-08-14 15:21:00.000' AS DATETIME),'OUT'
UNION ALL SELECT 1,125,CAST('2020-08-14 15:24:00.000' AS DATETIME),'IN'
UNION ALL SELECT 1,125,CAST('2020-08-14 17:24:00.000' AS DATETIME),'OUT'
UNION ALL SELECT 1,157,CAST('2020-08-14 15:01:00.000' AS DATETIME),'OUT'
)
,
with_session AS (
SELECT
*
, SUM(CASE entrytype WHEN 'IN' THEN 1 ELSE 0 END) OVER(
PARTITION BY empid ORDER BY dttime
) AS sessid
FROM input
)
SELECT
id
, empid
, sessid
, CAST(MAX(CASE entrytype WHEN 'IN' THEN dttime END) AS DATE) AS day
, CAST(MAX(CASE entrytype WHEN 'IN' THEN dttime END) AS TIME) AS indtm
, CAST(MAX(CASE entrytype WHEN 'OUT' THEN dttime END) AS TIME) AS outdtm
, CAST(
MAX(CASE entrytype WHEN 'OUT' THEN dttime END)
- MAX(CASE entrytype WHEN 'IN' THEN dttime END)
AS TIME
) AS totalhours
FROM with_session
GROUP BY
id
, empid
, sessid
ORDER BY
id
, 4
, empid
, sessid
;
-- out id | empid | sessid | day | indtm | outdtm | totalhours
-- out ----+-------+--------+------------+----------+----------+------------
-- out 1 | 125 | 1 | 2020-08-13 | 08:10:00 | 15:21:00 | 07:11:00
-- out 1 | 125 | 2 | 2020-08-13 | 15:24:00 | 17:24:00 | 02:00:00
-- out 1 | 157 | 1 | 2020-08-13 | 08:01:00 | 15:01:00 | 07:00:00
-- out 1 | 125 | 3 | 2020-08-14 | 08:10:00 | 15:21:00 | 07:11:00
-- out 1 | 125 | 4 | 2020-08-14 | 15:24:00 | 17:24:00 | 02:00:00
-- out 1 | 157 | 2 | 2020-08-14 | 08:01:00 | 15:01:00 | 07:00:00

Split date into multiple date ranges in Hive or SQL Server

I would like to convert the below dates into different date ranges, here emp belongs to Chennai location from 2019-07-25 to 2099-02-14, but in between, emp worked from DEL between 2020-02-15 and 2020-02-23.
So I would like to convert above dates into below date ranges
I have tried below in SQL Server and it works:
Test Setup
CREATE TABLE Employee(EmployeeId INT, fromDate date, todate date, Placename VARCHAR(100), PlaceCode VARCHAR(100))
INSERT INTO Employee
VALUES(1111,'2019-07-25','2099-02-14','CHENNAI','MAA'),
(1111,'2020-02-15','2020-02-23','DELHI','DEL');
Query to Execute
;WITH CTE_Ranges AS
(
SELECT EmployeeId, fromdate, lag(fromdate,1) OVER(PARTITION BY EmployeeId ORDER BY fromdate) previousfromDate,todate
, lead(todate,1) OVER(PARTITION BY EmployeeId ORDER BY todate) nexttodate, placename, placecode from Employee
)
--Handle the Maximum and minimum dates
SELECT * FROM
(
SELECT EmployeeId, fromdate, DATEADD(day,-1,lead(fromdate) over(partition by EmployeeId ORDER BY fromDate)) as todate, PlaceName, PlaceCode
FROM CTE_Ranges
UNION ALL
SELECT EmployeeId, DATEADD(day,1,lag(todate) over(partition by EmployeeId ORDER BY todate)) as fromdate, todate,PlaceName, PlaceCode
FROM CTE_Ranges) AS t
WHERE fromdate is not null and todate is not null
UNION ALL
--Now handle normal date ranges
SELECT EmployeeId, fromDate,todate, placename, placecode
from cte_ranges
WHERE previousfromdate is not null and nexttodate is not null
order by fromdate
Resultset
+------------+------------+------------+-----------+-----------+
| EmployeeId | fromdate | todate | PlaceName | PlaceCode |
+------------+------------+------------+-----------+-----------+
| 1111 | 2019-07-25 | 2020-02-14 | CHENNAI | MAA |
| 1111 | 2020-02-15 | 2020-02-23 | DELHI | DEL |
| 1111 | 2020-02-24 | 2099-02-14 | CHENNAI | MAA |
+------------+------------+------------+-----------+-----------+
To handle multiple levels of nesting, you can unpivot the data and
with e as (
-- unpivot the dates
select employeeid, fromdate as dte,
placename, placecode
from t
union all
select employeeid, enddate, null, null
from t
),
e2 as (
-- impute the intermediate placenames
select e.*,
max(placename) over (partition by employeeid, grp) as imputed_placename
from (select e.*,
count(placename) over (partition by employeeid order by dte) as grp
from e
) e
)
select employeeid, fromdate,
dateadd(day, -1, lead(fromdate) over (partition by employeeid order by dte)) as enddate,
placename, placecode
from e1;

Find the customers and other metrics based on the purchase frequency new & repeat

I am trying to find the customer count and sales by the type of customer (New and Returning) and the number of times they have purchased.
txn_date Customer_ID Transaction_Number Sales Reference(not in the SQL table) customer type (not in the sql table)
1/2/2019 1 12345 $10 Second Purchase SLS Repeat
4/3/2018 1 65890 $20 First Purchase SLS Repeat
3/22/2019 3 64453 $30 First Purchase SLS new
4/3/2019 4 88567 $20 First Purchase SLS new
5/21/2019 4 85446 $15 Second Purchase SLS new
1/23/2018 5 89464 $40 First Purchase SLS Repeat
4/3/2019 5 99674 $30 Second Purchase SLS Repeat
4/3/2019 6 32224 $20 Second Purchase SLS Repeat
1/23/2018 6 46466 $30 First Purchase SLS Repeat
1/20/2018 7 56558 $30 First Purchase SLS new
I am using the below code to get the aggregate sales and customer count for the total customers:
select seqnum, count(distinct customer_id), sum(sales) from (
select co.*,
row_number() over (partition by customer_id order by txn_date) as seqnum
from somya co)
group by seqnum
order by seqnum;
I want to get the same data by the customer type:
for example for the new customers my result should show:
New Customers Customer_Count Sum(Sales)
1st Purchase 3 $80
2nd Purchase 1 $15
Returning Customers Customer_Count Sum(Sales)
1st Purchase 3 $90
2nd Purchase 3 $60
I am trying the below query to get the data for new and repeat customers:
New Customers:
select seqnum, count(distinct customer_id), sum(sales)
from (
select co.*,
row_number() over (partition by customer_id order by trunc(txn_date)) as seqnum,
MIN (TRUNC (TXN_DATE)) OVER (PARTITION BY customer_id) as MIN_TXN_DATE
from somya co
)
where MIN_TXN_DATE between '01-JAN-19' and '31-DEC-19'
group by seqnum
order by seqnum asc;
Returning Customers:
select seqnum, count(distinct customer_id), sum(sales)
from (
select co.*,
row_number() over (partition by customer_id order by trunc(txn_date)) as seqnum,
MIN (TRUNC (TXN_DATE)) OVER (PARTITION BY customer_id) as MIN_TXN_DATE
from somya co
)
where MIN_TXN_DATE <'01-JAN-19'
group by seqnum
order by seqnum asc;
I am not able to figure out what is wrong with my query or if there is a problem with my logic.
This is just a sample data, I have transactions from all the years in my data base so I need to narrow the transaction date in the query but as soon as I narrowing down the data using the transaction date the repeat customer query doesnt give me anything and the new customer query gives me the total customer for that period.
If I understand correctly, you need to know the first time someone becomes a customer. And then use this:
select (case when first_year < 2019 then 'returning' else 'new' end) as custtype,
seqnum, count(*), sum(sales)
from (select co.*,
row_number() over (partition by customer_id, extract(year from txn_date) order by txn_date) as seqnum,
min(extract(year from txn_date)) over (partition by customer_id) as first_year
from somya co
) s
where txn_date >= date '2019-01-01' and
txn_date < date '2020-01-01'
group by (case when first_year < 2019 then 'returning' else 'new' end),
seqnum
order by custtype, seqnum;
You can categorize your sales data to assign a customer type and a purchase sequence using windowing functions, like this:
SELECT sd.txn_date,
sd.customer_id,
sd.transaction_number,
sd.sales,
case when min(txn_date) over ( partition by customer_id ) < DATE '2019-01-01'
AND max(txn_date) OVER ( partition by customer_id ) >= DATE '2019-01-01'
THEN 'Repeat'
ELSE 'New' END customer_type,
row_number() over ( partition by customer_id order by txn_date) purchase_sequence
FROM sales_data sd
+-----------+-------------+--------------------+-------+---------------+-------------------+
| TXN_DATE | CUSTOMER_ID | TRANSACTION_NUMBER | SALES | CUSTOMER_TYPE | PURCHASE_SEQUENCE |
+-----------+-------------+--------------------+-------+---------------+-------------------+
| 03-APR-18 | 1 | 65890 | 20 | Repeat | 1 |
| 02-JAN-19 | 1 | 12345 | 10 | Repeat | 2 |
| 22-MAR-19 | 3 | 64453 | 30 | New | 1 |
| 03-APR-19 | 4 | 88567 | 20 | New | 1 |
| 21-MAY-19 | 4 | 85446 | 15 | New | 2 |
| 23-JAN-18 | 5 | 89464 | 40 | Repeat | 1 |
| 03-APR-19 | 5 | 99674 | 30 | Repeat | 2 |
| 23-JAN-18 | 6 | 46466 | 30 | Repeat | 1 |
| 03-APR-19 | 6 | 32224 | 20 | Repeat | 2 |
| 20-JAN-18 | 7 | 56558 | 30 | New | 1 |
+-----------+-------------+--------------------+-------+---------------+-------------------+
Then, you can wrap that in a common table expression (aka "WITH" clause) and summarize by the customer type and purchase sequence:
WITH categorized_sales_data AS (
SELECT sd.txn_date,
sd.customer_id,
sd.transaction_number,
sd.sales,
case when min(txn_date) over ( partition by customer_id ) < DATE '2019-01-01' AND max(txn_date) OVER ( partition by customer_id ) >= DATE '2019-01-01' THEN 'Repeat' ELSE 'New' END customer_type,
row_number() over ( partition by customer_id order by txn_date) purchase_sequence
FROM sales_data sd)
SELECT customer_type, purchase_sequence, count(*), sum(sales)
FROM categorized_sales_data
group by customer_type, purchase_sequence
order by customer_type, purchase_sequence
+---------------+-------------------+----------+------------+
| CUSTOMER_TYPE | PURCHASE_SEQUENCE | COUNT(*) | SUM(SALES) |
+---------------+-------------------+----------+------------+
| New | 1 | 3 | 80 |
| New | 2 | 1 | 15 |
| Repeat | 1 | 3 | 90 |
| Repeat | 2 | 3 | 60 |
+---------------+-------------------+----------+------------+
Here's a full SQL with test data:
with sales_data (txn_date, Customer_ID, Transaction_Number, Sales ) as (
SELECT TO_DATE('1/2/2019','MM/DD/YYYY'), 1, 12345, 10 FROM DUAL UNION ALL
SELECT TO_DATE('4/3/2018','MM/DD/YYYY'), 1, 65890, 20 FROM DUAL UNION ALL
SELECT TO_DATE('3/22/2019','MM/DD/YYYY'), 3, 64453, 30 FROM DUAL UNION ALL
SELECT TO_DATE('4/3/2019','MM/DD/YYYY'), 4, 88567, 20 FROM DUAL UNION ALL
SELECT TO_DATE('5/21/2019','MM/DD/YYYY'), 4, 85446, 15 FROM DUAL UNION ALL
SELECT TO_DATE('1/23/2018','MM/DD/YYYY'), 5, 89464, 40 FROM DUAL UNION ALL
SELECT TO_DATE('4/3/2019','MM/DD/YYYY'), 5, 99674, 30 FROM DUAL UNION ALL
SELECT TO_DATE('4/3/2019','MM/DD/YYYY'), 6, 32224, 20 FROM DUAL UNION ALL
SELECT TO_DATE('1/23/2018','MM/DD/YYYY'), 6, 46466, 30 FROM DUAL UNION ALL
SELECT TO_DATE('1/20/2018','MM/DD/YYYY'), 7, 56558, 30 FROM DUAL ),
-- Query starts here
/* WITH */ categorized_sales_data AS (
SELECT sd.txn_date,
sd.customer_id,
sd.transaction_number,
sd.sales,
case when min(txn_date) over ( partition by customer_id ) < DATE '2019-01-01' AND max(txn_date) OVER ( partition by customer_id ) >= DATE '2019-01-01' THEN 'Repeat' ELSE 'New' END customer_type,
row_number() over ( partition by customer_id order by txn_date) purchase_sequence
FROM sales_data sd)
SELECT customer_type, purchase_sequence, count(*), sum(sales)
FROM categorized_sales_data
group by customer_type, purchase_sequence
order by customer_type, purchase_sequence
Response to comment from OP
all the customers whose first purchase date is in 2019 would be a new customer. Any customer who has transacted in 2019 but their first purchase date is before 2019 would be a repeat customer
So, change
case when min(txn_date) over ( partition by customer_id ) < DATE '2019-01-01'
AND max(txn_date) OVER ( partition by customer_id ) >= DATE '2019-01-01'
THEN 'Repeat' ELSE 'New' END customer_type
to
case when min(txn_date) over ( partition by customer_id )
BETWEEN DATE '2019-01-01' AND DATE '2020-01-01' - INTERVAL '1' SECOND
THEN 'New' ELSE 'Repeat' END customer_type
i.e., if and only if a customer's first purchase was in 2019 then they are "new".

Pivot DateTime fields by date

I have a table that contains employee 'punches' (clock ins/outs) each punch can be an 'in'(punch_type=1) or an 'out' (punch_type=2).
The table is formatted as follows:
emp_num | report_date | punch_time | punch_type
-----------------------------------------------------------
1 | 2018-04-20 |2018-04-20 04:46:00.000 | 1
1 | 2018-04-20 |2018-04-20 06:58:00.000 | 2
1 | 2018-04-20 |2018-04-20 08:10:00.000 | 1
1 | 2018-04-20 |2018-04-20 12:00:00.000 | 2
I am trying to get the first 'punch' (clock in) and the following 'punch' (clock out) in the same row. Then, of course, any following would be the same.
Desired output:
emp_num | report_date | punch_in | punch_out
-----------------------------------------------------------
1 | 2018-04-20 |2018-04-20 04:46:00.000 | 2018-04-20 06:58:00.000
1 | 2018-04-20 |2018-04-20 08:10:00.000 | 2018-04-20 12:00:00.000
Keep in mind there may be multiple punch in/out combos in one day as shown in the example.
Any help would be greatly appreciated!
First you want to know which punch out time belongs to which punch in time. Answer: the nth punch out time belongs to the nth punch in time. So number your records:
select
p_in.emp_num,
p_in.report_date,
p_in.punch_time as punch_in,
p_out.punch_time as punch_out
from
(
select
emp_num,
report_date,
punch_time,
row_number() over (partition by emp_num, report_date order by punch_time) as rn
from mytable
where punch_type = 1
) p_in
left join
(
select
emp_num,
report_date,
punch_time,
row_number() over (partition by emp_num, report_date order by punch_time) as rn
from mytable
where punch_type = 2
) p_out on p_out.emp_num = p_in.emp_num
and p_out.report_date = p_in.report_date
and p_out.rn = p_in.rn
order by p_in.emp_num, p_in.report_date, punch_in;
select emp_num, report_date, max(case when punch_type=1 then punch_time else null end) punch_in,
max(case when punch_type=2 then punch_time else null end) punch_out
from (select *, row_number() over(partition by emp_num, report_date, punch_type order by emp_num, report_date, punch_time) value from yourtable )a
group by emp_num, report_date, value

Count and pivot a table by date

I would like to identify the returning customers from an Oracle(11g) table like this:
CustID | Date
-------|----------
XC321 | 2016-04-28
AV626 | 2016-05-18
DX970 | 2016-06-23
XC321 | 2016-05-28
XC321 | 2016-06-02
So I can see which customers returned within various windows, for example within 10, 20, 30, 40 or 50 days. For example:
CustID | 10_day | 20_day | 30_day | 40_day | 50_day
-------|--------|--------|--------|--------|--------
XC321 | | | 1 | |
XC321 | | | | 1 |
I would even accept a result like this:
CustID | Date | days_from_last_visit
-------|------------|---------------------
XC321 | 2016-05-28 | 30
XC321 | 2016-06-02 | 5
I guess it would use a partition by windowing clause with unbounded following and preceding clauses... but I cannot find any suitable examples.
Any ideas...?
Thanks
No need for window functions here, you can simply do it with conditional aggregation using CASE EXPRESSION :
SELECT t.custID,
COUNT(CASE WHEN (last_visit- t.date) <= 10 THEN 1 END) as 10_day,
COUNT(CASE WHEN (last_visit- t.date) between 11 and 20 THEN 1 END) as 20_day,
COUNT(CASE WHEN (last_visit- t.date) between 21 and 30 THEN 1 END) as 30_day,
.....
FROM (SELECT s.custID,
LEAD(s.date) OVER(PARTITION BY s.custID ORDER BY s.date DESC) as last_visit
FROM YourTable s) t
GROUP BY t.custID
Oracle Setup:
CREATE TABLE customers ( CustID, Activity_Date ) AS
SELECT 'XC321', DATE '2016-04-28' FROM DUAL UNION ALL
SELECT 'AV626', DATE '2016-05-18' FROM DUAL UNION ALL
SELECT 'DX970', DATE '2016-06-23' FROM DUAL UNION ALL
SELECT 'XC321', DATE '2016-05-28' FROM DUAL UNION ALL
SELECT 'XC321', DATE '2016-06-02' FROM DUAL;
Query:
SELECT *
FROM (
SELECT CustID,
Activity_Date AS First_Date,
COUNT(1) OVER ( PARTITION BY CustID
ORDER BY Activity_Date
RANGE BETWEEN CURRENT ROW AND INTERVAL '10' DAY FOLLOWING )
- 1 AS "10_Day",
COUNT(1) OVER ( PARTITION BY CustID
ORDER BY Activity_Date
RANGE BETWEEN CURRENT ROW AND INTERVAL '20' DAY FOLLOWING )
- 1 AS "20_Day",
COUNT(1) OVER ( PARTITION BY CustID
ORDER BY Activity_Date
RANGE BETWEEN CURRENT ROW AND INTERVAL '30' DAY FOLLOWING )
- 1 AS "30_Day",
COUNT(1) OVER ( PARTITION BY CustID
ORDER BY Activity_Date
RANGE BETWEEN CURRENT ROW AND INTERVAL '40' DAY FOLLOWING )
- 1 AS "40_Day",
COUNT(1) OVER ( PARTITION BY CustID
ORDER BY Activity_Date
RANGE BETWEEN CURRENT ROW AND INTERVAL '50' DAY FOLLOWING )
- 1 AS "50_Day",
ROW_NUMBER() OVER ( PARTITION BY CustID ORDER BY Activity_Date ) AS rn
FROM Customers
)
WHERE rn = 1;
Output
USTID FIRST_DATE 10_Day 20_Day 30_Day 40_Day 50_Day RN
------ ------------------- ---------- ---------- ---------- ---------- ---------- ----------
AV626 2016-05-18 00:00:00 0 0 0 0 0 1
DX970 2016-06-23 00:00:00 0 0 0 0 0 1
XC321 2016-04-28 00:00:00 0 0 1 2 2 1
Here is an answer that works for me, I have based it on your answers above, thanks for contributions from MT0 and Sagi:
SELECT CustID,
visit_date,
Prev_Visit ,
COUNT( CASE WHEN (Days_between_visits) <=10 THEN 1 END) AS "0-10_day" ,
COUNT( CASE WHEN (Days_between_visits) BETWEEN 11 AND 20 THEN 1 END) AS "11-20_day" ,
COUNT( CASE WHEN (Days_between_visits) BETWEEN 21 AND 30 THEN 1 END) AS "21-30_day" ,
COUNT( CASE WHEN (Days_between_visits) BETWEEN 31 AND 40 THEN 1 END) AS "31-40_day" ,
COUNT( CASE WHEN (Days_between_visits) BETWEEN 41 AND 50 THEN 1 END) AS "41-50_day" ,
COUNT( CASE WHEN (Days_between_visits) >50 THEN 1 END) AS "51+_day"
FROM
(SELECT CustID,
visit_date,
Lead(T1.visit_date) over (partition BY T1.CustID order by T1.visit_date DESC) AS Prev_visit,
visit_date - Lead(T1.visit_date) over (
partition BY T1.CustID order by T1.visit_date DESC) AS Days_between_visits
FROM T1
) T2
WHERE Days_between_visits >0
GROUP BY T2.CustID ,
T2.visit_date ,
T2.Prev_visit ,
T2.Days_between_visits;
This returns:
CUSTID | VISIT_DATE | PREV_VISIT | DAYS_BETWEEN_VISIT | 0-10_DAY | 11-20_DAY | 21-30_DAY | 31-40_DAY | 41-50_DAY | 51+DAY
XC321 | 2016-05-28 | 2016-04-28 | 30 | | | 1 | | |
XC321 | 2016-06-02 | 2016-05-28 | 5 | 1 | | | | |