I have generated a table
my initial code for this is
select segment ,count(distinct user_id)as value,SEGMENTED_DATE from weekly_customer_RFM_TABLE
where segment in('About to sleep','Promising','champion','Loyal_customer',
'Potential_Loyalist','At_Risk','Need_Attention','New_customer',
'Hibernating','Cant_loose')
and SEGMENTED_DATE between '2022-10-07' and '2022-10-28'
Group by segment,SEGMENTED_DATE
I am expecting to make a consolidated table like this condition
from first table in Segmented column
1.i want to subtract the segment value from date 14/10/22 – 7/10/22 and this will be named as ‘week2- week1’ in changed_time column
2.Following segment value from 21/10/22 -14/10/22 will be stated as week 3- week2
3. Following Segment Value from 28/10/22 -21/10/22 will be stated as week 4 -week3
so far i have come up this but it is not that close what i want
for this my code was like
select '7to14'as change_date,a.* from (select sum(SEGMENT = 'champion' AND SEGMENTED_DATE = '2022-10-14') -
SUM(SEGMENT = 'champion' and SEGMENTED_DATE = '2022-10-07')
as total_change_champion_14,
sum(SEGMENT = 'Loyal_customer' AND SEGMENTED_DATE = '2022-10-14') -
SUM(SEGMENT = 'Loyal_customer' and SEGMENTED_DATE = '2022-10-07')
as total_change_Loyal_14,
sum(SEGMENT = 'Potential_Loyalist' AND SEGMENTED_DATE = '2022-10-14') -
SUM(SEGMENT = 'Potential_Loyalist' and SEGMENTED_DATE = '2022-10-07')
as total_change_Potential_loyal_14,
sum(SEGMENT = 'Promising' AND SEGMENTED_DATE = '2022-10-14') -
SUM(SEGMENT = 'Promising' and SEGMENTED_DATE = '2022-10-07')
as total_change_promising_14,
sum(SEGMENT = 'At_Risk' AND SEGMENTED_DATE = '2022-10-14') -
SUM(SEGMENT = 'At_Risk' and SEGMENTED_DATE = '2022-10-07')
as total_change_At_risk14,
sum(SEGMENT = 'Need_Attention' AND SEGMENTED_DATE = '2022-10-14') -
SUM(SEGMENT = 'Need_Attention' and SEGMENTED_DATE = '2022-10-07')
as total_change_Need_attention_14,
sum(SEGMENT = 'New_customer' AND SEGMENTED_DATE = '2022-10-14') -
SUM(SEGMENT = 'New_customer' and SEGMENTED_DATE = '2022-10-07')
as total_change_New_customer_14,
sum(SEGMENT = 'About to sleep' AND SEGMENTED_DATE = '2022-10-14') -
SUM(SEGMENT = 'About to sleep' and SEGMENTED_DATE = '2022-10-07')
as total_change_About_to_sleep_14,
sum(SEGMENT = 'Hibernating' AND SEGMENTED_DATE = '2022-10-14') -
SUM(SEGMENT = 'Hibernating' and SEGMENTED_DATE = '2022-10-07')
as total_change_Hibernating_14,
sum(SEGMENT = 'Cant_loose' AND SEGMENTED_DATE = '2022-10-14') -
SUM(SEGMENT = 'Cant_loose' and SEGMENTED_DATE = '2022-10-07')
as total_change_cant_loose_14
from weekly_customer_RFM_TABLE
)a
kindly help me to get this format or how could i achieve this output in mysql
Thanks in advance
7