
                   
--------------------------Revenue Leakage --------------------------

drop table if exists public.om_revenue_leakage_qty_match;



create table public.om_revenue_leakage_qty_match
as
select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount) as revenue_leakage,
a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at as date) as concat1
from (
select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku,
a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_amount as a_payment_amount,
b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku,
b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_amount as b_payment_amount
from (
	select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_amount,payment_status_sort,quantity
		from (
				select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_amount desc) as R
				from (
						select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price  as payment_amount,
						case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity
						from RAENA_ORDER_MANAGEMENT.order o
						inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
						where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%'
					) inn
			) m where R=1
) a
left join
(
		select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.quantity,oi.payment_price  as payment_amount
		from RAENA_ORDER_MANAGEMENT.order o
		inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
		where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%'
) b
on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku and a.quantity=b.quantity and a.customer_id isnull and b.customer_id isnull
and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS'
where a.customer_id isnull
) c
where b_reseller_id isnull
--group by 1,2,3,4,5,6
union --cusotmer_id notnull cases
select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount) as payment_amount,
a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at as date)
from (
		select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku,
		a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_amount as a_payment_amount,
		b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku,
		b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_amount as b_payment_amount
		from (
		select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_amount,payment_status_sort,quantity
		from (
				select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_amount desc) as R
				from (
						select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price  as payment_amount,
						case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity
						from RAENA_ORDER_MANAGEMENT.order o
						inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
						where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%'
					) inn
			) m where R=1
			) a
		left join
		(
				select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.quantity,oi.payment_price  as payment_amount
				from RAENA_ORDER_MANAGEMENT.order o
				inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
				where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367' --and sku like 'AVS019%'
		) b
		on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku and a.quantity=b.quantity and a.customer_id = b.customer_id
		and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS'
		where a.customer_id notnull
) c
where b_reseller_id isnull;


drop table if exists public.om_revenue_leakage_qty_not_match;




create table public.om_revenue_leakage_qty_not_match
as
select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount)-(b_payment_amount) as revenue_leakage,
a_order_placed_by || a_reseller_id || a_sku || a_payment_status || cast(a_created_at as date) as concat2
from (
		select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku,
		a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount,
		b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku,
		b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount
		from (
			select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity from (
				select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R from (
				select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price,
				case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity
				from RAENA_ORDER_MANAGEMENT.order o
				inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
				where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01' ----and o.reseller_id='014b731e-e1f6-4d9e-b84e-8715194bc624'  --and sku like 'AVS019%'
				) inn
				) m where R=1
		) a
		inner join
		(
			select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price
				from
					(
					select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,
					cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price
					from RAENA_ORDER_MANAGEMENT.order o
					inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
					where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id isnull --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367'  --and sku like 'SF005%'
					group by 1,2,3,4,5,6
					) ist
					inner join
					(
					select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at
					from
						(
						select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,
						row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R
						from RAENA_ORDER_MANAGEMENT.order o
						inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
						where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id isnull --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367'  --and sku like 'SF005%'
						) a
						where a.R=1
					) nxt
					on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id
					and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date)
		) b
		on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku
		and  b.quantity < a.quantity and a.payment_price>b.payment_price
		and a.customer_id isnull and b.customer_id isnull
		and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS'
) c
--group by 1,2,3,4,5,6
union --customer-d notnull
select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_sku,a_payment_status,a_created_at,(a_payment_amount)-(b_payment_amount) as revenue_leakage,
a_order_placed_by || a_reseller_id || a_customer_id || a_sku || a_payment_status || cast(a_created_at as date)
from (
		select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku,
		a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount,
		b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku,
		b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount
		from (
				select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity
				from (
				select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R
				from  (
				select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price,
				case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity
				from RAENA_ORDER_MANAGEMENT.order o
				inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
				where o.payment_status IN ('Expired','Failed') and o.status='Order_Placed' and cast(o.created_at as date) >='2022-01-01'
				--and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367'  --and sku like 'SF005%'
				) inn
				) m where R=1
		) a
		inner join
		(
				select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price
				from
					(
					select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,
					cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price
					from RAENA_ORDER_MANAGEMENT.order o
					inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
					where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id notnull --and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367'  --and sku like 'SF005%'
					group by 1,2,3,4,5,6
					) ist
					inner join
					(
					select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at
					from
						(
						select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,
						row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R
						from RAENA_ORDER_MANAGEMENT.order o
						inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
						where o.payment_status='Paid' and cast(o.created_at as date) >='2022-01-01' and customer_id notnull
						--and o.reseller_id='f9a6fc9e-0b6c-4fe9-a314-090b50cf4367'  --and sku like 'SF005%'
						) a
						where a.R=1
					) nxt
					on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id
					and ist.customer_id=nxt.customer_id and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date)
				) b
		on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku
		and  b.quantity < a.quantity and a.payment_price>b.payment_price
		and a.customer_id = b.customer_id
		and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS'
) c
--group by 1,2,3,4,5,6
order by 2,4;



