Untitled

 avatar
unknown
plain_text
2 years ago
4.4 kB
6
Indexable
necesito que me ayudes a revisar unas consultas de sql

Tengo este codigo:

-- netsales MX by month
with qry1 as (
select d.year_month, 
country_iso, 
count(*) gross_sales
from dwh.fact_booking
left join dwh.dim_date d on fk_scheduled_delivery_date=d.sk_date
left join dwh.dim_geography on fk_client_geo=sk_geography
where flag_gross_sale is true
and country_iso='MX'
group by 1,2
)
,qry2 as (
select d.year_month, 
country_iso,
count(*) cancelled_sales
from dwh.fact_booking
left join dwh.dim_date d on fk_credit_memo_creation_date=d.sk_date
left join dwh.dim_geography on fk_client_geo=sk_geography
where flag_cancelled_sale is true
and country_iso='MX'
group by 1,2
order by 1 desc)
select qry1.*, qry2.cancelled_sales, qry1.gross_sales-qry2.cancelled_sales as net_sales
from qry1
left join qry2 on qry1.year_month = qry2.year_month
and qry1.country_iso = qry2.country_iso
order by 1 desc

y me da de resultado esto:

Mayo ventas brutas 586	 ventas canceladas 55 ventas netas 	531

y tengo este otro codigo 

/*Sold Cars */
select
	*,	
	x.amt_olp_real_published_price / x.alg_amt_market_price as price_index,-- amt_olp_sale_price  & alg_amt_market_price
	(x.amt_olp_real_published_price - x.amt_olp_offer_price) / (x.amt_olp_real_published_price) as gross_margin,
	x.amt_olp_real_published_price - x.amt_olp_offer_price as gross_profit
from
	(
	select
		dc.bk_car_stock as stock_id,
		dc.car_brand as brand,
		dc."model" as modelo, 
		dc."year" as car_year,
		cs.body_type as body_type, 
		cs.fuel,
		mi.qty_olp_inventory_days_published as pub_days,
		mi.fk_olp_car_inventory_status as total_inv_days,
		mi.qty_olp_km as km_, 
		dd."date" as sale_date,
		dd2."date" as cancelation_date,
		dd3."date" as sale_date_factbook,
		dpt."type" as canal_book, 
		mi.alg_amt_market_price,
		mi.amt_olp_real_published_price,
		mi.amt_olp_offer_price,
		case
			when mi.amt_olp_real_published_price <= 100000 then '0-110'
			when (mi.amt_olp_real_published_price > 100000 and mi.amt_olp_real_published_price <= 200000) then '100-200'
			when (mi.amt_olp_real_published_price > 200000 and mi.amt_olp_real_published_price <= 300000) then '200-300'
			when (mi.amt_olp_real_published_price > 300000 and mi.amt_olp_real_published_price <= 400000) then '300-400'
			when (mi.amt_olp_real_published_price > 400000 and mi.amt_olp_real_published_price <= 500000) then '300-400'
			when (mi.amt_olp_real_published_price > 500000 and mi.amt_olp_real_published_price <= 600000) then '500-600'
			when (mi.amt_olp_real_published_price > 600000 and mi.amt_olp_real_published_price <= 700000) then '600-700'
			when (mi.amt_olp_real_published_price > 700000 and mi.amt_olp_real_published_price <= 800000) then '700-800'
			when mi.amt_olp_real_published_price >= 800000 then '800+'
			else 'Other'
			end as price_bucket,
		dis.bk_inventory_status,
		case
			when mi.amt_olp_sale_price notnull then true
			else false
			end as hasDiscount,
		case
			when mi.amt_olp_sale_price notnull then mi.amt_olp_real_published_price - mi.amt_olp_sale_price
			end as discount,
		row_number() over (partition by dc.bk_car_stock
	order by dd1.sk_date asc) as rn
	from
		dwh.mv_inventory mi
			left join dwh.dim_car_stock dc on mi.fk_car_stock = dc.sk_car_stock
			left join dwh.dim_car_sku cs on mi.fk_car_sku = cs.sk_car_sku
			left join dwh.dim_date dd on mi.fk_olp_sale_date = dd.sk_date
			left join dwh.dim_date dd1 on mi.offer_fk_algorithm_valid_until_date = dd1.sk_date 
			left join dwh.dim_segment_type ds on mi.alg_fk_segment_type = ds.sk_segment_type
			left join dwh.dim_inventory_status dis on mi.fk_olp_car_inventory_status =dis.sk_inventory_status
			left join dwh.fact_booking fb on mi.fk_car_stock = fb.fk_stock_id
			left join dwh.dim_payment_type dpt on fk_payment_type =dpt.sk_payment_type 
			left join dwh.dim_date dd2 on fb.fk_sale_cancellation_date = dd2.sk_date
			left join dwh.dim_date dd3 on fb.fk_declared_sale_date = dd3.sk_date
	where mi.country_iso = 'MX'
	and dis.bk_inventory_status = 3
	and dd3."date" >= '2023-02-01') as x
where rn = 1
and x.amt_olp_real_published_price <> 0
and x.alg_amt_market_price <> 0
order by sale_date desc


y me da de resultado una tabla, encuentro una discrepancia en el resultado:

Mayo ventas brutas  752 ventas canceladas  95  ventas netas 657 
Editor is loading...