create a postgresql sql to fetch swaps where SourceCurrencyId = 27 order by CreatedDate and group by UserId
To fetch swaps where `SourceCurrencyId = 27`, order the results by `CreatedDate`, and group them by `UserId`, you can use the following SQL query in PostgreSQL:

```sql
SELECT UserId, 
       ARRAY_AGG(SwapId) AS SwapIds,  -- Assuming you want to collect SwapIds for each user
       MIN(CreatedDate) AS FirstSwapDate,  -- Get the earliest CreatedDate for the group
       MAX(CreatedDate) AS LastSwapDate    -- Get the latest CreatedDate for the group
FROM Swaps  -- Replace 'Swaps' with your actual table name
WHERE SourceCurrencyId = 27
GROUP BY UserId
ORDER BY FirstSwapDate;  -- Order by the first swap date
```

### Explanation:
- `SELECT UserId`: We're selecting the `UserId` to group by.
- `ARRAY_AGG(SwapId)`: This aggregates the `SwapId`s into an array for each user. You can adjust this based on what you need.
- `MIN(CreatedDate)` and `MAX(CreatedDate)`: These aggregate functions help you to find the first and last swap dates for each user.
- `FROM Swaps`: This specifies the table from which we're retrieving the data. Make sure to replace `Swaps` with your actual table name.
- `WHERE SourceCurrencyId = 27`: This filters the results to only include swaps with a specific `
 Here is a PostgreSQL SQL query that should accomplish what you're looking for:
```sql
SELECT UserId, MAX(CreatedDate) as MaxCreatedDate, SourceCurrencyId, TargetCurrencyId, SUM(Amount) as TotalAmount
FROM Swaps
WHERE SourceCurrencyId = 27
GROUP BY UserId, SourceCurrencyId, TargetCurrencyId
ORDER BY MaxCreatedDate DESC;
```
This query will select the `UserId`, the latest `CreatedDate` (using the `MAX()` function), `SourceCurrencyId`, `TargetCurrencyId`, and the sum of the `Amount` for each user where `SourceCurrencyId` is 27. It will then group the results by `UserId`, `SourceCurrencyId`, and `TargetCurrencyId`, and order the results by the latest `CreatedDate` in descending order.

Note that I assumed that `Swaps` is the name of the table containing the swap data. Please replace it with the actual name of your table if it's different. Also, I assumed that `Amount` is the column containing the amount of the swap. If it's different, please replace it with the actual column name.