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

  • Home
  • SEARCH
  • 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 8245261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:07:33+00:00 2026-06-07T22:07:33+00:00

Column1 Column2 Column3 ——- ——- ——- jim 1788 5F jim 2000 9F jim 500

  • 0
Column1 Column2 Column3
------- ------- -------
jim       1788    5F 
jim       2000    9F
jim       500     9F
ben       190     4H
matt      400     46
matt      20      3G

I need to run a query that outputs:

Column1 MaxValue PL
------- ------- -------
jim       2000    9F
jim       2000    NULL
ben       190     4H
matt      400     46
matt      400     NULL

For each value in Column1 (e.g. jim, ben, matt): we group the data by Column1 and for each group we display the row that has the maximum value on column2.
Then, for each row found in this manner, it displays it again but with NULL in column3, if the groupping by Column1 returns more than 1 row and there are smaller values in Column2 than the maximum found number in the previous step.
ben 190 NULL is not displayed because we have ben only once on Column1.

Thank you in advance for any tips or suggestions.

This is what I tried so far but I receive an error prompting me to include Column2 and Column3 in the GROUP By clause, but if I do so I don’t reach the desired output as shown above.

CREATE VIEW VIEWB AS 
   SELECT DISTINCT t1.Column1, 
          /* MAX_Value */
            (MAX(t1.[Column2])) AS [MAX Value], 
          /* PL */
            (CASE 
            WHEN t1.[Column2] = MAX(t1.[Column2]) THEN t1.[Column3]
            ELSE NULL
            END) AS PL
      FROM TABLEA AS t1
      GROUP BY t1.Column1;
  • 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-07T22:07:34+00:00Added an answer on June 7, 2026 at 10:07 pm

    try this

    ;with cte as (select *,ROW_NUMBER() over(partition by Column1 
    order by Column2 desc) as row_num from table_A),
    a as (
    select Column1,MAX(Column2) [Column2],null [row_num]
    from table_A
    group by Column1
    having COUNT(*)>1),
    b as (select Column1,Column2,Column3 from cte where row_num=1
    union all
    select * from a)
    select  Column1,Column2 [MaxValue],Column3 [PL] from b
    order by Column2 desc,Column1,ISNULL(Column3,'') desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that looks like SELECT P.Column1, P.Column2, P.Column3, ... ( SELECT
I have a table say table1 which has 3 columns column1, column2 and column3
i have table structure with 3 colums (column1, column2, column3) and i want to
I have some text in the following format: column1 DATE, column2 VARCHAR2(6), column3 VARCHAR2(15)
For a list like: Column1 Column2 Column3 DataA 1 1234 DataA 2 4678 DataA
I have a huge tab-separated file formatted like this X column1 column2 column3 row1
here is my csv column1,column2,column3,column4,column5 column1_row1,column2_row1,column3_row1,column4_row1,column5_row1 column1_row2,column2_row2,column3_row2,column4_row2,column5_row2 column1_row3,column2_row3,column3_row3,column4_row3,column5_row3 column1_row4,column2_row4,column3_row4,column4_row4,column5_row4 column1_row5,column2_row5,column3_row5,column4_row5,column5_row5 column1_row6,column2_row6,column3_row6,column4_row6,column5_row6 column1_row7,column2_row7,column3_row7,column4_row7,column5_row7 column1_row8,column2_row8,column3_row8,column4_row8,column5_row8 column1_row9,column2_row9,column3_row9,column4_row9,column5_row9
I have a sql statement like SELECT column1, column2,column3, column4 FROM table1 GROUP BY
I have a table like this: ID Name Column1 Column2 Column3 1 ABC 202.2
I have a DataGridView with 6 columns. Example: column1 column2 column3 column4 column5 column6

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.