Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
How can I pull out a row in a GROUP BY Django query?

I have a table like this:

EVENT
-------------
user_id   thumbnail_name
0         event/abc.jpg
0         event/efg.jpg
0         event/xyz.jpg
1         event/lmn.jpg
1         event/opq.jpg
2         event/rsf.jpg

How can I GROUP BY user_id and retrieve an arbitrary thumbnail? Expected output:

[ {user_id: 0, thumbnail_name: event/abc.jpg},
  {user_id: 1, thumbnail_name: event/lmn.jpg},
  {user_id: 2, thumbnail_name: event/rsf.jpg},
]

Is this even possible in SQL much less Django ORM?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use FOR JSON AUTO in SQl Server

SELECT *
FROM EVENT
FOR JSON AUTO

If you expect to return just a single row, then you can add Without_Array_Wrapper after FOR JSON AUTO with a comma after the AUTO keyword, and you will get an object instead of an array:

SELECT *
 FROM EVENT
 FOR JSON AUTO, Without_Array_Wrapper;

in MySQL

SELECT json_object('user_id', user_id, 'thumbnail_name', thumbnail_name)
FROM EVENT;

Fuhrer Modifications

SELECT JSON_ARRAYAGG(JSON_OBJECT('user_id', user_id, 'thumbnail_name', thumbnail_name)) from EVENT;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!