sql 马来西亚上市表现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 马来西亚上市表现相关的知识,希望对你有一定的参考价值。

--Listing Level

WITH
listing_table AS (
SELECT
id AS listing_id,
agent_id,
type_code
FROM
  `propertyguru-datalake-v0.propertydb_staging.my_v_listing_active_ds`
WHERE DATE(_PARTITIONTIME) >= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -1 MONTH)
  AND status_code = 'ACT'
GROUP BY 1,2,3
),

listing_view_lead AS (
SELECT
listing_id, 
total_view,
impression,
total_ViewPhone_lead,
(total_ViewPhone_lead + total_Email_lead + total_Call_lead + total_SMS_lead + total_Whatsapp_lead) AS total_lead
FROM
  `propertyguru-datalake-v0.datamart_v0.my_listing_performance`
GROUP BY 1,2,3,4,5
),

listing_msg_table AS (
SELECT
listing_id,
COUNT(listing_id) as total_msg
FROM
  `propertyguru-datalake-v0.propertydb_staging.my_contact_history`
GROUP BY 1
ORDER BY 2
)


SELECT 
CAST(lt.listing_id AS INT64) AS listing_id,
CAST(agent_id AS INT64) AS agent_id,
type_code,
CAST(total_view AS INT64) AS total_view,
CAST(impression AS INT64) AS impression,
CAST(total_ViewPhone_lead AS INT64) AS total_ViewPhone_lead,
IF(total_msg IS NULL, 0, total_msg) AS total_msg,
CAST((total_lead*15 + total_view) AS INT64) AS lead_view_score
FROM listing_table lt
LEFT JOIN listing_view_lead lv
ON CAST(lt.listing_id AS STRING) = lv.listing_id 
LEFT JOIN listing_msg_table lm
ON lt.listing_id = lm.listing_id



-- agent level

WITH
listing_table AS (
SELECT
id AS listing_id,
agent_id
FROM
  `propertyguru-datalake-v0.propertydb_staging.my_v_listing_active_ds`
WHERE 
  DATE(_PARTITIONTIME) >= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -1 MONTH)
  AND status_code = 'ACT'
GROUP BY 1,2
),

agent_table AS (
SELECT
agent_id,
firstname,
lastname,
mobile,
email,
agency_name,
status_code,
account_rule_code,
subscription_end
FROM `propertyguru-datalake-v0.propertydb_staging.my_v_agent_search`
WHERE status_code = 'ACT'
GROUP BY 1,2,3,4,5,6,7,8,9
),

purchase_quantity_table AS (
SELECT
pp.id AS utilized_id,
p.webuser_id AS agent_id,
pp.quantity AS purchased_quantity
FROM `propertydb_staging.my_purchases` p
LEFT JOIN `propertydb_staging.my_purchased_products` pp 
ON p.id = pp.purchase_id
LEFT JOIN `propertydb_staging.my_marketplace_product_prices` mp
ON product_price_id = mp.id
WHERE mp.product_code = "PG_1237" 
AND pp.status_code = "ACTIVE"),

agent_credit_usage_table AS (
SELECT
utilized_id,
agent_id,
purchased_quantity,
status_code AS utilized_status,
SUM(CASE WHEN ut.quantity IS NOT NULL THEN ut.quantity ELSE 0 END) AS utilized_quantity
FROM purchase_quantity_table pt
LEFT JOIN `propertydb_staging.my_utilized_products` ut
ON  ut.purchased_product_id = pt.utilized_id
GROUP BY 1,2,3,4),

agent_credit_remaining_table AS (
SELECT
agent_id,
(SUM(purchased_quantity) - SUM(utilized_quantity)) as ad_credits_remaining
FROM agent_credit_usage_table
GROUP BY 1),

listing_view_lead AS (
SELECT
listing_id, 
total_view,
impression,
total_ViewPhone_lead,
(total_ViewPhone_lead + total_Email_lead + total_Call_lead + total_SMS_lead + total_Whatsapp_lead) AS total_lead
FROM
  `propertyguru-datalake-v0.datamart_v0.my_listing_performance`
GROUP BY 1,2,3,4,5
),

