Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 321749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:52:27+00:00 2026-05-12T08:52:27+00:00

I need to (safely) handle the case where there is < 5 reviews for

  • 0

I need to (safely) handle the case where there is < 5 “reviews” for a product (even none). In addition, being able to define the ordering of the “reviews” is important. I would prefer a solution that would work with both SQL Server and PostgreSQL (with minor modification). I would be willing to use PIVOT as a last resort, but from what I have read it won’t do what I want it to do.

Table1
id, product
1, ‘Product 1’
2, ‘Product 2’

Table2
id, review
1, ‘review #1a’
1, ‘review #1b’
1, ‘review #1c’
1, ‘review #1d’
1, ‘review #1e’
1, ‘review #1f’
2, ‘review #2a’
2, ‘review #2b’
2, ‘review #2c’
2, ‘review #2d’
2, ‘review #2e’
2, ‘review #2f’

Result
1, ‘Product 1’, ‘review #1a’, ‘review #1b’, ‘review #1c’, ‘review #1d’, ‘review #1e’
2, ‘Product 2’, ‘review #2a’, ‘review #2b’, ‘review #2c’, ‘review #2d’, ‘review #2e’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:52:28+00:00Added an answer on May 12, 2026 at 8:52 am

    Try this (tweaked to refer to the appropriate things):

    The idea is… use row_number() to get up to 5 reviews out per product. Then pivot the results, and then do a left join to get product details (including those that don’t have any reviews).

    with top5reviews as 
    (
    select * 
    from
    (
    select 
      productid, review
      , row_number() over (partition by productid order by reviewid) as reviewnum
    from reviews
    ) r
    where reviewnum <= 5
    )
    , pivotted as
    (
    select productid, [1] as review1, [2] as review2, [3] as review3, [4] as review4, [5] as review5
    from top5reviews r
     pivot
     (max(review) for reviewnum in ([1],[2],[3],[4],[5])) p
    )
    select *
    from products p
      left join
      pivotted r
      on p.productid = r.productid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As there is no helper for File Input , how can we safely handle
I need to know if i can safely use normal string or I have
I need to be able to let multiple instances of the same form be
Specifically common.inc. I need to modify format_date, but I want to do it safely.
Which character do I need to replace to safely 'print' raw HTML code from
I need to be able to send users a link that contains an encrypted
In a class, I need to define different actions which are passed into a
i need to convert pointers to long (SendMessage()) and i want to safely check
Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0
Need a map reduce function by mongo in php This my mongo structure [_id]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.