sql 新加坡AIME表

Posted

tags:

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

WITH
  listing_table AS (
  SELECT
    id,
    agent_id 
  FROM
     `propertyguru-datalake-v0.propertydb_staging.sg_v_listing_active`
  WHERE status_code = 'ACT'
    GROUP BY 1,2),
    
  active_listing AS (
  SELECT
    agent_id,
    owner_id AS listing_id,
    media.id AS image_id
  FROM
    `propertyguru-datalake-v0.propertydb_staging.sg_media` media
  INNER JOIN listing_table lt
  ON lt.id = media.owner_id
  WHERE status_code = 'ACT'
  ),
  
  image_table AS (
  SELECT
    media_id as image_id,
    status,
    media_url,
    updated_at
  FROM
    `propertyguru-datalake-v0.propertydb_staging.sg_media_moderator_status`
  ),
  
  image_table_complete AS (
  SELECT 
  agent_id,
  listing_id,
  al.image_id,
  status,
  media_url,
  updated_at
  FROM active_listing al
  LEFT JOIN image_table it
  ON al.image_id = it.image_id
  ORDER BY 1,2 ),
  
  agent_table AS (
  SELECT
  agent_id,
  mobile,
  email,
  firstname,
  lastname,
  agency_name,
  status_code,
  account_rule_code
  FROM
    `propertyguru-datalake-v0.propertydb_staging.sg_v_agent_search`
  WHERE status_code = 'ACT'
  ),
  
  final_table AS (
  SELECT
  listing_id,
  image_id,
  status,
  media_url,
  img.agent_id,
  mobile,
  email,
  firstname,
  lastname,
  agency_name,
  status_code,
  account_rule_code,
  updated_at
  FROM image_table_complete img
  LEFT JOIN agent_table agt
  ON img.agent_id = agt.agent_id
  ),
  
  agg_image_table AS (
  SELECT
  agent_id,
  listing_id,
  COUNT(IF(status = 'PASSED', 1, NULL)) AS passed_count,
  COUNT(IF(status = 'SUSPENDED', 1, NULL)) AS suspended_count,
  COUNT(IF(status = 'FLAGGED', 1, NULL)) AS flagged_count
  FROM image_table_complete
  GROUP BY 1,2
  ORDER BY 1,2,3),
  
  final_agg_table AS (
  SELECT
  listing_id,
  img.agent_id,
  passed_count,
  suspended_count,
  flagged_count,
  mobile,
  email,
  firstname,
  lastname,
  agency_name,
  status_code,
  account_rule_code
  FROM agg_image_table img
  LEFT JOIN agent_table agt
  ON img.agent_id = agt.agent_id
  )
  
  SELECT 
  listing_id,
  agent_id,
  passed_count AS passed_img_count,
  suspended_count AS suspended_img_count,
  flagged_count AS flagged_img_count,
  mobile,
  email,
  firstname,
  lastname,
  agency_name,
  status_code,
  account_rule_code,
  IF(passed_count >= 3, "AIME PASSED", "AIME SUSPENDED") AS listing_status,
  CONCAT("https://agentnet.propertyguru.com.sg/create-listing/location/",CAST(listing_id AS STRING)) AS listing_URL
  FROM final_agg_table

  
 