drop table if exists OM_Revenue_leakage;



create table OM_Revenue_leakage
as
select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_payment_status,a_created_at,sum(revenue_leakage) as revenue_leakage from (
select *
from
(
select 	qm.*,qnm.concat2
from public.om_revenue_leakage_qty_match qm
left join public.om_revenue_leakage_qty_not_match qnm on qm.concat1=qnm.concat2
) n where concat2 isnull
) s
group by 1,2,3,4,5
union
--qty not match
select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_payment_status,a_created_at,sum(a_payment_amount)-sum(b_payment_amount) as revenue_leakage
from (
		select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku,
		a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount,
		b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku,
		b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount
		from (
			select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity from (
				select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R from (
				select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price,
				case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity
				from RAENA_ORDER_MANAGEMENT.order o
				inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
				where o.payment_status!='Paid' and payment_status!='Pending' and o.status='Order_Placed' and cast(o.created_at as date) >= '2022-01-01' --and o.reseller_id='014b731e-e1f6-4d9e-b84e-8715194bc624'  and sku like 'AVS019%'
				) inn
				) m where R=1
		) a
		inner join
		(
			select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price
				from
					(
					select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,
					cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price
					from RAENA_ORDER_MANAGEMENT.order o
					inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
					where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id isnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4'  and sku like 'SF005%'
					group by 1,2,3,4,5,6
					) ist
					inner join
					(
					select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at
					from
						(
						select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,
						row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R
						from RAENA_ORDER_MANAGEMENT.order o
						inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
						where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id isnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4'  and sku like 'SF005%'
						) a
						where a.R=1
					) nxt
					on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id
					and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date)
		) b
		on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku
		and  b.quantity < a.quantity and a.payment_price>b.payment_price
		and a.customer_id isnull and b.customer_id isnull
		and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS'
) c
group by 1,2,3,4,5
union --customer-d notnull
select distinct a_order_placed_by,a_reseller_id,a_customer_id,a_payment_status,a_created_at,sum(a_payment_amount)-sum(b_payment_amount) as revenue_leakage
from (
		select a.order_placed_by as a_order_placed_by,a.reseller_id as a_reseller_id,a.customer_id as a_customer_id,a.sku as a_sku,
		a.payment_status as a_payment_status,a.created_at as a_created_at,a.quantity as a_quantity,a.payment_price as a_payment_amount,
		b.order_placed_by as b_order_placed_by,b.reseller_id as b_reseller_id,b.customer_id as b_customer_id,b.sku as b_sku,
		b.payment_status as b_payment_status,b.created_at as b_created_at,b.quantity as b_quantity,b.payment_price as b_payment_amount
		from (
				select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at,payment_price,payment_status_sort,quantity
				from (
				select *,row_number() over ( partition by reseller_id,sku,payment_status,cast(created_at as date) order by created_at,payment_status_sort,payment_price desc) as R
				from  (
				select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,oi.payment_price,
				case when payment_status='Paid' then 1 when payment_status='Expired' then 2 else 3 end as payment_status_sort,oi.quantity
				from RAENA_ORDER_MANAGEMENT.order o
				inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
				where o.payment_status!='Paid' and payment_status!='Pending' and o.status='Order_Placed' and cast(o.created_at as date) >= '2022-01-01' --and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4'  and sku like 'SF005%'
				) inn
				) m where R=1
		) a
		inner join
		(
				select ist.order_placed_by,ist.reseller_id,ist.customer_id,ist.sku,ist.payment_status,nxt.created_at,ist.quantity,ist.payment_price
				from
					(
					select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,
					cast(o.created_at as date) as created_at,sum(oi.quantity) as quantity,sum(oi.payment_price) as payment_price
					from RAENA_ORDER_MANAGEMENT.order o
					inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
					where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id notnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4'  and sku like 'SF005%'
					group by 1,2,3,4,5,6
					) ist
					inner join
					(
					select order_placed_by,reseller_id,customer_id,sku,payment_status,created_at
					from
						(
						select distinct o.order_placed_by,o.reseller_id,o.customer_id,sku,payment_status,o.created_at,
						row_number() over (partition by o.order_placed_by,o.reseller_id,o.customer_id,sku,cast(o.created_at as date) order by o.created_at desc) as R
						from RAENA_ORDER_MANAGEMENT.order o
						inner join RAENA_ORDER_MANAGEMENT.order_item oi  on o.id=oi.order_id
						where o.payment_status='Paid' and cast(o.created_at as date) >= '2022-01-01' and customer_id notnull--and o.reseller_id='7b998baf-7091-41c6-b00f-2a1247ecd0f4'  and sku like 'SF005%'
						) a
						where a.R=1
					) nxt
					on ist.order_placed_by=nxt.order_placed_by and ist.reseller_id=nxt.reseller_id
					and ist.customer_id=nxt.customer_id and ist.sku=nxt.sku and ist.created_at=cast(nxt.created_at as date)
				) b
		on a.order_placed_by=b.order_placed_by and a.reseller_id=b.reseller_id and a.sku=b.sku
		and  b.quantity < a.quantity and a.payment_price>b.payment_price
		and a.customer_id = b.customer_id
		and b.created_at between a.created_at and a.created_at + INTERVAL '24 HOURS'
) c
group by 1,2,3,4,5
order by 2,4;




