Question: I have defined a React select component as follows:

```
   <FormControl>
      <InputLabel id="demo-simple-select-label">Age</InputLabel>
      <Select
        labelId="demo-simple-select-label"
        id="demo-simple-select"
        label="Age"
      >
        <MenuItem value={10}>Ten</MenuItem>
        <MenuItem value={20}>Twenty</MenuItem>
        <MenuItem value={30}>Thirty</MenuItem>
      </Select>
    </FormControl>

```

This renders as follows:

* When not clicked:  

[![enter image description here](https://i.stack.imgur.com/6LZrh.png)](https://i.stack.imgur.com/6LZrh.png)
* When clicked:  

[![enter image description here](https://i.stack.imgur.com/0PYIf.png)](https://i.stack.imgur.com/0PYIf.png)

Notice that when not clicked, the label Age is not completely displayed.

I know I can mage label "Age" display completely by having set `sx={{minWidth: 80}}` on `FormControl`. But that is not "Automatic". What if my label changes dynamically? I somehow want the select component to adjust width to display label completely. Is it possible?

Answer: 
With respect, using `id` numbers to determine numbers of rows in a time period is incorrect. DELETEing rows leaves gaps in id number sequences; they are not designed for this purpose.

This is a job for `date_trunc()`, `COUNT(*)`, and `GROUP BY`.

The `date_trunc('day', created_at)` function turns an arbitrary timestamp into midnight on its day. For example, it turns ``2022-03-02 16:41:00`into`2022-03-02 00:00:00`. Using that we can write the query this way.

```sql
SELECT COUNT(*) order_count, 
       date_trunc('day', created_at) day
  FROM orders
 WHERE created_at >= date_trunc('day', NOW()) - INTERVAL '7 day'
   AND created_at <  date_trunc('day', NOW())
 GROUP BY date_trunc('day', created_at)

```

This query gives the number of orders on each day in the last 7 days.

Every minute you spend learning how to use SQL data arithmetic like this will pay off in hours saved in your work.
Try this :

```
SELECT  d.ref_date :: date AS "date"
     , count(*) AS "total requests"
  FROM generate_series('20220301' :: timestamp, '20220331' :: timestamp, '1 day') AS d(ref_date)
  LEFT JOIN orders
    ON date_trunc('day', d.ref_date) = date_trunc('day', created_at)
 GROUP BY d.ref_date

```

* `generate_series()` generates the list of reference days where you
want to count the number of orders
* Then you join with the `orders` table by comparing the reference date with the `created_at` date on year/month/day only. `LEFT JOIN` allows you to select reference days with no existing order.
* Finally you count the number of orders per day by grouping by reference day.