Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with Max function


Help with Max function

Author
Message
Sql Student-446896
Sql Student-446896
Valued Member
Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)

Group: General Forum Members
Points: 71 Visits: 201
I want to get the 10.1 and 10.2 in my output. How do i get that?

Table A has the columns change_id

Change_id
-----------
1.1
1.2
2.1
2.2
3.1
3.2
10.1
10.2
HanShi
HanShi
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3051 Visits: 3633
The MAX statement will only return one single value. You can get the desired results using the TOP clause:
SELECT TOP 2
change_id
FROM table_a
ORDER BY change_id DESC



** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
mickyT
mickyT
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1253 Visits: 3309
Hi

You could also try (assuming this is a numeric/float column)
with sampledata as (
select v
from (VALUES (1.1), (1.2), (2.1), (2.2), (10.1), (10.2), (10.3)) v (v)
)
select v
from sampledata
where cast(v as int) = (SELECT MAX(cast(v as int)) FROM sampledata);


dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4279 Visits: 6431
Sql Student-446896 (8/22/2013)
I want to get the 10.1 and 10.2 in my output. How do i get that?

Table A has the columns change_id

Change_id
-----------
1.1
1.2
2.1
2.2
3.1
3.2
10.1
10.2


There's about a thousand ways in SQL to get those 2 rows out of the sample data. But you haven't given enough information about the business rule to help us help you:

1. I want all the values in the result set whose change_id is > 10.
2. I want the 2 largest change_id values.
3. I want only change_id values where the number of digits to the left of the decimal point are 2.
4. I want the 2 highest ranked changes, which may contain more than 2 rows in the case of a tie.
5. I want the rows where the change_id sums to 20.3.

These are just a few examples of how the clarity of the business rule could affect the final query you're going to deliver.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45329 Visits: 39936
dwain.c (8/22/2013)
Sql Student-446896 (8/22/2013)
I want to get the 10.1 and 10.2 in my output. How do i get that?

Table A has the columns change_id

Change_id
-----------
1.1
1.2
2.1
2.2
3.1
3.2
10.1
10.2


There's about a thousand ways in SQL to get those 2 rows out of the sample data. But you haven't given enough information about the business rule to help us help you:

1. I want all the values in the result set whose change_id is > 10.
2. I want the 2 largest change_id values.
3. I want only change_id values where the number of digits to the left of the decimal point are 2.
4. I want the 2 highest ranked changes, which may contain more than 2 rows in the case of a tie.
5. I want the rows where the change_id sums to 20.3.

These are just a few examples of how the clarity of the business rule could affect the final query you're going to deliver.


BWAAA-HAAAA!!! Good guesses, Dwain, but it's painfully obvious that the OP wanted the two numbers whose sum is the cube root of 8365.427 which has only decending number pairs from left to right with the last digit forming a single-digit "pair", has no duplicate digits, and has all possible digits greater than 0 except for 1 and 9. :-P

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4279 Visits: 6431
Jeff Moden (8/22/2013)
dwain.c (8/22/2013)
Sql Student-446896 (8/22/2013)
I want to get the 10.1 and 10.2 in my output. How do i get that?

Table A has the columns change_id

Change_id
-----------
1.1
1.2
2.1
2.2
3.1
3.2
10.1
10.2


There's about a thousand ways in SQL to get those 2 rows out of the sample data. But you haven't given enough information about the business rule to help us help you:

1. I want all the values in the result set whose change_id is > 10.
2. I want the 2 largest change_id values.
3. I want only change_id values where the number of digits to the left of the decimal point are 2.
4. I want the 2 highest ranked changes, which may contain more than 2 rows in the case of a tie.
5. I want the rows where the change_id sums to 20.3.

These are just a few examples of how the clarity of the business rule could affect the final query you're going to deliver.


BWAAA-HAAAA!!! Good guesses, Dwain, but it's painfully obvious that the OP wanted the two numbers whose sum is the cube root of 8365.427 which has only decending number pairs from left to right with the last digit forming a single-digit "pair", has no duplicate digits, and has all possible digits greater than 0 except for 1 and 9. :-P


I think I'm glad you're not one of my business users if you can come up with a diabolical requirement like that one. :-P


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search