--   SELECT 
--   agent_id,
--   mobile,
--   email,
--   firstname,
--   lastname,
--   agency_name,
--   status_code,
--   account_rule_code,
--   COUNT(listing_id) AS total_active_listing,
--   COUNT(CASE WHEN suspended_count > 0 THEN 1
--              WHEN flagged_count > 0 THEN 1 
--              ELSE NULL END) AS total_listing_with_faulty_img,
--   COUNT(CASE WHEN passed_count < 3 THEN 1
--              ELSE NULL END) AS total_listing_less_than_3_good_img,
--   SUM(passed_count) AS total_passed_img,
--   SUM(suspended_count) AS total_suspended_img,
--   SUM(flagged_count) AS total_flagged_img
--   FROM final_agg_table
--   GROUP BY 1,2,3,4,5,6,7,8
--   ORDER BY 14 DESC

  
  
  


  -------------------- for new table ------
                                                                              WITH
  listing_table AS (
  SELECT
    id,
    agent_id 
  FROM
     `propertyguru-datalake-v0.production_pg_db05_propertydb.v_listing_active` 
  WHERE status_code = 'ACT'
  AND _fivetran_deleted = false 
    GROUP BY 1,2),
    
  active_listing AS (
  SELECT
    agent_id,
    owner_id AS listing_id,
    media.id AS image_id
  FROM
    `propertyguru-datalake-v0.production_pg_db05_propertydb.media` media
  INNER JOIN listing_table lt
  ON lt.id = media.owner_id
  WHERE status_code = 'ACT'
  AND _fivetran_deleted = false 
  ),
  
  image_table AS (
  SELECT
    media_id as image_id,
    status,
    media_url,
    updated_at
  FROM
    `propertyguru-datalake-v0.production_pg_db05_propertydb.media_moderator_status`
    WHERE _fivetran_deleted = false 
  ),
  
  image_table_complete AS (
  SELECT 
  agent_id,
  listing_id,
  al.image_id,
  status,
  media_url,
  updated_at
  FROM active_listing al
  LEFT JOIN image_table it
  ON al.image_id = it.image_id
  ORDER BY 1,2 ),
  
  agent_table AS (
  SELECT
  agent_id,
  mobile,
  email,
  firstname,
  lastname,
  agency_name,
  status_code,
  account_rule_code
  FROM
    `propertyguru-datalake-v0.propertydb_staging.sg_v_agent_search`
  WHERE status_code = 'ACT'
  ),
  
  final_table AS (
  SELECT
  listing_id,
  image_id,
  status,
  media_url,
  img.agent_id,
  mobile,
  email,
  firstname,
  lastname,
  agency_name,
  status_code,
  account_rule_code,
  updated_at
  FROM image_table_complete img
  LEFT JOIN agent_table agt
  ON img.agent_id = agt.agent_id
  ),
  
  agg_image_table AS (
  SELECT
  agent_id,
  listing_id,
  COUNT(IF(status = 'PASSED', 1, NULL)) AS passed_count,
  COUNT(IF(status = 'SUSPENDED', 1, NULL)) AS suspended_count,
  COUNT(IF(status = 'FLAGGED', 1, NULL)) AS flagged_count
  FROM image_table_complete
  GROUP BY 1,2
  ORDER BY 1,2,3),
  
  final_agg_table AS (
  SELECT
  listing_id,
  img.agent_id,
  passed_count,
  suspended_count,
  flagged_count,
  mobile,
  email,
  firstname,
  lastname,
  agency_name,
  status_code,
  account_rule_code
  FROM agg_image_table img
  LEFT JOIN agent_table agt
  ON img.agent_id = agt.agent_id
  )
  
--   SELECT 
--   listing_id,
--   agent_id,
--   passed_count AS passed_img_count,
--   suspended_count AS suspended_img_count,
--   flagged_count AS flagged_img_count,
--   mobile,
--   email,
--   firstname,
--   lastname,
--   agency_name,
--   status_code,
--   account_rule_code,
--   IF(passed_count >= 3, "AIME PASSED", "AIME SUSPENDED") AS listing_status,
--   CONCAT("https://agentnet.propertyguru.com.sg/create-listing/location/",CAST(listing_id AS STRING)) AS listing_URL
--   FROM final_agg_table

  
 
  SELECT 
  agent_id,
  mobile,
  email,
  firstname,
  lastname,
  agency_name,
  status_code,
  account_rule_code,
  COUNT(listing_id) AS total_active_listing,
  COUNT(CASE WHEN suspended_count > 0 THEN 1
             WHEN flagged_count > 0 THEN 1 
             ELSE NULL END) AS total_listing_with_faulty_img,
  COUNT(CASE WHEN passed_count < 3 THEN 1
             ELSE NULL END) AS total_listing_less_than_3_good_img,
  SUM(passed_count) AS total_passed_img,
  SUM(suspended_count) AS total_suspended_img,
  SUM(flagged_count) AS total_flagged_img
  FROM final_agg_table
  GROUP BY 1,2,3,4,5,6,7,8
  ORDER BY 14 DESC

  
  
  


  
  
  
  
  

以上是关于sql 新加坡AIME表的主要内容,如果未能解决你的问题,请参考以下文章

sql AIME软和硬悬浮图像

为二维表设置核心数据

sql 新加坡上市表现v2

sql 新加坡上市表现

sql 新加坡聚焦名单

sql 新加坡上市印象