Sample Header Ad - 728x90

How to fetch related rows on multiple condition with ranking

2 votes
1 answer
272 views
I have following table for storing item information in PostgreSQL. id | name | category | seller | m_age | status | type_id | region | -------------------------------------------------------------------------- 10 | TBox | Gun | 14 | 4 | Q4 | 12 | EU | 11 | Globe | Gun | 36 | 12 | Q1 | 17 | EU | 12 | Nike | CFK | 12 | 5 | Q2 | 12 | AS | 32 | Raven | Knife | 21 | 14 | Q1 | 12 | EU | 46 | TBox | Gun | 14 | 10 | Q4 | 12 | AS | 46 | Blaster | Gun | 14 | 10 | Q4 | 12 | EU | 78 | PaintGun | Gun | 12 | 10 | Q2 | 54 | US | 98 | TBox | OD | 21 | 4 | Q3 | 54 | AS | I want to find related items on basic of certain criteria. Category > title > Tech Info Matched(m_age, status, type_id, region) > Seller So for id 10 - TBox item rank should be following(name and category column value and Info matched and reason cololms are for visual reference only) id | name | category | INFO MATCHED | RANK | REASON | -------------------------------------------------------------------------------- 10 | TBox | Gun | - | - | - | -------------------------------------------------------------------------------- 11 | Diff | Same | 1 | 3 | same cat diff name 1 info match | 12 | Diff | Diff | 1 | 6 | diff cat diff name 1 info match | 32 | Diff | Diff | 2 | 7 | diff cat diff name 2 info match | 46 | Same | Same | 3 | 1 | same cat and name | 46 | Diff | Same | 4 | 2 | same cat diff name 4 info match | 78 | Diff | Same | 0 | 4 | same cat diff name 0 info match | 98 | Same | Diff | 1 | 5 | diff cat same name | I am still a novice to SQL. I have tried with normal CASE and function. but haven't got proper results. Rank will not work with this many condition. Is there any way to pull this results anyhow with PostgreSQL?
Asked by Rajan M (21 rep)
Apr 1, 2020, 06:33 AM
Last activity: May 14, 2025, 12:04 AM