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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:09:06+00:00 2026-05-18T04:09:06+00:00

I have a simple problem , Although i believe its simple , am not

  • 0

I have a simple problem , Although i believe its simple , am not able to figure out the same.

Consider i have the below table with exactly same data as given below :

CREATE TABLE #temp
    (
    link varchar(255),
    number INT,
    fname varchar(255)            
    )

    insert into #temp VALUES ('abc',1,'f1')
    insert into #temp VALUES ('abc',2,'f2')
    insert into #temp VALUES ('abc',3,'f3')
    insert into #temp VALUES ('abc',4,'f6')
    insert into #temp VALUES ('abc',10,'f100')

    insert into #temp VALUES ('abe',-1,'f0')
    insert into #temp VALUES ('abe',1,'f1')
    insert into #temp VALUES ('abe',2,'f2')
    insert into #temp VALUES ('abe',3,'f3')
    insert into #temp VALUES ('abe',4,'f6')
    insert into #temp VALUES ('abe',20,'f200')

    insert into #temp VALUES ('cbe',-1,'f0')
    insert into #temp VALUES ('cbe',1,'f1')
    insert into #temp VALUES ('cbe',2,'f2')
    insert into #temp VALUES ('cbe',3,'f3')

Now for a given link , i need to get the max ‘number’ and the corresponding ‘fname’ which has the max ‘number’ for the given ‘link’.

1)Ex : if link is ‘abc’ , output should be
abc, 10, f100

2)Ex : if link if ‘abe’ , Output should be
abe, 20, f200

3)Now link can be also given as a pattern , like (link like ‘ab%’) , so output should be

abc, 10, f100

abe, 20, f200

4)if (link like ‘cb%’) , so output should be
cbe, 3, f3

Any help in writing this group by query. I have a solution using CAST and string concat like below , but that seems to be in-efficient.

select link,number,fname from #temp 
where link like 'ab%' and link+'_'+CAST(number AS varchar(255)) 
in (select link+'_'+CAST(MAX(number) AS varchar(255)) from #temp 
group by link)

Thanks..

  • 1 1 Answer
  • 2 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-05-18T04:09:07+00:00Added an answer on May 18, 2026 at 4:09 am

    Using a self join:

    SELECT x.link,
           x.number,
           x.fname
      FROM #temp x
      JOIN (SELECT t.link,
                   MAX(t.number) AS max_number
              FROM #temp t
          GROUP BY t.link) y ON y.link = x.link
                            AND y.max_number = x.number
    

    Using a CTE and ROW_NUMBER (SQL Server 2005+):

    WITH cte AS (
       SELECT x.link,
              x.number,
              x.fname,
              ROW_NUMBER() OVER(PARTITION BY x.link
                                    ORDER BY x.number DESC) rank
         FROM #temp x)
    SELECT c.link, 
           c.number,
           c.fname
      FROM cte c
     WHERE c.rank = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple problem that I have not been able to find an
I have a simple problem while putting text in multiple table! please have a
I have a simple problem but I am not sure how to solve it.
Consider this simple problem: Suppose I have a 1x4 array. I have to add
Although it seems like a simple problem, I have really come up empty on
I'm trying to send out a very simple ASIHTTPRequest with https. Although I have
I have a simple problem with SQL SERVER charindex function. DECLARE @VAR1 varchar SET
I have a simple problem that I've been stuck on for some time and
I have a simple problem that i cannot solve. I have a dictionary: aa
I have a simple problem but no matter what I try I can't see

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.