# Bar

<table data-card-size="large" data-view="cards"><thead><tr><th data-card-target data-type="content-ref"></th><th data-hidden></th><th data-hidden></th><th data-hidden></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td><a href="/pages/2ezY2RInAwmBulQOlYT4#basic-bar-chart">/pages/2ezY2RInAwmBulQOlYT4#basic-bar-chart</a></td><td></td><td></td><td></td><td><a href="/files/1ZlZmT6bKXq2dJjQ70rJ">/files/1ZlZmT6bKXq2dJjQ70rJ</a></td></tr><tr><td><a href="/pages/2ezY2RInAwmBulQOlYT4#bar-chart-with-parent-and-sub-category-groups">/pages/2ezY2RInAwmBulQOlYT4#bar-chart-with-parent-and-sub-category-groups</a></td><td></td><td></td><td></td><td><a href="/files/dshQViJ2oCRwVQZwMsq3">/files/dshQViJ2oCRwVQZwMsq3</a></td></tr><tr><td><a href="/pages/2ezY2RInAwmBulQOlYT4#bar-chart-with-formats">/pages/2ezY2RInAwmBulQOlYT4#bar-chart-with-formats</a></td><td></td><td></td><td></td><td><a href="/files/jAroCTyrwHnEYAM3HJku">/files/jAroCTyrwHnEYAM3HJku</a></td></tr><tr><td><a href="/pages/2ezY2RInAwmBulQOlYT4#bar-with-stacking">/pages/2ezY2RInAwmBulQOlYT4#bar-with-stacking</a></td><td></td><td></td><td></td><td><a href="/files/hcRHE8kolLghTEzS2rIZ">/files/hcRHE8kolLghTEzS2rIZ</a></td></tr></tbody></table>

## Basic Bar Chart

```sql
-- @chart: bar
-- @title: Bar Chart
-- @subtitle: An basic example of a bar chart
SELECT 
Channel,
count(*) as Won_Sales
FROM Sales
WHERE Status = 'Won'
GROUP BY Channel
ORDER BY Won_Sales DESC;
```

<figure><img src="/files/1ZlZmT6bKXq2dJjQ70rJ" alt=""><figcaption><p>An example of a bar chart</p></figcaption></figure>

| CHANNEL  | WON\_SALES |
| -------- | ---------- |
| pr\_ad   | 21.0       |
| coldcall | 37.0       |
| search   | 66.0       |
| event    | 112.0      |
| referral | 243.0      |

## Bar chart with Parent and Sub Category Groups

```sql
-- @chart: bar
-- @title: Groups - Multiple category groups
-- @subtitle: An example of multiple category groups
-- @groups: Channel, Owner
-- @series: TotalSales
-- @formats: currency
SELECT 
	Final.*
FROM (  
  SELECT 
  	Channel,    
  	Owner,
    (
      SELECT sum(Sub.Amount)
      FROM Sales Sub
      WHERE Sub.Channel = Sales.Channel
    ) as TotalChannelSales,
    sum(Amount) as TotalSales
  FROM Sales
  WHERE Owner IN (
    SELECT Owner
    FROM Sales
    GROUP BY Owner
    ORDER BY sum(Amount)
    LIMIT 10
  )
  GROUP BY Channel, Owner
) as Final
ORDER BY TotalChannelSales DESC, TotalSales DESC

```

<figure><img src="/files/dshQViJ2oCRwVQZwMsq3" alt=""><figcaption><p>An example bar chart with group parent and sub category</p></figcaption></figure>

| CHANNEL  | OWNER   | TOTALCHANNELSALES | TOTALSALES |
| -------- | ------- | ----------------- | ---------- |
| pr\_ad   | Kim     | 9181300.0         | 84603.0    |
| pr\_ad   | Roger   | 9181300.0         | 118985.0   |
| pr\_ad   | Lucy    | 9181300.0         | 138374.0   |
| pr\_ad   | Norbit  | 9181300.0         | 141711.0   |
| pr\_ad   | Justice | 9181300.0         | 160517.0   |
| pr\_ad   | Heather | 9181300.0         | 239870.0   |
| pr\_ad   | Peter   | 9181300.0         | 266655.0   |
| pr\_ad   | Tammy   | 9181300.0         | 501805.0   |
| coldcall | Justice | 1.7372264E7       | 87207.0    |
| coldcall | Heather | 1.7372264E7       | 162974.0   |
| coldcall | Norbit  | 1.7372264E7       | 333648.0   |

