# annotation in a raw query

**URL:** <https://forum.djangoproject.com/t/annotation-in-a-raw-query/24929>\
**Category:** Using the ORM\
**Created:** [October 31, 2023, 6:25pm UTC](https://forum.djangoproject.com/t/annotation-in-a-raw-query/24929 "2023-10-31T18:25:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave-lawrence-dev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dave-lawrence-dev/32/16746_2.png) [@dave-lawrence-dev](https://forum.djangoproject.com/u/dave-lawrence-dev)\
**Post date:** [October 31, 2023, 6:25pm UTC](https://forum.djangoproject.com/t/annotation-in-a-raw-query/24929/1 "2023-10-31T18:25:54Z")

</div>

I am writing a query to find entries in a table where the value of one of the fields changes and report the timestamps when this happens.

I have this working using a raw query as it needs to use a LAG and a comparison which I don’t believe is possible without a WITH which isn’t available in django

in the model ‘update\_time’ is a DateTime.

Here is the code snippet:

```auto
Q = models.MyTable.objects.raw("""
WITH s2 as (
SELECT
  id,
  site_id,
  update_time,
  LAG(site_id,1,0) OVER(ORDER BY update_time) AS old_site,
  LAG(update_time,1,"2000-01-01 00:00:00.000001+00:00") OVER(ORDER BY update_time) AS old_time
  FROM my_table WHERE my_id=12345
)
SELECT id,update_time,site_id,old_time FROM s2 WHERE NOT(site_id=old_site);
""")

```

The problem is that old\_time appears in the model as string, not a date time. It’s also missing any TZ info. Experimentally If I return it “AS” one of my other model fields that is defined as a DateTime then I get what I want, but can I do this legally?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 31, 2023, 7:18pm UTC](https://forum.djangoproject.com/t/annotation-in-a-raw-query/24929/2 "2023-10-31T19:18:33Z")

</div>

I can’t address your questions about your data field directly, but…

You don’t need to use the `WITH` statement for your query.

You should be able to rewrite your SQL as:

```auto
select * from (
  select id, site_id, update_time,
  LAG(site_id,1,0) over (order by update_time) as old_site,
  LAG(update_time, 1, "2000-01-01 00:00:00.000001+00:00") 
      OVER(ORDER BY update_time) AS old_time
  from my_table where my_id = 12345
) as s2 where not(site_id = old_site);

```

which _might_ put you in a position where you can resolve the issues with using the ORM.

---

<div class="post-metadata">

**Author:** ![dave-lawrence-dev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dave-lawrence-dev/32/16746_2.png) [@dave-lawrence-dev](https://forum.djangoproject.com/u/dave-lawrence-dev)\
**Post date:** [October 31, 2023, 7:35pm UTC](https://forum.djangoproject.com/t/annotation-in-a-raw-query/24929/3 "2023-10-31T19:35:27Z")

</div>

Hi Ken,  
Your rewrite does work as you suggest, thanks. (I know django reasonably, but I’m very new to the SQL-world of windows, partitions and lags and leads in SQL let alone in the ORM, so translating working SQL to Django is currently quite slow wading through the wall of unfamiliar jargon).

I’ve been digging through the django source code, specifically `django/db/models/query.py` and examining `RawQuerySet.iterator` but I don’t think there’s a way to wedge in the required hook into `converters` in order to supply the required conversion to a `DateTime`

---

<div class="post-metadata">

**Author:** ![dave-lawrence-dev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dave-lawrence-dev/32/16746_2.png) [@dave-lawrence-dev](https://forum.djangoproject.com/u/dave-lawrence-dev)\
**Post date:** [November 3, 2023, 2:52pm UTC](https://forum.djangoproject.com/t/annotation-in-a-raw-query/24929/4 "2023-11-03T14:52:14Z")

</div>

Found this actually caused by sqlite3. with psql a datetime is returned as expected
