Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Districts and Their Grama Panchayats and Wards
Comment thread
sonzsara marked this conversation as resolved.

> Hierarchical mapping of districts to their grama panchayats and wards

## Purpose

Provides a location hierarchy list of government organizations by mapping:
- District -> Grama Panchayat -> Ward


---

## Query

```sql
SELECT
district.id AS district_id,
district.name AS district,
grama.id AS grama_panchayat_id,
grama.name AS grama_panchayat,
ward.id AS ward_id,
ward.name AS ward
FROM emr_organization AS district
JOIN emr_organization AS grama
ON grama.parent_cache @> ARRAY[district.id::integer]
AND grama.deleted = false
AND grama.org_type = 'govt'
AND grama.metadata->>'govt_org_type' = 'grama_panchayat'
JOIN emr_organization AS ward
ON ward.parent_id = grama.id
AND ward.deleted = false
AND ward.org_type = 'govt'
AND ward.metadata->>'govt_org_type' = 'ward'
WHERE district.deleted = false
AND district.org_type = 'govt'
AND district.level_cache = 1
ORDER BY district.name, grama.name, ward.name;
Comment thread
sonzsara marked this conversation as resolved.
```

## Notes

- This query has no runtime parameters.
- The district records are represented at `level_cache = 1`.
- Filters only non-deleted government organizations (`org_type = 'govt'`).
- Government subtype filtering is based on `metadata->>'govt_org_type'` values:
- `grama_panchayat`
- `ward`

*Last updated: 2026-08-03*