Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to get only one value from subquery

select users.name
from(
select users.name, pay.uid, 
RANK() OVER(ORDER BY count(pay.uid) DESC) AS ranking
from pay, users
where users.uid = pay.uid
group by users.uid)
where ranking = 1

i only want to get users.name output not name,uid,rank.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT sub.name
FROM (SELECT users.name, pay.uid, 
      RANK() OVER(ORDER BY count(pay.uid) DESC) AS ranking
      FROM pay
      INNER JOIN users
        on users.uid = pay.uid
      GROUP BY users.uid) sub
WHERE ranking = 1
  • alias the inline view (sub)
  • change alias on outer select to sub instead of pay
  • THe outer query has no knowledge of the tables inside thus the sub alias.
  • avoid using , for joins that’s a 1980s technique the newer standars are to use joins (inner, outer, left right, cross etc.)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading