Question

Sorry for the long post I'm new to this & want to make sure that I'm fully understood.

I'm trying to make an order by & group query. I've started with the order by part:

SELECT "tId", "mId","sId","tr", "tg","tp", "date" 
FROM table 
WHERE "tId" =1 
ORDER BY "date" DESC, "mId","sId";

the ouput:

 tId |  mId  | sId  | tr  | tg | tp    |          date                                            
-----+-------+------+-----+----+-------+------------------------
   1 |     5 |    2 | -73 |  1 |   122 | 2007-01-01 02:03:01+02
   1 |     5 |    1 | -72 |  1 |   122 | 2007-01-01 02:02:01+02
   1 |     4 |    1 | -70 |  1 |   120 | 2007-01-01 01:01:01+02
   1 |     1 |    1 | -30 |  0 |     0 | 2004-10-19 10:23:54+02
   1 |     1 |    2 | -31 |  0 |     0 | 2004-10-19 10:23:54+02
   1 |     1 |    3 | -32 |  0 |     0 | 2004-10-19 10:23:54+02
   1 |     2 |    1 | -40 |  0 |     0 | 2004-10-19 10:23:54+02
   1 |     2 |    2 | -41 |  0 |     0 | 2004-10-19 10:23:54+02
   1 |     2 |    3 | -42 |  0 |     0 | 2004-10-19 10:23:54+02
   1 |     3 |    1 | -50 |  0 |     0 | 2004-10-19 10:23:54+02
   1 |     3 |    3 | -50 |  0 |     0 | 2004-10-19 10:23:54+02

The query I would like to do is to group the output of the prev' result and to get:

 mId |    agg_r     | agg_tg  |  agg_tp   | agg_sid |                                   agg_date           
-----+--------------+---------+-----------+----------+------------------------------------------------------------------------------
  5 | {-73,-72}     | {1,1}   | {122,122} | {2,1}    | {"2007-01-01 02:03:01+02","2007-01-01 02:02:01+02"}
  4 | {-70}         | {1}     | {120}     | {1}      | {"2007-01-01 01:01:01+02"}
  1 | {-30,-31,-32} | {0,0,0} | {0,0,0}   | {1,2,3}  | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}
  2 | {-40,-41,-42} | {0,0,0} | {0,0,0}   | {1,2,3}  | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}
  3 | {-50,-50}     | {0,0}   | {0,0}     | {1,3}    | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}

So I've assumed this would work:

SELECT "mId", array_agg("tr") AS agg_r, array_agg("tg") AS agg_tg, array_agg("tp") AS agg_tp, array_agg("sId") AS agg_sid ,array_agg("date") AS agg_date 
FROM (
   SELECT "tId", "mId","sId","tr", "tg","tp", "date" 
   FROM table 
   WHERE "tId" =1 
   ORDER BY "date" DESC, "mId","sId"
)AS qRes 
GROUP BY qRes."mId";

But I'm getting:

 mId |    agg_r     | agg_tg  |  agg_tp   | agg_sid |                                   agg_date           
-----+--------------+---------+-----------+----------+------------------------------------------------------------------------------
  1 | {-30,-31,-32} | {0,0,0} | {0,0,0}   | {1,2,3}  | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}
  4 | {-70}         | {1}     | {120}     | {1}      | {"2007-01-01 01:01:01+02"}
  2 | {-40,-41,-42} | {0,0,0} | {0,0,0}   | {1,2,3}  | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}
  3 | {-50,-50}     | {0,0}   | {0,0}     | {1,3}    | {"2004-10-19 10:23:54+02","2004-10-19 10:23:54+02"}
  5 | {-73,-72}     | {1,1}   | {122,122} | {2,1}    | {"2007-01-01 02:03:01+02","2007-01-01 02:02:01+02"}

What am I missing? why does the grouping changes the order?

Was it helpful?

Solution

Like the comment says, there isn't any order on the outer query. Notice the last line.

SELECT "mId", array_agg("tr") AS agg_r, array_agg("tg") AS agg_tg, array_agg("tp") AS agg_tp, array_agg("sId") AS agg_sid ,array_agg("date") AS agg_date 
FROM (
   SELECT "tId", "mId","sId","tr", "tg","tp", "date" 
   FROM table 
   WHERE "tId" =1 
   ORDER BY "date" DESC, "mId","sId"
)AS qRes 
GROUP BY qRes."mId"
order by max("date") desc;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top