• 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St