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

382
Views
TypeORM distinctOn with orderBy

I'm Trying to get the latest message for a user, Ether if he is the sender or the receiver But I can't use distinctOn with orderBy

     const quiry = this.createQueryBuilder('message')
    .orderBy('message.date', 'DESC')
    .where('message.senderId = :id OR message.receiverId = :id',{id:user.id})
    .distinctOn(['message.senderId','message.receiverId'])
    const messages = await quiry.getRawMany();

SELECT DISTINCT ON ("message"."senderId", "message"."receiverId") "message"."id" AS "message_id", "message"."message" AS "message_message", "message"."date" AS "message_date", "message"."status" AS "message_status", "message"."senderId" AS "message_senderId", "message"."receiverId" AS "message_receiverId" FROM "message" "message" WHERE "message"."senderId" = $1 OR "message"."receiverId" = $2 ORDER BY "message"."date" DESC

SELECT DISTINCT ON expressions must match initial ORDER BY expressions
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can also try this way

const messages = this.createQueryBuilder('message')
        .distinctOn('DISTINCT ON (message.senderId) as senderId, message.receiverId' as "receiverId")
        .where('message.senderId = :id OR message.receiverId = :id',{id:user.id})
        .orderBy('message.date', 'DESC').getRawMany()
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!