SQL Server Query

Handian Sudianto 4,836 Reputation points
2024-08-22T04:12:29.8833333+00:00

Hello,

I have dummy data below

DECLARE @TBL TABLE(col1 VARCHAR(100),col2 varchar(100))

INSERT INTO @TBL VALUES ('server-1','23658512')

INSERT INTO @TBL VALUES ('server-2','24658512')

INSERT INTO @TBL VALUES ('server-2','14658512')

the result from select * from @TBL

User's image

Now i want to add one column contain higher number of col2.

The higher number of all col2 is 24658512, so the expected result will be like :

User's image

need some help from expert here to modify the select query.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,670 questions
0 comments No comments
{count} votes

Accepted answer
  1. LiHongMSFT-4306 26,706 Reputation points
    2024-08-22T05:34:10.3533333+00:00

    Hi @Handian Sudianto

    Try this:

    SELECT *,MAX(col2)OVER(PARTITION BY NULL)AS COL3
    FROM @TBL
    --Or
    SELECT *
    FROM @TBL CROSS APPLY (SELECT MAX(col2)AS COL3 FROM @TBL)T
    

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


1 additional answer

Sort by: Most helpful
  1. Rodger Kong 0 Reputation points
    2024-08-22T07:04:20.78+00:00

    Use OVER clause to implement it, like this

    SELECT *,  CONVERT(VARCHAR(100) ,MAX(CONVERT(BIGINT, col2)) OVER()) AS col3 FROM @TBL
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.