KYZN Docs
Home
  • Welcome
  • Intro
    • What is KYZN?
    • Age Group
  • KYZN Funnel
    • Glossary
      • General
      • Tracking Framework
    • Report
      • TAC Funnel
      • TCC Funnel
      • TAC Sleekflow
  • KYZN CRM
    • Glossary
      • My Leads
        • Add Leads
        • Add Family Leads
        • Add Appointment
        • Add Membership
        • Edit Branch
        • Edit PIC
        • Edit & Delete Leads
        • Search Leads
        • Reschedule Appointment
        • Clear Abnormal Stage
      • Approach
      • Pool
      • My Appt
        • Edit & Delete Appointment
        • Trial Form
      • My Convert
      • User Manager
      • UTM Generator
      • Assign PIC
      • Set Target
      • Schedule List
    • Event Bulk Data
  • Digital System
    • ETL Process
      • Query Checking
        • Cek Funnel Stage
        • Insert from temp_leads
        • Insert from sleekflow
        • Check Duplicate
        • Update Source Ads
        • Conversion Duration Analysist
        • Cek branch tidak match
        • Select Potential Contact
      • Query View
        • TAC Combined
        • TAC
        • TAC Appt
        • TAC Appt - Appt
        • TAC Appt - MQL
        • TAC Appt - MQLplus
        • TAC Appt - Tourplus
        • TAC Appt - Trialplus
        • TAC Convert
        • TAC Inbound
        • TAC Leads
        • TAC MQL
        • TAC MQLplus
        • TAC Tourplus
        • TAC Trialplus
        • Target View
        • Leads View
        • Appt View
        • Actual vs Target
        • All Time Leads Stages
        • All Time Appt Date
        • Budget Control
      • Database Management
        • pgAdmin
        • Looker Studio
  • Changelog
    • 2025-03
Powered by GitBook
On this page
  • Check Duplicate Multiple Table
  • Check Duplicate Single Table
  1. Digital System
  2. ETL Process
  3. Query Checking

Check Duplicate

Check Duplicate Multiple Table

temp_leads terhadap leads dan family
WITH RankedTempLeads AS (
    SELECT 
        t.*,
        ROW_NUMBER() OVER (PARTITION BY t.phone ORDER BY t.timestamp ASC) AS row_num
    FROM public.temp_leads t
    WHERE NOT EXISTS (
        SELECT 1
        FROM public.leads l
        WHERE l.phone = t.phone
    )
    AND NOT EXISTS (
        SELECT 1
        FROM public.family f
        WHERE f.family_phone = t.phone
    )
    AND t.phone IS NOT NULL
)
SELECT *
FROM RankedTempLeads
WHERE row_num = 1
ORDER BY phone ASC;
sleekflow terhadap combined
WITH RankedTempLeads AS (
    SELECT 
        t.*,
        ROW_NUMBER() OVER (PARTITION BY t.phone ORDER BY t.created_at ASC) AS row_num
    FROM public.sleekflow t
    WHERE NOT EXISTS (
        SELECT 1
        FROM public.combined l
        WHERE l.phone = t.phone
    )
    AND t.phone IS NOT NULL
)
SELECT *
FROM RankedTempLeads
WHERE row_num = 1
ORDER BY phone ASC;

1. Bagian "WITH RankedTempLeads AS (...)"

Bagian ini membuat temporary table bernama RankedTempLeads yang berisi data dari tabel temp_leads, tetapi hanya data tertentu yang memenuhi syarat. Berikut penjelasannya:

a. Kolom Tambahan

  • ROW_NUMBER() OVER (PARTITION BY t.phone ORDER BY t.timestamp ASC) AS row_num Menambahkan kolom bernama row_num, yang memberikan nomor urut untuk setiap group berdasarkan nilai t.phone.

    • Pengelompokan dilakukan menggunakan PARTITION BY t.phone (artinya data dikelompokkan berdasarkan nomor telepon).

    • Nomor urut ditentukan berdasarkan urutan waktu (t.timestamp ASC), sehingga data dengan waktu yang paling awal mendapat nomor 1.

b. Syarat "WHERE"

  • WHERE NOT EXISTS (...): Mengecek apakah data t.phone dari temp_leads tidak ada di dua tabel lain, yaitu:

    1. Tabel leads (kolom phone).

    2. Tabel family (kolom family_phone).

    Jika t.phone ditemukan di salah satu tabel tersebut, maka data tersebut akan diabaikan.

  • AND t.phone IS NOT NULL: Memastikan nomor telepon (t.phone) tidak kosong/null.