listing_msg_table AS (
SELECT
listing_id,
COUNT(listing_id) as total_msg
FROM
  `propertyguru-datalake-v0.propertydb_staging.my_contact_history`
GROUP BY 1
),

listing_detail AS (
SELECT 
lt.listing_id,
agent_id,
IF(total_view IS NULL, 0, total_view) AS total_view,
IF(impression IS NULL, 0, impression) AS total_impression,
IF(total_ViewPhone_lead IS NULL, 0, total_ViewPhone_lead) AS total_ViewPhone,
IF(total_msg IS NULL, 0, total_msg) AS total_msg,
(total_lead*15 + total_view) AS lead_view_score
FROM listing_table lt
LEFT JOIN listing_view_lead lv
ON CAST(lt.listing_id AS STRING) = lv.listing_id 
LEFT JOIN listing_msg_table lm
ON lt.listing_id = lm.listing_id
),

agent_lead_view_table AS (
SELECT
1 AS join_index,
agent_id, 
SUM(IF(metric = 'VIEWS', value, 0)) AS total_view,
SUM(IF(metric = 'LEADS', value, 0)) AS total_lead
FROM
  `propertyguru-datalake-v0.datamart_v0.my_fact_leads_views`
WHERE
  DATE(_PARTITIONTIME) >= DATE_ADD(DATE(CURRENT_TIMESTAMP()), INTERVAL -1 MONTH)
GROUP BY 1,2
),
      
median_table AS (
SELECT 
1 AS join_index,
APPROX_QUANTILES(total_view, 100)[OFFSET(50)] AS median_view,
APPROX_QUANTILES(total_lead, 100)[OFFSET(50)] AS median_lead
FROM
agent_lead_view_table
),
      
agent_segment_table AS (
SELECT 
agent_id, 
total_view,
total_lead,
median_view,
median_lead,
CASE 
WHEN (total_view > median_view AND total_lead > median_lead) THEN 'high_view_high_lead' 
WHEN (total_view > median_view AND total_lead <= median_lead) THEN 'high_view_low_lead' 
WHEN (total_view <= median_view AND total_lead > median_lead) THEN 'low_view_high_lead' 
WHEN (total_view <= median_view AND total_lead <= median_lead) THEN 'low_view_low_lead' END AS segment
FROM agent_lead_view_table alt
LEFT JOIN median_table mt
ON alt.join_index = mt.join_index
),
      
final_agent_table AS (
SELECT 
agt.agent_id,
firstname,
lastname,
mobile,
email,
agency_name,
status_code,
account_rule_code,
subscription_end,
ad_credits_remaining,
IF(segment IS NULL, 'low_view_low_lead', segment) AS segment
FROM agent_table agt
LEFT JOIN agent_credit_remaining_table crt
ON agt.agent_id = crt.agent_id
LEFT JOIN agent_segment_table ast
ON agt.agent_id = ast.agent_id
),

final_table AS (
SELECT 
lt.listing_id,
total_view,
total_impression,
total_ViewPhone,
total_msg,
ft.agent_id,
firstname,
lastname,
mobile,
email,
agency_name,
status_code,
account_rule_code,
subscription_end,
ad_credits_remaining,
segment
FROM listing_detail lt
LEFT JOIN final_agent_table ft
ON lt.agent_id = ft.agent_id
)

SELECT
CAST(agent_id AS INT64) AS agent_id,
firstname,
lastname,
mobile,
email,
account_rule_code,
subscription_end,
CAST(ad_credits_remaining AS INT64) AS ad_credits_remaining,
segment,
COUNT(listing_id) AS total_active_listing,
SUM(total_view) AS total_view,
SUM(total_impression) AS total_impression,
SUM(total_ViewPhone) AS total_ViewPhone,
SUM(total_msg) AS total_msg
FROM final_table
GROUP BY 1,2,3,4,5,6,7,8,9

       

以上是关于sql 马来西亚上市表现的主要内容,如果未能解决你的问题,请参考以下文章

sql 新加坡上市表现

sql 马来西亚聚焦清单

sql 马来西亚信贷使用表

sql 马来西亚地理区域详情

sql 马来西亚地区专家

sql 马来西亚物业展