# Update Source Ads

## Update Source Ads (meta\_ads terhadap existing data)

{% code title="" overflow="wrap" lineNumbers="true" %}

```plsql
WITH sleekflow_inbound AS (
    SELECT
        s.uid AS sleekflow_uid,
        DATE(s.created_at) AS sleekflow_date,
        s.source AS sleekflow_source,
        s.sremarks AS sleekflow_sremarks,
        s.medium AS sleekflow_medium,
        s.mremarks AS sleekflow_mremarks,
        l.uid AS leads_uid,
        DATE(l.timestamp) AS leads_date,
        DATE(l.convert_date) AS leads_convert_date,
        l.source AS leads_source
    FROM
        public.sleekflow s
    JOIN
        public.leads l
    ON
        s.uid = l.uid -- Menyesuaikan kolom untuk join berdasarkan UID atau identifier lainnya
),
updates AS (
    SELECT
        sleekflow_uid,
        sleekflow_date,
        sleekflow_source,
        sleekflow_sremarks,
        sleekflow_medium,
        sleekflow_mremarks
    FROM
        sleekflow_inbound
)
SELECT
    leads.uid AS leads_uid,
    DATE(leads."timestamp") AS current_leads_timestamp,
    u.sleekflow_date AS new_sleekflow_date,
    leads.source AS current_source,
    u.sleekflow_source AS new_source,
    leads.sremarks AS current_sremarks,
    u.sleekflow_sremarks AS new_sremarks,
    leads.medium AS current_medium,
    u.sleekflow_medium AS new_medium,
    leads.mremarks AS current_mremarks,
    u.sleekflow_mremarks AS new_mremarks
FROM
    updates u
JOIN
    public.leads leads
ON
    leads.uid = u.sleekflow_uid
WHERE
    leads."timestamp" IS NOT NULL 
    AND u.sleekflow_date::date <= leads."timestamp"::date AND leads.source = 'web';
```

{% endcode %}

{% code overflow="wrap" lineNumbers="true" %}

```plsql
WITH sleekflow_inbound AS (
    SELECT
        s.uid AS sleekflow_uid,
        DATE(s.created_at) AS sleekflow_date,
        s.source AS sleekflow_source,
        s.sremarks AS sleekflow_sremarks,
        s.medium AS sleekflow_medium,
        s.mremarks AS sleekflow_mremarks,
        l.uid AS leads_uid,
        DATE(l.timestamp) AS leads_date,
        DATE(l.convert_date) AS leads_convert_date,
        l.source AS leads_source
    FROM
        public.sleekflow s
    JOIN
        public.leads l
    ON
        s.uid = l.uid -- Menyesuaikan kolom untuk join berdasarkan UID atau identifier lainnya
),
updates AS (
    SELECT
        sleekflow_uid,
        sleekflow_date,
        sleekflow_source,
        sleekflow_sremarks,
        sleekflow_medium,
        sleekflow_mremarks
    FROM
        sleekflow_inbound
)
UPDATE public.leads
SET
    "timestamp" = u.sleekflow_date,
    source = u.sleekflow_source,
    sremarks = u.sleekflow_sremarks,
    medium = u.sleekflow_medium,
    mremarks = u.sleekflow_mremarks
FROM
    updates u
WHERE
    leads.uid = u.sleekflow_uid AND leads."timestamp" IS NOT NULL 
    AND u.sleekflow_date::date <= leads."timestamp"::date AND leads.source = 'social_media' AND u.sleekflow_source ='meta_ads';
```

{% endcode %}

## Fix Manual source = 'meta\_ads'

{% code title="" overflow="wrap" lineNumbers="true" %}

```plsql
update public.sleekflow
set
source = 'meta_ads',
sremarks = ad_source_id,
medium = 'sleekflow',
mremarks = 'wa'
where ad_source_id is not null AND source = 'social_media'
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://work.bahrul.me/work/kyzn/database-management/query-checking/update-source-ads.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
