> ## Documentation Index
> Fetch the complete documentation index at: https://docs.linkiasoft.com/llms.txt
> Use this file to discover all available pages before exploring further.

# مرجع LQL: كل حقل ومقياس ودالة

> المرجع الكامل للغة التقارير خلف تقاريرك الخاصة: الصياغة والتواريخ والتجميع وCOMBINE وDRILL، وكل حقل ومقياس يمكنك استخدامه.

LQL هي اللغة التي تعمل بها **التحليلات المتعمقة ← تقاريرك الخاصة**. يكتبها لك منشئ التقارير و"اسأل الذكاء الاصطناعي"؛ وهذه الصفحة لمن يريد كتابة استعلام أو تعديله بنفسه. والمرجع نفسه موجود داخل التطبيق — زر **الدليل** بجانب **تشغيل**، ولكل مثال فيه زر **جرّب**.

جديد على التقارير المخصصة؟ ابدأ بـ [تقاريرك الخاصة ولوحات المعلومات](https://docs.linkiasoft.com/ar/help/analytics/custom-reports.md).

## المحتويات

* [البداية](#start)
* [الشروط (WHERE)](#where)
* [القيم والأشخاص والفلاتر المحفوظة](#values)
* [التواريخ والفترات](#dates)
* [السجل (WAS / CHANGED)](#history)
* [التجميع (GROUP BY)](#group)
* [الأعمدة والمقاييس (SHOW)](#show)
* [الترتيب والحد (ORDER BY وLIMIT)](#order)
* [دمج الاستعلامات (COMBINE وCALC)](#combine)
* [أرقام قابلة للنقر (DRILL)](#drill)
* [أداء الفريق (FROM team)](#team)
* [الاستعانة بالذكاء الاصطناعي](#ai)
* [الحدود والأمان](#limits)
* [مرجع البيانات](#data-reference)
* [الدوال والعوامل](#functions)
* [مزيد من الأمثلة](#more-examples)

## البداية

كل تقرير استعلام واحد. يبدأ بـ FROM واسم ما تريد عدّه، تليه أجزاء اختيارية بأي ترتيب. الكلمات المفتاحية لا تتأثر بحالة الأحرف. ويعيد الاستعلام أحد ثلاثة أشكال: قائمة سجلات، أو صف ملخص واحد، أو صفًا لكل مجموعة.

* `FROM <entity> WHERE … GROUP BY … SHOW … ORDER BY … LIMIT n` — الشكل الكامل. وحده FROM إلزامي.

```sql theme={null}
FROM orders WHERE created >= -7d SHOW number, customer, total
```

قائمة: تذكر SHOW حقولًا، فيظهر صف لكل طلب.

```sql theme={null}
FROM orders WHERE created >= -30d SHOW count(), revenue()
```

ملخص: تذكر SHOW مقاييس فقط، فيظهر صف واحد.

```sql theme={null}
FROM orders WHERE created >= -30d GROUP BY governorate SHOW count(), revenue()
```

مجمّع: صف لكل محافظة ومعه مقاييسها.

## الشروط (WHERE)

تُبقي WHERE السجلات المطابقة فقط. اربط الشروط بـ AND وOR وNOT، واستخدم الأقواس لتجميعها. التصفية على حقل مفهرس (موضّح في مرجع البيانات) هي الأسرع.

* `source = facebook` — يساوي. وكذلك != و> و>= و\< و\<=.
* `governorate IN (Cairo, Giza)  ·  stage.name NOT IN ("Lost")` — إحدى قيم قائمة، أو ليست أيًّا منها.
* `customer ~ "ahmed"  ·  name ~ "test*"  ·  name !~ "demo"` — يحتوي على (دون تمييز حالة الأحرف). الرمزان \* و? رموز بدل، و!\~ تعني لا يحتوي.
* `owner IS EMPTY  ·  ad IS NOT EMPTY` — بلا قيمة / له قيمة.
* `tags ANY ("vip", "wholesale")  ·  tags ALL ("vip", "repeat")` — حقول القوائم: تحتوي واحدة على الأقل / تحتويها جميعًا.
* `text ~ "acme"` — يبحث في الحقول النصية الرئيسية دفعة واحدة (الأسماء، الهاتف، الشركة …).
* `(source = facebook OR source = instagram) AND NOT cod = true` — الأقواس وNOT. ملاحظة: يتجاهل != وNOT IN القيم الفارغة — أضف "OR field IS EMPTY" لإبقائها.

## القيم والأشخاص والفلاتر المحفوظة

اكتب النص بين "علامتَي تنصيص" إذا احتوى مسافات؛ ويمكن كتابة الكلمات المفردة وقيم القوائم (won وpending وwhatsapp) دونها. وتُكتب الأرقام وtrue / false كما هي.

* `stage.name = "Follow up"  ·  value > 5000  ·  cod = true` — قيمة نصية ورقمية ونعم/لا.
* `owner = currentUser()` — أنت — أيًّا كان من يشغّل التقرير.
* `owner = "sara@company.com"  ·  owner IN membersOf("Sales")` — زميل ببريده الإلكتروني، أو جميع أعضاء فريق.
* `filter = "VIP leads"` — يعيد استخدام شروط تقرير محفوظ على النوع نفسه (حتى 3 مستويات).

## التواريخ والفترات

تُحسب التواريخ بالمنطقة الزمنية لشركتك. استخدم مدة نسبية أو تاريخًا أو دالة. وتنظر الجداول الكبيرة (الطلبات والرسائل والمحادثات …) إلى فترة حديثة إذا خلا الاستعلام من شرط تاريخ — وتذكر النتيجة الفترة المستخدمة.

* `created >= -30d  ·  -12h  ·  -2w  ·  -3M  ·  -1y  ·  "-4w 2d"` — نسبي: m دقائق، وh ساعات، وd أيام، وw أسابيع، وM أشهر، وy سنوات.
* `created >= "2026-03-01" AND created < "2026-04-01 00:00"` — تواريخ: yyyy-MM-dd أو yyyy/MM/dd، ويمكن إضافة HH:mm.
* `created >= today()  ·  created = yesterday()` — اليوم وأمس؛ وnow() هي هذه اللحظة.
* `created >= startOfMonth()` — هذا الشهر. وكذلك startOfDay وstartOfWeek وstartOfQuarter وstartOfYear ونظائرها endOf….
* `created >= startOfMonth(-1) AND created < startOfMonth()` — الشهر الماضي: يتحرك الرقم بفترات كاملة (-1 = السابقة).

```sql theme={null}
FROM leads WHERE created >= startOfWeek(-1) AND created < startOfWeek() SHOW count()
```

الأسبوع الماضي، من الاثنين إلى الأحد.

## السجل (WAS / CHANGED)

في الصفقات يمكنك السؤال عن الماضي لا القيمة الحالية فقط: أيّ الصفقات مرّت بمرحلة معيّنة، أو من نقلها. والحقول التي لها سجل مذكورة في مرجع البيانات.

```sql theme={null}
FROM leads WHERE stage WAS won AND stage = lost SHOW name, owner.name, value
```

كانت رابحة في وقت ما، وهي خاسرة الآن.

```sql theme={null}
FROM leads WHERE stage CHANGED TO won DURING (startOfMonth(), now()) SHOW name, value
```

انتقلت إلى رابحة هذا الشهر. وكذلك FROM x وBY user وAFTER / BEFORE / ON تاريخ.

```sql theme={null}
FROM leads WHERE owner CHANGED AFTER -7d SHOW name, owner.name
```

أعيد إسنادها خلال آخر 7 أيام.

## التجميع (GROUP BY)

تُنشئ GROUP BY صفًا لكل قيمة، حتى 3 مفاتيح. جمّع التاريخ حسب فترة للحصول على خط زمني، أو حسب ساعة اليوم / يوم الأسبوع لاكتشاف الأنماط. وتظهر السجلات التي بلا قيمة في صف "(none)" خاص بها.

* `GROUP BY owner.name  ·  GROUP BY channel.type, source` — حسب حقل، أو حقلين.

```sql theme={null}
FROM orders WHERE created >= -90d GROUP BY week(created) SHOW count(), revenue()
```

خط زمني: hour وday وweek وmonth وquarter وyear.

```sql theme={null}
FROM orders WHERE created >= -90d GROUP BY hourOfDay(created) SHOW count()
```

نمط: hourOfDay (من 00 إلى 23) أو weekday (1 الاثنين … 7 الأحد).

* `GROUP BY channel.type AS channel` — إعادة تسمية عمود المجموعة — لازمة لدمج استعلامين بـ COMBINE.

## الأعمدة والمقاييس (SHOW)

تحدد SHOW ما يظهر: حقولًا للقائمة، أو مقاييس (حتى 10) للملخص أو المجموعات. لكل نوع مقاييس جاهزة (revenue() وwinRate() …) مذكورة في مرجع البيانات؛ أما المقاييس العامة أدناه فتعمل على أي حقل رقمي.

* `count()  ·  count(phone)  ·  countDistinct(customer)` — عدد الصفوف، والصفوف التي لها قيمة، والقيم المختلفة.
* `sum(total)  ·  avg(total)  ·  min(total)  ·  max(total)` — مجموع حقل رقمي ومتوسطه وأدناه وأقصاه.
* `median(total)  ·  p90(daysToWin)` — القيمة النموذجية، والقيمة التي يقع 90% تحتها.
* `rate(cod = true)` — نسبة الصفوف المطابقة لشرط، كنسبة مئوية.

```sql theme={null}
FROM orders WHERE created >= -30d GROUP BY governorate SHOW count() AS orders, revenue() AS revenue
```

تسمّي AS العمود؛ واستخدم الاسم في ORDER BY وCALC.

## الترتيب والحد (ORDER BY وLIMIT)

ترتّب ORDER BY حسب اسم عمود أو مقياس أو الاسم المعطى بـ AS. تعرض القائمة 100 صف ما لم تحدد غير ذلك، حتى 1000؛ ويصل التصدير إلى 5000.

```sql theme={null}
FROM products WHERE created >= -30d GROUP BY name SHOW units(), revenue() ORDER BY revenue() DESC LIMIT 10
```

أعلى 10. DESC = الأكبر أولًا، وASC = الأصغر أولًا.

```sql theme={null}
FROM leads WHERE owner = currentUser() SHOW name, value ORDER BY value DESC LIMIT 50
```

ترتيب قائمة حسب حقل.

## دمج الاستعلامات (COMBINE وCALC)

تضع COMBINE أرقامًا من أنواع مختلفة جنبًا إلى جنب — الطلبات بجانب المحادثات، والإنفاق بجانب الإيراد. اكتب حتى 4 استعلامات مجمّعة، وامنح المفتاح المشترك اسم AS نفسه في كل منها، ثم اربطها بـ COMBINE BY ذلك الاسم. لكل جزء شروط WHERE خاصة به، فكرّر الفترة الزمنية. ويحتاج كل مقياس إلى اسم AS خاص به. وتضيف CALC حتى 10 أعمدة محسوبة (+ − × ÷ والأقواس وROUND)؛ والقسمة على صفر تترك الخانة فارغة. ويأتي ORDER BY وLIMIT في النهاية.

```sql theme={null}
FROM orders WHERE created >= -30d GROUP BY channel.type AS channel SHOW count() AS orders COMBINE BY channel FROM conversations WHERE created >= -30d GROUP BY channel.type AS channel SHOW count() AS chats CALC orders_per_chat = ROUND(orders / chats, 2) ORDER BY orders DESC
```

الطلبات والمحادثات لكل قناة، وعدد الطلبات لكل محادثة.

```sql theme={null}
FROM leads WHERE created >= startOfMonth() SHOW count() AS leads COMBINE FROM orders WHERE created >= startOfMonth() SHOW count() AS orders
```

دون GROUP BY: تربط COMBINE وحدها صفَّي ملخص.

```sql theme={null}
FROM orders WHERE created >= startOfMonth() GROUP BY channel.type AS channel SHOW count() AS this_month COMBINE BY channel FROM orders WHERE created >= startOfMonth(-1) AND created < startOfMonth() GROUP BY channel.type AS channel SHOW count() AS last_month CALC change_pct = ROUND((this_month - last_month) * 100 / last_month, 1)
```

هذا الشهر مقابل الشهر الماضي: الاستعلام نفسه على فترتين، ونسبة التغيّر.

```sql theme={null}
FROM ads WHERE date >= -30d GROUP BY ad SHOW spend() AS spend COMBINE BY ad FROM orders WHERE created >= -30d AND ad IS NOT EMPTY GROUP BY ad SHOW revenue() AS revenue CALC roas = ROUND(revenue / spend, 2)
```

العائد على الإنفاق الإعلاني لكل إعلان.

## أرقام قابلة للنقر (DRILL)

اختم الاستعلام بـ DRILL ليفتح كل رقم في النتيجة السجلات التي خلفه، 50 في كل صفحة، الأحدث أولًا، مع رابطَي فتح المحادثة وفتح الصفقة. وتختار DRILL SHOW أعمدة تلك القائمة. وفي COMBINE تكفي DRILL واحدة في النهاية لكل الأجزاء. احذفها إذا أردت أن تبقى الأرقام عادية.

```sql theme={null}
FROM conversations WHERE created >= -30d GROUP BY channel.name SHOW count() DRILL
```

انقر العدد لعرض تلك المحادثات.

```sql theme={null}
FROM orders WHERE created >= -30d GROUP BY governorate SHOW count(), revenue() DRILL SHOW number, customer, total, deliveryStatus
```

اختر أعمدة قائمة السجلات.

## أداء الفريق (FROM team)

تعطي FROM team صفًا لكل عضو في الفريق بأرقام مجمّعة من عدة جداول — الرسائل والمحادثات والصفقات والطلبات والمكالمات. حدد الفترة بـ WHERE period؛ ولا حاجة إلى GROUP BY (وGROUP BY team.name تجمع الأعضاء في فرقهم).

```sql theme={null}
FROM team WHERE period >= startOfMonth() SHOW name, messagesSent(), chats(), wonDeals(), orders(), revenue()
```

هذا الشهر، لكل عضو.

```sql theme={null}
FROM team WHERE period >= -90d GROUP BY team.name SHOW messagesSent(), wonDeals()
```

لكل فريق بدلًا من كل شخص.

## الاستعانة بالذكاء الاصطناعي

في "اسأل الذكاء الاصطناعي" صِف التقرير بكلماتك (بالعربية أو الإنجليزية) فيكتب الذكاء الاصطناعي الاستعلام ويتحقق من أنه يعمل ويصلحه إن لزم. وحين يكون على الشاشة استعلام، اكتب التعديل فقط — "أضف الإيراد"، "الدفع عند الاستلام فقط"، "قسّمه حسب الشهر"، "قارن بالشهر الماضي" — فيتغيّر ذلك الجزء وحده. ويحتفظ التراجع بآخر النسخ. ويؤدي زر ✨ بجانب المحرر الأمر نفسه لاستعلام كتبته أنت.

## الحدود والأمان

لا يمكن للتقارير أن تبطئ النظام: يُفحص كل استعلام قبل تشغيله ويُرفض إذا كان سيقرأ بيانات كثيرة — ضيّق التواريخ أو صفِّ على حقل مفهرس. ويتوقف التشغيل بعد 15 ثانية. ويمكن لكل شخص تشغيل 30 تقريرًا في الدقيقة. وتُحفظ النتائج نحو دقيقة. ولا ترى إلا السجلات التي يسمح بها دورك، وتبقى أرقام الهواتف مخفية للأدوار التي لا يحق لها رؤيتها.

* `GROUP BY ≤ 3 keys · ≤ 10 metrics · ≤ 30 conditions · LIMIT ≤ 1000 · ≤ 1000 groups · 4000 characters` — حدود الحجم لكل استعلام.

## مرجع البيانات

ما يعدّه كل نوع، وحقوله (لـ WHERE وGROUP BY وSHOW)، ومقاييسه الجاهزة. أسماء الحقول والمقاييس بالإنجليزية لأنها ما تكتبه في الاستعلام. **تجميع** = يمكن استخدامه في GROUP BY. **سريع** = مفهرس، وهو الأسرع في التصفية. وحقول الهاتف والبريد مخفية للأدوار التي لا ترى أرقام الهواتف، ولا يمكن التصفية عليها.

### Leads / deals — `FROM leads`

* حقل التاريخ: `created`.
* السجل (WAS / CHANGED) على: `stage`, `owner`, `value`, `priority`, `source`, `probability`, `currency`, `customerType`.
* يبحث `text ~ "…"` في: `name`, `email`, `company`, `phone`.

| الحقل | النوع | تجميع | سريع | القيم | الوصف |
| - | - | - | - | - | - |
| `id` | نص | | | | Lead id |
| `chat` | نص | | | | The lead's most recent chat (id) |
| `name` | نص | | | | Name |
| `email` | نص (مخفي) | | | | Email |
| `phone` | نص (مخفي) | | | | Phone |
| `company` | نص | ✓ | ✓ | | Company |
| `stage` | قيمة من قائمة | ✓ | ✓ | | The stage key, e.g. new, won, lost |
| `stage.name` | نص | ✓ | | | Stage |
| `stage.won` | نعم / لا | ✓ | | | Stage is won |
| `stage.lost` | نعم / لا | ✓ | | | Stage is lost |
| `source` | قيمة من قائمة | ✓ | | `facebook` `instagram` `whatsapp` `website` `manual` `import` | Source |
| `priority` | قيمة من قائمة | ✓ | ✓ | `urgent` `high` `medium` `low` | Priority |
| `probability` | رقم | ✓ | ✓ | | Probability % |
| `value` | مبلغ | ✓ | ✓ | | Deal value |
| `weightedValue` | مبلغ | | | | Deal value × probability % (forecast: sum(weightedValue) by month(expectedClose)) |
| `currency` | قيمة من قائمة | ✓ | | | Currency |
| `customerType` | قيمة من قائمة | ✓ | | `individual` `business` | Customer type |
| `industry` | نص | ✓ | | | Industry |
| `owner` | عضو فريق | ✓ | ✓ | | Owner |
| `owner.name` | نص | ✓ | | | Owner name |
| `owner.email` | نص | ✓ | | | Owner email |
| `channel.name` | نص | ✓ | | | Channel |
| `channel.type` | قيمة من قائمة | ✓ | | `whatsapp` `messenger` `instagram` `telegram` `email` `sms` | Channel type |
| `tags` | وسوم | | ✓ | | Tags |
| `created` | تاريخ | | ✓ | | Created |
| `updated` | تاريخ | | | | Updated |
| `wonAt` | تاريخ | | | | When the deal last entered a won stage |
| `daysToWin` | رقم | | | | Created → won, days; empty unless the deal is won (sales cycle: median(daysToWin)) |
| `ageDays` | رقم | | | | Days since the deal was created |
| `idleDays` | رقم | | | | Days since the deal last changed (stale pipeline: idleDays > 14) |
| `expectedClose` | تاريخ | | | | Expected close |
| `current` | نعم / لا | ✓ | | | false for an older deal cycle replaced by a newer one for the same person |
| `ad` | نص | ✓ | | | The ad the lead came from: its first chat’s click-to-message ad, or its Meta lead form’s ad |
| `ad.name` | نص | ✓ | | | Ad name from the Meta ad sync, else the ad headline (lead form name for form leads) |
| `campaign.name` | نص | ✓ | | | Campaign of that ad (Meta ad sync; empty until an ad account is connected) |
| `hasOrder` | نعم / لا | ✓ | | | The lead’s customer (or one of its chats) has a store order that is not cancelled. Per-row lookup: combine with a date filter on large workspaces |
| `conversation.lastInbound` | تاريخ | | | | Per-row lookup: combine with an indexed filter on large workspaces |

| المقياس | النوع | الأفضل | الوصف |
| - | - | - | - |
| `wonCount()` | رقم | ▲ الأعلى | Deals in a won stage |
| `wonValue()` | مبلغ | ▲ الأعلى | Sum of deal value in won stages |
| `pipelineValue()` | مبلغ | ▲ الأعلى | Sum of deal value in stages that are neither won nor lost |
| `winRate()` | نسبة | ▲ الأعلى | Won / (won + lost) — open deals are not counted |
| `orderCount()` | رقم | ▲ الأعلى | Store orders (not cancelled) the leads’ customers placed — see hasOrder. A person with several deal cycles counts in each; add current = true to count people once |
| `orderRevenue()` | مبلغ | ▲ الأعلى | Revenue of those orders, net of refunds (orders revenue()) — revenue by lead source: GROUP BY source SHOW orderRevenue() |
| `orderConversion()` | نسبة | ▲ الأعلى | Share of leads whose customer placed a store order (hasOrder) |

### Store orders — `FROM orders`

* حقل التاريخ: `created`.
* دون شرط تاريخ يعرض آخر 90 يومًا.
* يبحث `text ~ "…"` في: `number`, `customer`, `email`, `phone`.

| الحقل | النوع | تجميع | سريع | القيم | الوصف |
| - | - | - | - | - | - |
| `id` | نص | | | | Order id |
| `number` | نص | | | | Order |
| `conversation` | نص | | | | The chat the order is linked to; empty for a store order nobody chatted about |
| `customer` | نص | | | | Customer |
| `phone` | نص (مخفي) | | | | Phone |
| `email` | نص (مخفي) | | | | Email |
| `created` | تاريخ | | ✓ | | Placed at |
| `total` | مبلغ | ✓ | | | Total |
| `subtotal` | مبلغ | ✓ | | | Subtotal |
| `shipping` | مبلغ | ✓ | | | Shipping |
| `discount` | مبلغ | ✓ | | | Discount |
| `refunded` | مبلغ | ✓ | | | Refunded |
| `net` | مبلغ | ✓ | | | Net (total − refunds) |
| `currency` | قيمة من قائمة | ✓ | | | Currency |
| `cancelled` | نعم / لا | ✓ | | | Cancelled |
| `cancelReason` | قيمة من قائمة | ✓ | | `customer` `fraud` `inventory` `declined` `other` | Cancel reason |
| `financialStatus` | قيمة من قائمة | ✓ | | `pending` `paid` `partially_paid` `refunded` `partially_refunded` `voided` `authorized` | Payment status |
| `fulfillmentStatus` | قيمة من قائمة | ✓ | | `unfulfilled` `partial` `fulfilled` `restocked` | Fulfillment status |
| `cod` | نعم / لا | ✓ | | | Cash on delivery |
| `confirmation` | قيمة من قائمة | ✓ | | `pending` `sent` `read` `confirmed` `cancelled` `no_answer` `needs_call` | COD confirmation |
| `confirmationChannel` | قيمة من قائمة | ✓ | | `whatsapp` `sms` `voice` `agent` `thank_you_page` | Confirmed via |
| `governorate` | نص | ✓ | | | Governorate |
| `source` | نص | ✓ | | | Sales channel |
| `fromChat` | نعم / لا | ✓ | | | Created from chat |
| `tags` | قائمة | | | | Order tags |
| `discountCodes` | قائمة | | | | Discount codes |
| `owner` | عضو فريق | ✓ | | | Chat owner |
| `owner.name` | نص | ✓ | | | Chat owner name |
| `channel.name` | نص | ✓ | | | Channel |
| `channel.type` | قيمة من قائمة | ✓ | | | Channel type |
| `ad` | نص | ✓ | | | The click-to-message ad the order’s chat started from; empty when none |
| `ad.name` | نص | ✓ | | | Ad name from the Meta ad sync, else the ad headline |
| `campaign.name` | نص | ✓ | | | Campaign of that ad (Meta ad sync) |
| `deliveryStatus` | قيمة من قائمة | ✓ | | `not_shipped` `shipped` `in_transit` `delivered` `returned` | Across all its shipments: returned (sent back to us, RTO) beats delivered beats in\_transit (incl. out for delivery and a failed attempt); shipped = fulfilled with no courier status yet |
| `delivered` | نعم / لا | ✓ | | | A shipment reached the customer and none came back |
| `returned` | نعم / لا | ✓ | | | A shipment was returned to origin |
| `courier` | نص | ✓ | | | Shipping company of the latest shipment |
| `repeatCustomer` | نعم / لا | ✓ | | | The customer had an earlier (not cancelled) order; false for a first order |

| المقياس | النوع | الأفضل | الوصف |
| - | - | - | - |
| `customers()` | رقم | ▲ الأعلى | Distinct customers who ordered (orders per customer = count() / customers()) |
| `repeatRate()` | نسبة | ▲ الأعلى | Share of orders placed by a customer who had ordered before |
| `revenue()` | مبلغ | ▲ الأعلى | Net of refunds, cancelled orders excluded |
| `aov()` | مبلغ | ▲ الأعلى | Revenue / live orders |
| `cancelRate()` | نسبة | ▼ الأقل | Share of orders cancelled |
| `confirmRate()` | نسبة | ▲ الأعلى | Confirmed / all cash-on-delivery orders |
| `deliveryRate()` | نسبة | ▲ الأعلى | Delivered / all orders (Delivery & returns report definition) |
| `returnRate()` | نسبة | ▼ الأقل | Returned / (delivered + returned): the share of settled shipments that came back |

### Products sold (order lines) — `FROM products`

* حقل التاريخ: `created`.
* دون شرط تاريخ يعرض آخر 90 يومًا.
* يبحث `text ~ "…"` في: `name`, `sku`.

| الحقل | النوع | تجميع | سريع | القيم | الوصف |
| - | - | - | - | - | - |
| `id` | نص | | | | Line id |
| `conversation` | نص | | | | The chat the order came from (id); empty for a store order nobody chatted about |
| `name` | نص | ✓ | | | Product |
| `productId` | نص | ✓ | | | Product id |
| `variant` | نص | ✓ | | | Variant |
| `sku` | نص | ✓ | | | SKU |
| `quantity` | رقم | ✓ | | | Quantity |
| `price` | مبلغ | ✓ | | | Unit price |
| `lineTotal` | مبلغ | | | | Line total |
| `orderNumber` | نص | | | | Order |
| `created` | تاريخ | | ✓ | | Order placed at |
| `cancelled` | نعم / لا | ✓ | | | Order cancelled |
| `cod` | نعم / لا | ✓ | | | Cash on delivery |
| `confirmation` | قيمة من قائمة | ✓ | | `pending` `sent` `read` `confirmed` `cancelled` `no_answer` `needs_call` | COD confirmation |
| `governorate` | نص | ✓ | | | Governorate |
| `source` | نص | ✓ | | | Sales channel |
| `fromChat` | نعم / لا | ✓ | | | Order created from chat |
| `currency` | قيمة من قائمة | ✓ | | | Currency |
| `owner` | عضو فريق | ✓ | | | Chat owner |
| `owner.name` | نص | ✓ | | | Chat owner name |
| `channel.name` | نص | ✓ | | | Channel |
| `channel.type` | قيمة من قائمة | ✓ | | | Channel type |
| `ad` | نص | ✓ | | | The click-to-message ad the order’s chat started from |
| `ad.name` | نص | ✓ | | | Ad name from the Meta ad sync, else the ad headline |
| `campaign.name` | نص | ✓ | | | Campaign of that ad (Meta ad sync) |

| المقياس | النوع | الأفضل | الوصف |
| - | - | - | - |
| `units()` | رقم | ▲ الأعلى | Quantity on orders not cancelled |
| `revenue()` | مبلغ | ▲ الأعلى | Line value (price × quantity), cancelled orders excluded (the Products report definition) |
| `orders()` | رقم | ▲ الأعلى | Orders containing it |
| `cancelRate()` | نسبة | ▼ الأقل | Share of the orders containing it that were cancelled |
| `confirmRate()` | نسبة | ▲ الأعلى | Confirmed / cash-on-delivery orders containing it |

### Conversations (chats) — `FROM conversations`

* حقل التاريخ: `created`.
* دون شرط تاريخ يعرض آخر 90 يومًا.
* يبحث `text ~ "…"` في: `name`, `phone`.

| الحقل | النوع | تجميع | سريع | القيم | الوصف |
| - | - | - | - | - | - |
| `id` | نص | | | | Conversation id |
| `customer` | نص | | | | Customer id |
| `name` | نص | | | | Customer name |
| `phone` | نص (مخفي) | | | | Phone |
| `status` | قيمة من قائمة | ✓ | ✓ | `open` `pending` `snoozed` `closed` | Status |
| `open` | نعم / لا | ✓ | | | Not closed |
| `owner` | عضو فريق | ✓ | ✓ | | Assigned to |
| `owner.name` | نص | ✓ | | | Assigned to (name) |
| `owner.email` | نص | ✓ | | | Assigned to (email) |
| `team.name` | نص | ✓ | | | Team |
| `channel.name` | نص | ✓ | | | Channel |
| `channel.type` | قيمة من قائمة | ✓ | | `whatsapp` `facebook` `instagram` `tiktok` `telegram` `webchat` `sip` | Channel type |
| `awaitingReply` | نعم / لا | ✓ | | | The last message is from the customer |
| `lastDirection` | قيمة من قائمة | ✓ | | `inbound` `outbound` | Last message from |
| `hasInbound` | نعم / لا | ✓ | | | Customer wrote |
| `hasOutbound` | نعم / لا | ✓ | | | Any outbound message (agent, AI or automation) |
| `hasLead` | نعم / لا | ✓ | | | Linked to a lead |
| `lead` | نص | | | | The lead this chat belongs to (id) |
| `fromAd` | نعم / لا | ✓ | | | Started from a click-to-message ad |
| `ad` | نص | ✓ | | | The click-to-message ad the chat started from; empty when none |
| `ad.name` | نص | ✓ | | | Ad name from the Meta ad sync, else the ad headline |
| `campaign.name` | نص | ✓ | | | Campaign of that ad (Meta ad sync) |
| `answered` | نعم / لا | ✓ | | | A teammate replied from the CRM after the customer wrote (AI / automations / phone app do not count) |
| `firstInbound` | تاريخ | | | | The customer message the first response answers; set on answered chats |
| `firstResponseAt` | تاريخ | | ✓ | | First response at |
| `firstResponseMins` | رقم | | | | First customer message → first teammate reply, minutes; empty when unanswered |
| `firstResponder` | عضو فريق | ✓ | | | The teammate who sent the first reply |
| `firstResponder.name` | نص | ✓ | | | First responder (name) |
| `aiHandled` | نعم / لا | ✓ | | | An AI agent sent at least one message in it |
| `waitingHours` | رقم | | | | Open chats awaiting our reply: hours since the customer’s last message; empty otherwise |
| `hoursToAssign` | رقم | | | | Chat started → assigned to a teammate, hours |
| `optedOut` | نعم / لا | ✓ | | | Opted out |
| `reachability` | قيمة من قائمة | ✓ | | `blocked` `unreachable` | Empty when reachable |
| `created` | تاريخ | | ✓ | | Started |
| `lastMessage` | تاريخ | | ✓ | | Last message |
| `lastInbound` | تاريخ | | | | Last customer message |
| `assignedAt` | تاريخ | | | | Assigned at |
| `updated` | تاريخ | | | | Updated |

| المقياس | النوع | الأفضل | الوصف |
| - | - | - | - |
| `openCount()` | رقم | | Chats not closed |
| `waitingCount()` | رقم | ▼ الأقل | Open chats whose last message is from the customer |
| `unassignedCount()` | رقم | ▼ الأقل | Open chats with nobody assigned |
| `customers()` | رقم | ▲ الأعلى | Distinct customers who chatted (e.g. customers per channel) |
| `replyRate()` | نسبة | ▲ الأعلى | Chats the customer wrote in that got any reply (agent, AI or automation) |
| `avgFirstResponseMins()` | رقم | ▼ الأقل | Average minutes from the customer’s first message to the first teammate reply, answered chats |
| `medianFirstResponseMins()` | رقم | ▼ الأقل | Median of the same (the typical wait; not pulled up by a few very slow chats) |
| `answeredWithin5m()` | نسبة | ▲ الأعلى | Chats the customer wrote in that a teammate first answered within 5 minutes (unanswered = no) |
| `unansweredCount()` | رقم | ▼ الأقل | Chats the customer wrote in with no teammate reply yet |

### Chat messages — `FROM messages`

* حقل التاريخ: `created`.
* دون شرط تاريخ يعرض آخر 30 يومًا.

| الحقل | النوع | تجميع | سريع | القيم | الوصف |
| - | - | - | - | - | - |
| `id` | نص | | | | Message id |
| `conversation` | نص | | | | Conversation id |
| `direction` | قيمة من قائمة | ✓ | | `inbound` `outbound` | Direction |
| `sentBy` | قيمة من قائمة | ✓ | | `customer` `agent` `ai` `automation` `phone` `external` | agent = a teammate in the CRM; phone = a teammate from the WhatsApp app; external = e.g. Meta inbox or away messages |
| `agent` | عضو فريق | ✓ | | | The teammate who sent it from the CRM; empty for anything else |
| `agent.name` | نص | ✓ | | | Agent name |
| `type` | قيمة من قائمة | ✓ | | `text` `image` `video` `audio` `document` `sticker` `template` `interactive` `location` `reaction` | Message type |
| `status` | قيمة من قائمة | ✓ | | `received` `processing` `sent` `delivered` `read` `played` `failed` | Delivery status |
| `failed` | نعم / لا | ✓ | | | Failed |
| `owner` | عضو فريق | ✓ | | | Chat assigned to |
| `owner.name` | نص | ✓ | | | Chat assigned to (name) |
| `channel.name` | نص | ✓ | | | Channel |
| `channel.type` | قيمة من قائمة | ✓ | | `whatsapp` `facebook` `instagram` `tiktok` `telegram` `webchat` `sip` | Channel type |
| `created` | تاريخ | | ✓ | | Sent at |

| المقياس | النوع | الأفضل | الوصف |
| - | - | - | - |
| `received()` | رقم | | Messages from customers |
| `sent()` | رقم | | Messages to customers, by anyone (agent, AI, automation…) |
| `agentReplies()` | رقم | | Sent by a teammate from the CRM |
| `aiReplies()` | رقم | | Sent by the AI agent |
| `aiShare()` | نسبة | | AI messages / all outbound messages |
| `chats()` | رقم | | Distinct conversations with a message |
| `failRate()` | نسبة | ▼ الأقل | Outbound messages that failed to send |

### Customers — `FROM customers`

* حقل التاريخ: `created`.
* يبحث `text ~ "…"` في: `name`, `company`, `email`, `phone`.

| الحقل | النوع | تجميع | سريع | القيم | الوصف |
| - | - | - | - | - | - |
| `id` | نص | | | | Customer id |
| `chat` | نص | | | | The customer's most recent chat (id) |
| `name` | نص | | | | Name |
| `company` | نص | ✓ | ✓ | | Company |
| `email` | نص (مخفي) | | | | Email |
| `phone` | نص (مخفي) | | | | Phone |
| `status` | قيمة من قائمة | ✓ | ✓ | `active` `inactive` `prospect` `archived` | Status |
| `type` | قيمة من قائمة | ✓ | | `individual` `business` | Customer type |
| `source` | قيمة من قائمة | ✓ | | `website` `referral` `import` `manual` `api` | How the record was created (api = from a chat or integration) |
| `industry` | نص | ✓ | | | Industry |
| `owner` | عضو فريق | ✓ | ✓ | | Owner |
| `owner.name` | نص | ✓ | | | Owner name |
| `owner.email` | نص | ✓ | | | Owner email |
| `tags` | وسوم | | ✓ | | Tags |
| `channel.type` | قيمة من قائمة | ✓ | | `whatsapp` `facebook` `instagram` `tiktok` `telegram` `webchat` `sip` | Channel of their first chat. Per-row lookup: combine with a date or indexed filter on large workspaces |
| `conversation.lastInbound` | تاريخ | | | | Per-row lookup: combine with a date or indexed filter on large workspaces |
| `created` | تاريخ | | ✓ | | Created |
| `updated` | تاريخ | | | | Updated |

| المقياس | النوع | الأفضل | الوصف |
| - | - | - | - |
| `unassignedCount()` | رقم | ▼ الأقل | Customers nobody owns |
| `phoneRate()` | نسبة | ▲ الأعلى | Share of customers with a phone number |
| `emailRate()` | نسبة | ▲ الأعلى | Share of customers with an email address |

### Tasks — `FROM tasks`

* حقل التاريخ: `created`.
* يبحث `text ~ "…"` في: `title`.

| الحقل | النوع | تجميع | سريع | القيم | الوصف |
| - | - | - | - | - | - |
| `id` | نص | | | | Task id |
| `title` | نص | | | | Title |
| `status` | قيمة من قائمة | ✓ | ✓ | `pending` `in_progress` `on_hold` `completed` `cancelled` | Status |
| `open` | نعم / لا | ✓ | | | Pending, in progress or on hold |
| `overdue` | نعم / لا | ✓ | | | Open and past its due date |
| `priority` | قيمة من قائمة | ✓ | ✓ | `urgent` `high` `medium` `low` | Priority |
| `assignee` | عضو فريق | ✓ | ✓ | | The first assignee |
| `assignee.name` | نص | ✓ | | | Assignee name |
| `creator` | عضو فريق | ✓ | ✓ | | Created by |
| `creator.name` | نص | ✓ | | | Created by (name) |
| `tags` | قائمة | | | | Tags |
| `due` | تاريخ | | ✓ | | Due |
| `completedAt` | تاريخ | | | | Completed at |
| `hoursToComplete` | رقم | | | | Created → completed, hours; empty unless completed |
| `created` | تاريخ | | ✓ | | Created |
| `updated` | تاريخ | | | | Updated |

| المقياس | النوع | الأفضل | الوصف |
| - | - | - | - |
| `openCount()` | رقم | | Pending, in progress or on hold |
| `overdueCount()` | رقم | ▼ الأقل | Open tasks past their due date |
| `completionRate()` | نسبة | ▲ الأعلى | Completed / all tasks except cancelled |
| `avgHoursToComplete()` | رقم | ▼ الأقل | Created → completed, completed tasks only |

### Phone calls — `FROM calls`

* حقل التاريخ: `created`.
* دون شرط تاريخ يعرض آخر 90 يومًا.
* يبحث `text ~ "…"` في: `phone`, `disposition`.

| الحقل | النوع | تجميع | سريع | القيم | الوصف |
| - | - | - | - | - | - |
| `id` | نص | | | | Call id |
| `direction` | قيمة من قائمة | ✓ | | `inbound` `outbound` | Direction |
| `status` | قيمة من قائمة | ✓ | | `ended` `missed` `failed` `voicemail` `queued` `ringing` `connected` `wrapup` `ivr` `created` | Status |
| `answered` | نعم / لا | ✓ | | | Answered |
| `missed` | نعم / لا | ✓ | | | Inbound missed call, as the Calls report counts it (short abandons excluded) |
| `missedReason` | قيمة من قائمة | ✓ | | `no_answer` `abandoned` `short_abandon` `no_agent` `outside_hours` `caller_choice` `ivr_abandoned` `ivr_hangup` `owner_offline` `outbound_abandoned` | Missed reason |
| `endReason` | قيمة من قائمة | ✓ | | `busy` `no_answer` `rejected` `carrier_error` `internal_error` `cancelled` `no_line` `not_entitled` | Failure reason |
| `provider` | قيمة من قائمة | ✓ | | `sip` `whatsapp` | Line |
| `agent` | عضو فريق | ✓ | ✓ | | Agent |
| `agent.name` | نص | ✓ | | | Agent name |
| `phone` | نص (مخفي) | | | | Customer number |
| `channel.name` | نص | ✓ | | | Line name |
| `queue.name` | نص | ✓ | | | Queue |
| `duration` | رقم | ✓ | | | Talk time (s) |
| `waitSeconds` | رقم | ✓ | | | Wait (s) |
| `handleSeconds` | رقم | ✓ | | | Talk + wrap-up |
| `disposition` | نص | ✓ | | | Disposition |
| `hangupBy` | قيمة من قائمة | ✓ | | `caller` `agent` `system` | Hung up by |
| `transfers` | رقم | ✓ | | | Transfers |
| `recorded` | نعم / لا | ✓ | | | Recorded |
| `created` | تاريخ | | ✓ | | Started at |
| `answeredAt` | تاريخ | | | | Answered at |

| المقياس | النوع | الأفضل | الوصف |
| - | - | - | - |
| `answerRate()` | نسبة | ▲ الأعلى | Answered inbound / inbound calls offered to agents (Calls report definition) |
| `missedCount()` | رقم | ▼ الأقل | Inbound calls missed (short abandons excluded) |
| `avgHandleSeconds()` | رقم | | Talk + wrap-up of answered calls, seconds |
| `avgAnswerSeconds()` | رقم | ▼ الأقل | Seconds an answered inbound caller waited |

### Broadcasts — `FROM broadcasts`

* حقل التاريخ: `created`.
* يبحث `text ~ "…"` في: `name`, `template`.

| الحقل | النوع | تجميع | سريع | القيم | الوصف |
| - | - | - | - | - | - |
| `id` | نص | | | | Broadcast id |
| `name` | نص | | | | Name |
| `status` | قيمة من قائمة | ✓ | ✓ | `draft` `scheduled` `sending` `paused` `completed` `cancelled` `failed` | Status |
| `type` | قيمة من قائمة | ✓ | | `whatsapp` `sms` | Sent over |
| `template` | نص | ✓ | | | Template |
| `channel.name` | نص | ✓ | | | Channel |
| `campaign.name` | نص | ✓ | | | Campaign |
| `creator` | عضو فريق | ✓ | | | Created by |
| `creator.name` | نص | ✓ | | | Created by (name) |
| `fromAutomation` | نعم / لا | ✓ | | | From an automation |
| `recipients` | رقم | ✓ | | | Recipients |
| `sent` | رقم | ✓ | | | Sent |
| `delivered` | رقم | ✓ | | | Delivered |
| `read` | رقم | ✓ | | | Read |
| `failed` | رقم | ✓ | | | Failed |
| `created` | تاريخ | | ✓ | | Created |
| `scheduled` | تاريخ | | | | Scheduled for |
| `started` | تاريخ | | | | Started sending |
| `completedAt` | تاريخ | | | | Finished |

| المقياس | النوع | الأفضل | الوصف |
| - | - | - | - |
| `messagesSent()` | رقم | | Sum of messages sent |
| `deliveryRate()` | نسبة | ▲ الأعلى | Delivered / sent |
| `readRate()` | نسبة | ▲ الأعلى | Read / delivered |
| `failRate()` | نسبة | ▼ الأقل | Failed / (sent + failed) |

### Ad spend (Meta ads, per ad per day) — `FROM ads`

* حقل التاريخ: `date`.
* دون شرط تاريخ يعرض آخر 90 يومًا.
* يحتاج أيضًا إلى الصلاحية `analytics:read`.
* يبحث `text ~ "…"` في: `ad.name`, `campaign.name`.

| الحقل | النوع | تجميع | سريع | القيم | الوصف |
| - | - | - | - | - | - |
| `id` | نص | | | | Row id |
| `date` | تاريخ | | ✓ | | The ad account’s day |
| `ad` | نص | ✓ | | | Matches the ad field of conversations / orders / leads |
| `ad.name` | نص | ✓ | | | Ad |
| `campaign` | نص | ✓ | | | Campaign id |
| `campaign.name` | نص | ✓ | | | Campaign |
| `adset` | نص | ✓ | | | Ad set id |
| `adset.name` | نص | ✓ | | | Ad set |
| `account` | نص | ✓ | | | Ad account |
| `currency` | قيمة من قائمة | ✓ | | | Currency |
| `spend` | مبلغ | ✓ | | | Spend |
| `impressions` | رقم | ✓ | | | Impressions |
| `clicks` | رقم | ✓ | | | Clicks |
| `conversationsStarted` | رقم | ✓ | | | Meta’s own count of messaging conversations the ad started |

| المقياس | النوع | الأفضل | الوصف |
| - | - | - | - |
| `spend()` | مبلغ | | Amount spent (account currency) |
| `impressions()` | رقم | ▲ الأعلى | Times the ads were shown |
| `clicks()` | رقم | ▲ الأعلى | Clicks on the ads |
| `ctr()` | نسبة | ▲ الأعلى | Clicks / impressions |
| `conversationsStarted()` | رقم | ▲ الأعلى | Messaging conversations the ads started (Meta’s count) |
| `costPerConversation()` | مبلغ | ▼ الأقل | Spend / conversations started; empty with none |
| `cpc()` | مبلغ | ▼ الأقل | Spend / clicks |

### Team (per teammate) — `FROM team`

* صف لكل عضو في الفريق. فترة التقرير هي `period` (في WHERE فقط).
* دون شرط تاريخ يعرض آخر 30 يومًا.
* يبحث `text ~ "…"` في: `name`.

| الحقل | النوع | تجميع | سريع | القيم | الوصف |
| - | - | - | - | - | - |
| `user` | عضو فريق | ✓ | ✓ | | user = currentUser(), user IN membersOf("Sales") |
| `name` | نص | ✓ | | | Name |
| `email` | نص (مخفي) | | | | Email |
| `role` | نص | ✓ | | | Role name(s), comma separated when a teammate has several (filter with role \~ "Sales") |
| `team.name` | نص | ✓ | | | Team name(s), comma separated when in several; for one team prefer user IN membersOf("Team") |
| `active` | نعم / لا | ✓ | | | Active |
| `status` | قيمة من قائمة | ✓ | | `active` `inactive` `suspended` | Account status |
| `joined` | تاريخ | | | | Joined |
| `lastLogin` | تاريخ | | | | Last login |
| `period` | تاريخ | | ✓ | | Not a column: the window every metric is counted in, e.g. period >= startOfMonth(). Only in the top-level AND chain |

| المقياس | النوع | الأفضل | الوصف |
| - | - | - | - |
| `messagesSent()` | رقم | | Messages the teammate sent from the CRM in the period (messages sentBy = agent) |
| `chats()` | رقم | | Distinct chats the teammate sent a message in, in the period |
| `wonDeals()` | رقم | ▲ الأعلى | Deals they own that entered a won stage in the period (by wonAt; Sales-rep report definition) |
| `wonValue()` | مبلغ | ▲ الأعلى | Value of those won deals |
| `leadsAssigned()` | رقم | | Leads created in the period that they own |
| `openDeals()` | رقم | | Current deals they own in a stage neither won nor lost — right now, not limited by the period |
| `pipelineValue()` | مبلغ | ▲ الأعلى | Value of their open deals right now |
| `orders()` | رقم | ▲ الأعلى | Store orders placed in the period from chats they own |
| `revenue()` | مبلغ | ▲ الأعلى | Revenue of those orders, net of refunds, cancelled excluded (orders revenue()) |
| `callsAnswered()` | رقم | ▲ الأعلى | Calls they answered in the period |
| `tasksCompleted()` | رقم | ▲ الأعلى | Tasks they are assigned to that were completed in the period |
| `firstResponses()` | رقم | | Chats whose first teammate reply they sent, in the period (by when the reply was sent) |
| `avgFirstResponseMins()` | رقم | ▼ الأقل | Their average first-response minutes over those chats |
| `medianFirstResponseMins()` | رقم | ▼ الأقل | Their median first-response minutes (per teammate only; cannot be totalled over a group) |
| `wonPerChat()` | نسبة | ▲ الأعلى | wonDeals / chats |
| `messagesPerWon()` | رقم | | messagesSent / wonDeals; empty without a win |

## الدوال والعوامل

* **دوال التجميع:** `count()`, `countDistinct()`, `sum()`, `avg()`, `min()`, `max()`, `median()`, `p90()`, `rate()`
* **فترات التاريخ (GROUP BY):** `hour(field)`, `day(field)`, `week(field)`, `month(field)`, `quarter(field)`, `year(field)`, `hourOfDay(field)`, `weekday(field)`
* **القيم:** `now()`, `today()`, `yesterday()`, `startOfDay()`, `startOfWeek()`, `startOfMonth()`, `startOfQuarter()`, `startOfYear()`, `endOfDay()`, `endOfWeek()`, `endOfMonth()`, `endOfQuarter()`, `endOfYear()`, `currentUser()`, `membersOf()`
* **تواريخ نسبية:** `-30m`, `-12h`, `-7d`, `-2w`, `-3M`, `-1y`, `"-4w 2d"`
* **صيغ التاريخ:** `yyyy/MM/dd`, `yyyy-MM-dd`, `yyyy-MM-dd HH:mm`, `ISO 8601 with zone`
* **العوامل:** `=`, `!=`, `>`, `>=`, `<`, `<=`, `~`, `!~`, `IN`, `NOT IN`, `ANY`, `ALL`, `IS EMPTY`, `IS NOT EMPTY`
* **COMBINE:** حتى 4 استعلامات، وحتى 10 أعمدة CALC
* **DRILL:** السجلات خلف الرقم، 50 في كل صفحة، مع رابطَي فتح المحادثة وفتح الصفقة

## مزيد من الأمثلة

```sql theme={null}
FROM leads WHERE created >= startOfMonth() GROUP BY stage SHOW count(), sum(value)
```

```sql theme={null}
FROM leads WHERE created >= -90d GROUP BY owner.name SHOW count(), wonValue(), winRate()
```

```sql theme={null}
FROM orders WHERE created >= -30d GROUP BY governorate SHOW count(), revenue(), cancelRate()
```

```sql theme={null}
FROM orders WHERE created >= -12w AND cod = true GROUP BY week(created) SHOW count(), confirmRate()
```

```sql theme={null}
FROM conversations WHERE created >= -30d GROUP BY channel.type SHOW count(), waitingCount(), replyRate()
```

```sql theme={null}
FROM messages WHERE created >= -7d GROUP BY day(created), sentBy SHOW count()
```

```sql theme={null}
FROM customers WHERE created >= startOfYear() GROUP BY month(created) SHOW count(), unassignedCount()
```

```sql theme={null}
FROM tasks WHERE overdue = true GROUP BY assignee.name SHOW count()
```

```sql theme={null}
FROM calls WHERE created >= -30d GROUP BY agent.name SHOW count(), answerRate(), avgHandleSeconds()
```

```sql theme={null}
FROM broadcasts WHERE created >= -90d GROUP BY campaign.name SHOW messagesSent(), deliveryRate(), readRate()
```

```sql theme={null}
FROM products WHERE created >= -30d GROUP BY name SHOW units(), revenue() ORDER BY revenue() DESC LIMIT 10
```

```sql theme={null}
FROM messages WHERE direction = inbound GROUP BY weekday(created), hourOfDay(created) SHOW count()
```

```sql theme={null}
FROM leads WHERE stage.won = true GROUP BY source SHOW median(daysToWin), p90(daysToWin)
```

```sql theme={null}
FROM orders WHERE created >= -90d GROUP BY repeatCustomer SHOW count(), revenue(), customers()
```

```sql theme={null}
FROM orders WHERE created >= -30d GROUP BY channel.type AS channel SHOW count() AS orders COMBINE BY channel FROM conversations WHERE created >= -30d GROUP BY channel.type AS channel SHOW count() AS chats CALC orders_per_chat = orders / chats ORDER BY orders DESC
```

```sql theme={null}
FROM leads WHERE created >= -2w GROUP BY day(created) AS day SHOW count() AS leads COMBINE BY day FROM orders WHERE created >= -2w GROUP BY day(created) AS day SHOW count() AS orders
```

```sql theme={null}
FROM team SHOW name, wonDeals(), messagesSent(), chats() ORDER BY wonDeals DESC
```

```sql theme={null}
FROM team WHERE period >= startOfMonth() SHOW name, messagesSent(), chats(), wonDeals(), orders(), revenue()
```

```sql theme={null}
FROM team WHERE period >= -90d GROUP BY team.name SHOW count(), messagesSent(), wonDeals(), wonPerChat()
```

```sql theme={null}
FROM orders WHERE created >= -30d GROUP BY governorate SHOW count(), deliveryRate(), returnRate()
```

```sql theme={null}
FROM orders WHERE created >= -30d GROUP BY deliveryStatus SHOW count(), revenue()
```

```sql theme={null}
FROM leads WHERE created >= -90d GROUP BY source SHOW count(), orderConversion(), orderRevenue()
```

```sql theme={null}
FROM orders WHERE created >= startOfMonth() GROUP BY channel.type AS channel SHOW count() AS this_month COMBINE BY channel FROM orders WHERE created >= startOfMonth(-1) AND created < startOfMonth() GROUP BY channel.type AS channel SHOW count() AS last_month CALC change = this_month - last_month, change_pct = ROUND((this_month - last_month) * 100 / last_month, 1)
```

```sql theme={null}
FROM orders WHERE created >= startOfMonth() SHOW revenue() AS this_month COMBINE FROM orders WHERE created >= startOfMonth(-1) AND created < startOfMonth() SHOW revenue() AS last_month CALC change_pct = ROUND((this_month - last_month) * 100 / last_month, 1)
```

```sql theme={null}
FROM conversations WHERE created >= -30d AND fromAd = true GROUP BY ad.name SHOW count(), replyRate()
```

```sql theme={null}
FROM ads WHERE date >= -30d GROUP BY campaign.name SHOW spend(), clicks(), ctr(), costPerConversation()
```

```sql theme={null}
FROM ads WHERE date >= -30d GROUP BY ad SHOW spend() AS spend COMBINE BY ad FROM orders WHERE created >= -30d AND ad IS NOT EMPTY GROUP BY ad SHOW revenue() AS revenue CALC roas = ROUND(revenue / spend, 2)
```

```sql theme={null}
FROM conversations WHERE created >= -30d GROUP BY firstResponder.name SHOW count(), avgFirstResponseMins(), medianFirstResponseMins(), answeredWithin5m()
```

```sql theme={null}
FROM team WHERE period >= startOfMonth() SHOW name, firstResponses(), avgFirstResponseMins(), medianFirstResponseMins()
```

```sql theme={null}
FROM orders WHERE created >= -30d GROUP BY governorate SHOW count(), revenue() DRILL SHOW number, customer, total, deliveryStatus
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.