## Bar Chart with Formats

```sql
-- @chart: bar
-- @title: Bar Chart With Formats
-- @subtitle: An example bar chart with formats
-- @formats: currency
SELECT 
Channel,
sum(Amount) as Total_Sales
FROM Sales
WHERE Status = 'Won'
GROUP BY Channel
ORDER BY Total_Sales DESC;
```

<div align="left"><figure><img src="/files/jAroCTyrwHnEYAM3HJku" alt=""><figcaption><p>An example bar chart with specified currency format</p></figcaption></figure></div>

| CHANNEL  | TOTAL\_SALES |
| -------- | ------------ |
| pr\_ad   | 1716590.0    |
| coldcall | 3267837.0    |
| search   | 6320993.0    |
| event    | 1.1198055E7  |
| referral | 2.6029056E7  |

## Bar with Stacking

```sql
-- @chart: bar
-- @title: Bar Stacking - Example bar chart with stacking
-- @groups: Owner, Channel
-- @subtitle: An example bar chart that has a single stack
-- @formats: currency
-- @series: Sales
-- @stacks: Channel
SELECT *
FROM (  
  SELECT 
      Owner,
      Channel,
      (
        SELECT sum(Amount)
        FROM Sales Sub
        WHERE Sub.Owner = Sales.Owner
      ) as OwnerTotal,
      sum(amount) as Sales
  FROM sales
  GROUP BY Owner, Channel
) as Final
ORDER BY Final.OwnerTotal ASC, Sales DESC;
```

<figure><img src="/files/hcRHE8kolLghTEzS2rIZ" alt=""><figcaption><p>An example bar chart with stacking</p></figcaption></figure>

| OWNER   | CHANNEL  | OWNERTOTAL | SALES     |
| ------- | -------- | ---------- | --------- |
| Roger   | referral | 1459150.0  | 607758.0  |
| Roger   | coldcall | 1459150.0  | 365129.0  |
| Roger   | search   | 1459150.0  | 223080.0  |
| Roger   | event    | 1459150.0  | 144198.0  |
| Roger   | pr\_ad   | 1459150.0  | 118985.0  |
| Lucy    | referral | 1460022.0  | 914463.0  |
| Lucy    | event    | 1460022.0  | 267908.0  |
| Lucy    | search   | 1460022.0  | 139277.0  |
| Lucy    | pr\_ad   | 1460022.0  | 138374.0  |
| Kim     | referral | 1768826.0  | 1338296.0 |
| Kim     | search   | 1768826.0  | 177605.0  |
| Kim     | event    | 1768826.0  | 168322.0  |
| Kim     | pr\_ad   | 1768826.0  | 84603.0   |
| Grenda  | referral | 2411989.0  | 1567753.0 |
| Grenda  | event    | 2411989.0  | 511020.0  |
| Grenda  | search   | 2411989.0  | 333216.0  |
| Irene   | referral | 2798575.0  | 1698665.0 |
| Irene   | search   | 2798575.0  | 706034.0  |
| Irene   | event    | 2798575.0  | 393876.0  |
| Norbit  | referral | 2921665.0  | 1282432.0 |
| Norbit  | event    | 2921665.0  | 722410.0  |
| Norbit  | search   | 2921665.0  | 441464.0  |
| Norbit  | coldcall | 2921665.0  | 333648.0  |
| Norbit  | pr\_ad   | 2921665.0  | 141711.0  |
| Justice | referral | 3292632.0  | 1971882.0 |
| Justice | search   | 3292632.0  | 638796.0  |
| Justice | event    | 3292632.0  | 434230.0  |
| Justice | pr\_ad   | 3292632.0  | 160517.0  |

## Heading


---

# 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://docs.chartsql.com/charts/bar.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.