drop table if exists OM_Revenue_leakage_dump;




create table OM_Revenue_leakage_dump
as
select * from public.om_revenue_leakage_qty_match
union
select * from public.om_revenue_leakage_qty_not_match;


drop table if exists public.om_conversion_order_data_1;




create table public.om_conversion_order_data_1
as
select distinct cast(created_at + interval '7 Hours' as date) as created_date,date_part('year',created_at + interval '7 Hours') as order_year,
date_part('month',created_at + interval '7 Hours' ) as order_month,To_char(created_date + interval '7 Hours','month') Month_name,
a.reseller_id,id as order_id,json_extract_path_text(reseller_info,'email',TRUE) as transacted_email,replace(json_extract_path_text(reseller_info,'mobile',TRUE),'+','') as transacted_mobile,
payment_amount,shipping_to,order_placed_by,b.Monthly_Tier,c.Max_Tier_of_any_month,
rank() over(partition by a.reseller_id order by date_part('year',created_at + interval '7 Hours' ),date_part('month',created_at + interval '7 Hours')) as R
from raena_order_management.order a
left join (
			select distinct 
			reseller_id,order_year,order_month,
			case when revenue<2000000 then '<2 mn'  when revenue>=2000000 and revenue<=10000000 then '2-10 mn' when revenue>10000000 then '10+ mn' end as Monthly_Tier
			from (
			select date_part('year',created_at + interval '7 Hours' ) as order_year,date_part('month',created_at + interval '7 Hours' ) as order_month,reseller_id
			,sum(payment_amount) as revenue
			from raena_order_management.order 
			where payment_status='Paid' and cast(is_archived as varchar)='false' 
			group by date_part('year',created_at + interval '7 Hours' ),date_part('month',created_at + interval '7 Hours' ),reseller_id
			) where reseller_id notnull
) b on a.reseller_id=b.reseller_id and date_part('year',a.created_at + interval '7 Hours' ) =b.order_year and date_part('month',a.created_at + interval '7 Hours' )=b.order_month
left join (
			select distinct 
			reseller_id,case when revenue<2000000 then '<2 mn'  when revenue>=2000000 and revenue<=10000000 then '2-10 mn' when revenue>10000000 then '10+ mn' end as Max_Tier_of_any_month
			from (	
			select reseller_id,max(payment_amount) as revenue
			from raena_order_management.order 
			where payment_status='Paid' and cast(is_archived as varchar)='false' 
			group by reseller_id
		) where reseller_id notnull
) c on a.reseller_id=c.reseller_id 
where payment_status='Paid' and cast(is_archived as varchar)='false' and cast(is_campaign as varchar)='false'
order by 1,3;      
                  
                 
             
                  
                 
         
