title: Prisma client query for latest values of each user
body: <p>I am new to <code>prisma-client</code> and I want to return the latest value register by each user of a given company.</p>
  <p>This is my <code>schema.prisma</code>:</p>
  <pre><code>model Locations {
      id        Int    @id @default(autoincrement())
      user      String
      company   String
      latitude  String
      longitude String
      timestamp String
  }
  </code></pre>
  <p>This is what I have tried so far:</p>
  <pre><code>const lastLocations = await db.locations.findMany({
      where: {
        company,
      },
      orderBy: {
        id: 'desc',
      },
      take: 1,
    });
  </code></pre>
  <p>But I need to get 1 value for each user, I solved this previously in sql with:</p>
  <pre><code>WITH ranked_messages AS (   SELECT m.*, ROW_NUMBER() OVER (PARTITION BY userID ORDER BY timestamp DESC) AS rn FROM locations AS m ) SELECT * FROM ranked_messages WHERE rn = 1 AND companyID = &quot;${companyID}&quot;;`;
  
  </code></pre>
  <p>But I have no idea how to do proceed in prisma. Is there an &quot;each&quot; method?</p>
  <p>I appreciate any help. Thank you.</p>