# All Time Appt And Leads

{% code title="all\_time\_appt\_date" overflow="wrap" lineNumbers="true" %}

```plsql
 SELECT to_char(date_trunc('day'::text, COALESCE(trial_date, tour_date)), 'YYYY-MM-DD'::text) AS appt_date,
    branch,
    visit_category,
    appt_category,
    contact_owner,
    assigned_pic,
    count(*) AS mql,
    count("mqlPlus_date") AS mqlplus,
    count("tourPlus_date") AS tourplus,
    count("trialPlus_date") AS trialplus
   FROM appt
  GROUP BY (to_char(date_trunc('day'::text, COALESCE(trial_date, tour_date)), 'YYYY-MM-DD'::text)), branch, visit_category, appt_category, contact_owner, assigned_pic
  ORDER BY (to_char(date_trunc('day'::text, COALESCE(trial_date, tour_date)), 'YYYY-MM-DD'::text));
```

{% endcode %}

{% code title="all\_time\_leads\_stages" overflow="wrap" lineNumbers="true" %}

```plsql
 SELECT to_char(date_trunc('month'::text, "timestamp")::date::timestamp with time zone, 'YYYY Mon'::text) AS month,
    (to_char(date_trunc('week'::text, "timestamp")::date::timestamp with time zone, 'DD Mon'::text) || ' - '::text) || to_char(date_trunc('week'::text, "timestamp")::date + '6 days'::interval, 'DD Mon'::text) AS week,
    date_trunc('day'::text, "timestamp")::date AS date,
    branch,
    contact_owner,
    assigned_pic,
    source,
    sremarks,
    medium,
    mremarks,
    count("timestamp") AS inbound,
    count(
        CASE
            WHEN leads_date::date = "timestamp"::date THEN leads_date
            ELSE NULL::timestamp without time zone
        END) AS leads,
    count(
        CASE
            WHEN mql_date::date = "timestamp"::date THEN mql_date
            ELSE NULL::timestamp without time zone
        END) AS mql,
    count(
        CASE
            WHEN "mqlPlus_date"::date = "timestamp"::date THEN "mqlPlus_date"
            ELSE NULL::timestamp without time zone
        END) AS mqlplus,
    count(
        CASE
            WHEN "tourPlus_date"::date = "timestamp"::date THEN "tourPlus_date"
            ELSE NULL::timestamp without time zone
        END) AS tourplus,
    count(
        CASE
            WHEN "trialPlus_date"::date = "timestamp"::date THEN "trialPlus_date"
            ELSE NULL::timestamp without time zone
        END) AS trialplus,
    count(
        CASE
            WHEN convert_date::date = "timestamp"::date THEN convert_date
            ELSE NULL::timestamp without time zone
        END) AS convert
   FROM leads
  GROUP BY (to_char(date_trunc('month'::text, "timestamp")::date::timestamp with time zone, 'YYYY Mon'::text)), ((to_char(date_trunc('week'::text, "timestamp")::date::timestamp with time zone, 'DD Mon'::text) || ' - '::text) || to_char(date_trunc('week'::text, "timestamp")::date + '6 days'::interval, 'DD Mon'::text)), (date_trunc('week'::text, "timestamp")::date), (date_trunc('day'::text, "timestamp")::date), branch, contact_owner, assigned_pic, source, sremarks, medium, mremarks
  ORDER BY (date_trunc('day'::text, "timestamp")::date);
```

{% 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/table-view/all-time-appt-and-leads.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.
