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 8004169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:50:48+00:00 2026-06-04T16:50:48+00:00

I have a query: select a.kli, b.term_desc, count(distinct(a.adic)) as count, a.partner_id from ad_delivery.sgmt_kli_adic a

  • 0

I have a query:

select
    a.kli,
    b.term_desc,
    count(distinct(a.adic)) as count,
    a.partner_id

from
    ad_delivery.sgmt_kli_adic a
    join wand.wandterms b on a.kli = b.term_code
    join wand.wandterms c on b.term_desc=c.term_desc
    join dwh.sgmt_clients e on a.partner_id::varchar = e.partner_id
    join dwh.schema_names f on e.partner_id::integer = f.partner_id::integer
where
    a.partner_id::integer in (f.partner_id)
    and c.class_code = 969
group by a.partner_id, b.term_desc, a.kli
order by partner_id, count desc;

which brings back counts for certain terms per partner_id. I want to be able to show the top 10 for each of the ~40 partner_id in order by the count desc

the query results look like

db=# SELECT * FROM xxx;
 pid | term_desc | count
----+------------+------
  4 | termdesc1  | 3434
  4 | termdesc2  | 235
  4 | termdesc3  | 367
  4 | termdesc4  | 4533
  5 | termdesc1  | 235
  5 | termdesc2  | 567
  5 | termdesc3  | 344
  5 | termdesc4  | 56
(10k+ rows)
  • 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-06-04T16:50:49+00:00Added an answer on June 4, 2026 at 4:50 pm

    You could add a rank column and then filter the result by the rank :

    select
        a.kli,
        b.term_desc,
        count(distinct(a.adic)) as count,
        a.partner_id,
        RANK() OVER (PARTITION BY a.partner_id order by a.partner_id DESC) AS r
    from
        ad_delivery.sgmt_kli_adic a
        join wand.wandterms b on a.kli = b.term_code
        join wand.wandterms c on b.term_desc=c.term_desc
        join dwh.sgmt_clients e on a.partner_id::varchar = e.partner_id
        join dwh.schema_names f on e.partner_id::integer = f.partner_id::integer
    where
        a.partner_id::integer in (f.partner_id)
        and c.class_code = 969
    group by a.partner_id, b.term_desc, a.kli
    HAVING r < 11
    order by partner_id, count desc;
    

    I have not tested the code, however the trick is ranking the each row of the GROUP BY and filter the resultset with the HAVING clause, keeping only item with a lower rank than 11 (you will get 10 item per group).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query: SELECT DISTINCT IM.EDIFICIOS_ID, TI.TITULAR FROM IMPORTACION IM INNER JOIN I_EDIFICIO
I have query SELECT helpc.cid, COUNT(help.cid) AS children_count FROM air_help_category AS helpc LEFT JOIN
I have this query: SELECT DISTINCT(b.friend_id) AS possible_id FROM friends_friends a JOIN friends_friends b
I have a query: SELECT DISTINCT countryName,countrySlug FROM countries AS Country INNER JOIN countries_networks
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have this query: SELECT * FROM sample INNER JOIN test ON sample.sample_number =
I have this query: SELECT COUNT(articles.id) AS count FROM articles, xml_documents, streams WHERE articles.xml_document_id
I have this query: SELECT g.title, g.asin, g.platform_id, r.rank FROM games g INNER JOIN
I have this query SELECT articles.*, users.username AS `user` FROM `articles` LEFT JOIN `users`
I have this query: SELECT DISTINCT phone, department FROM `users` WHERE ((SUBSTRING(phone,1,3) = '399')

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.