SQLite Forum

ORDER BY not ordering with the correct result
Login
I think they are probably about the same so I would generally prefer the latter one because it makes more clear exactly what you are doing by defining the things you need to define (the ProjID list), and then generating the "Proj_List" and "Proj_Budget" minimal set of rows you are interested in, then using a pretty simple "final select / projection" to get what you want to see.

That form is easiest to understand and leaves all the work to the query planner to figure out how best to compute what you asked for.

```
WITH ProjIDs AS
     (
      VALUES ('PR0000016675'),
             ('PR0000017285'),
             ('PR0000017416'),
             ('PR0000019109'),
             ('PR0000019129'),
             ('PR0000019130'),
             ('PR0000019131'),
             ('PR0000019133'),
             ('PR0000019135'),
             ('PR0000019136'),
             ('PR0000019139'),
             ('PR0000019140'),
             ('PR0000019141'),
             ('PR0000019142'),
             ('PR0000019143'),
             ('PR0000019147'),
             ('PR0000019148'),
             ('PR0000019150'),
             ('PR0000019152'),
             ('PR0000019164'),
             ('PR0000019176'),
             ('PR0000019180'),
             ('PR0000019186'),
             ('PR0000019191'),
             ('PR0000019764')
     ),
     Proj_List AS
     (
      select *
        from Project_List as o
       where ProjID in ProjIDs
         and InsertDate = (
                           select max(InsertDate)
                             from Project_List
                            where ProjID = o.ProjID
                          )
     ),
     Proj_Budget AS
     (
      select *
        from ABT_Budget as o
       where InsertDate = (
                           select max(InsertDate)
                             from ABT_Budget
                            where ProjID = o.ProjID
                          )
     )
   select a.*,
          sum(b.AnnualDossier) as Dossier
     from Proj_List as a
left join Proj_Budget as b
       on a.ProjID == b.ProjID
 group by a.ProjID
 order by a.Project_Manager
;
```