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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:56:25+00:00 2026-06-17T15:56:25+00:00

I have a table with 3 columns, the data in column1 has repeating values

  • 0

I have a table with 3 columns, the data in column1 has repeating values and column 3 has totals, what I’d like to do is to return the top 2 totals for each value in column 1.

My query to create this table is below:

SELECT service,name, total
  FROM [test].[dbo].[TestTable]
  join test1.dbo.service
  on substring(servan,0,4)=servicebn
  where substring(servan,0,4)=servicebn and name <> testname
group by service,name,total
order by service,total desc

any help would be much appreciated

  • 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-17T15:56:26+00:00Added an answer on June 17, 2026 at 3:56 pm

    if you are using SQL Server 2005+, you can use Common Table Expression and Window Function.

    WITH recordsList
    AS
    (
        SELECT  service, name, total,
                DENSE_RANK() OVER (PARTITION BY service 
                                    ORDER BY total DESC) rn
        FROM    [test].[dbo].[TestTable]
                INNER join test1.dbo.servd
                    on substring(servan,0,4)=servicebn
        where   substring(servan,0,4) = servicebn and 
                name <> testname
    )
    SELECT  service, name, total
    FROM    recordsLIst
    WHERE   rn <= 2
    

    As a side note, this query has poor in performance because it requires FULL TABLE SCAN on every table. The reason is because of the join condition substring(servan,0,4)=servicebn. It doesn’t use index.

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

Sidebar

Related Questions

I have a table in my data base that has three columns: Screen, Icon,
I have a file that has columns that look like this: Column1,Column2,Column3,Column4,Column5,Column6 1,2,3,4,5,6 1,2,3,4,5,6
The table columns have the data type BLOB and CLOB. What are the corresponding
I have a table with 4 columns, LoginID, LastName, FirstName, Email. The data for
I have table in data base name train delay, with columns train number(int), DelayTime(int),
I have a data table with many rows and columns. How can I display
I have a fairly basic data.table in R, with 250k rows and 90 columns.
I have some data I am querying. The table is composed of two columns
I have a relatively large table (5,208,387 rows, 400mb data/670mb index), all columns i
If I have a table column with data and create an index on this

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.