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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:33:04+00:00 2026-06-17T18:33:04+00:00

I am trying to get a return that gives me the top 80% of

  • 0

I am trying to get a return that gives me the top 80% of the values returned. Using SQL DB for i the TOP clause will not work. I have seen some examples of using Count() in a nested select statement, but I am not sure how that is supposed to fit into the query I have already written. I already have 2 sub-query’s so I need to find out how it would fit, or if it would work. Here is what I have so far:

Select  CATEGORY,
    LINE,
    ITEM#,
    Units
From    D*****.*****ST
Inner Join  (Select DW******.*****FO.ITEM,
                    Sum (SALES_UNITS) As Units,
                    CATEGORY
            From    DW*******.*****FO
            Inner Join  (Select CATEGORY,
                                DW****.******RY.ITEM
                         From   DW****.******RY
                         Where  CATEGORY='BRAKES') As CA***ST
            On DW*******.*****FO.ITEM=CA*****.***M              
            Where   ("DATE" between current date -1 years and current date) And (SALES > 5.00)
            Group By    DW*******.******O.ITEM,
                        CATEGORY) As Units_List
On      DW****.*****ST.**EM#=U*********.***M
Group By    CATEGORY,
            LINE,
            ITEM#,
            Units
Order By    Units DESC             

So somewhere in here would be the nested Count() clause I’m assuming, I’m just not sure where it fits in the grand scheme of things. I’m still learning some of the intermediate SQL stuff, so I’m sorry if the question seems a little simple.

  • 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-17T18:33:05+00:00Added an answer on June 17, 2026 at 6:33 pm

    I would suggest using window functions. I find your query hard to follow, but here is the idea:

    select t.*
    from (select t.*,
                 row_number() over (order by units desc) as seqnum,
                 count(*) over () as totnum
          from (<view that gets you all the data you want>
               ) t
         ) t
     where seqnum <= 0.8*totnum
    

    The idea is to use window functions to get the total count and also the ranking (I use row_number(), rank() might be more appropriate if you have ties). You can then just use a where clause to get the values you want.

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

Sidebar

Related Questions

I'm trying to get sphinx search to return excerpts of matched documents. I'm using
I'm trying to get the IsAdmin Value with SQL query(This query return 1 line
I'm trying to get my function to return the data it got into another
I'm trying to get an Excel function to return more arguments than are passed
I am trying to get a boolean method to return true or false on
I am trying to do 2 things: get a return value from a C
I am trying to get the below linq query to return -1 if there
i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
I'm trying to use $.post within a function, get the results, then return the
I'm trying to get the returned value from a java method, but it returns

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.