Hasilnya adalah temporary table RankedTempLeads yang hanya berisi data nomor telepon yang:

  • Tidak ada di tabel leads dan family.

  • Nomor teleponnya tidak kosong.


2. Bagian "SELECT * FROM RankedTempLeads WHERE row_num = 1"

Bagian ini memilih data dari temporary table RankedTempLeads, tetapi hanya baris dengan row_num = 1.

  • Artinya, hanya data dengan waktu terawal (timestamp paling kecil) untuk setiap nomor telepon yang akan diambil.


3. Bagian "ORDER BY phone ASC"

Hasil akhir diurutkan berdasarkan nomor telepon (phone) dalam urutan naik (ascending order).


Kesimpulan Fungsi Query

Query ini digunakan untuk:

  1. Mengambil data nomor telepon dari tabel temp_leads yang tidak ada di tabel leads dan family.

  2. Memastikan hanya nomor telepon yang tidak kosong yang diproses.

  3. Dari setiap nomor telepon, hanya memilih data dengan waktu terawal (timestamp paling kecil).

  4. Menampilkan hasil dengan urutan nomor telepon secara naik (ASC).

Hasil akhir adalah daftar unik nomor telepon dari temp_leads yang belum ada di tabel leads maupun family, dengan satu baris data per nomor telepon (berdasarkan waktu terawal).

Check Duplicate Single Table

Check duplicate leads
WITH DuplicatePhones AS (
    SELECT 
        phone
    FROM 
        public.leads
    GROUP BY 
        phone
    HAVING 
        COUNT(phone) > 1
)
SELECT 
    l.*
FROM 
    public.leads l
INNER JOIN 
    DuplicatePhones dp
ON 
    l.phone = dp.phone
	-- WHERE leads_qualification IN ('Inbound','Leads')
	ORDER BY phone, "timestamp" ASC;

1. Bagian WITH DuplicatePhones AS (...)

Bagian ini membuat temporary table bernama DuplicatePhones yang berisi daftar nomor telepon (phone) dari tabel public.leads yang terdapat lebih dari satu kali (duplikat). Berikut rincian logikanya:

a. SELECT phone

  • Hanya mengambil kolom phone dari tabel public.leads.

b. GROUP BY phone

  • Mengelompokkan data berdasarkan kolom phone, sehingga setiap nomor telepon dikelompokkan menjadi satu grup.

c. HAVING COUNT(phone) > 1

  • Memfilter grup yang memiliki jumlah (COUNT) lebih dari 1, yaitu nomor telepon yang muncul lebih dari satu kali (duplikat).

Hasil dari bagian ini adalah daftar nomor telepon yang memiliki duplikat di tabel public.leads.


2. Bagian SELECT l.*

Bagian ini digunakan untuk mengambil semua kolom dari tabel public.leads, tetapi hanya untuk baris yang nomornya termasuk dalam daftar DuplicatePhones. Berikut rincian logikanya:

a. INNER JOIN DuplicatePhones dp

  • Melakukan inner join antara tabel public.leads (alias l) dengan temporary table DuplicatePhones (alias dp).

  • Kondisi join: ON l.phone = dp.phone, artinya hanya data di tabel public.leads yang nomor teleponnya cocok dengan daftar nomor di DuplicatePhones akan diambil.

b. WHERE leads_qualification IN ('Inbound', 'Leads') (dalam komentar)

  • Baris ini saat ini dinonaktifkan (commented out), tetapi jika diaktifkan, akan memfilter data lebih lanjut untuk hanya menampilkan baris di mana kolom leads_qualification bernilai 'Inbound' atau 'Leads'.

c. ORDER BY phone, "timestamp" ASC

  • Mengurutkan hasil akhir berdasarkan:

    1. Nomor telepon (phone) dalam urutan naik (ascending).

    2. Waktu (timestamp) dalam urutan naik.


Kesimpulan Fungsi Query

Query ini digunakan untuk:

  1. Mendeteksi nomor telepon yang memiliki duplikat di tabel public.leads (nomor telepon yang muncul lebih dari sekali).

  2. Mengambil semua data dari tabel public.leads untuk nomor-nomor telepon tersebut.

  3. Mengurutkan hasil berdasarkan nomor telepon dan waktu (timestamp).

Hasil akhirnya adalah daftar lengkap baris dari tabel public.leads yang memiliki duplikat nomor telepon, diurutkan berdasarkan nomor telepon dan waktu secara naik.

Jika bagian WHERE leads_qualification IN ('Inbound', 'Leads') diaktifkan, maka hanya data dengan leads_qualification tertentu yang akan ditampilkan.

PreviousInsert from sleekflowNextUpdate Source Ads

Last updated 5 months ago