SORTING with ORDER by for a FLOAT field

  • select top 5 left(convert(varchar(10),EffectiveDate,101),10) [Date]

    , convert(varchar,cast(NetMarketValue as money),1) [NetMarketValue]

    , convert(varchar,cast(NetGainLoss as money),1) [NetGainLoss]

    FROM Investment.Portfolio.vForwardContractExposure

    WHERE (EffectiveDate IN (SELECT MAX(EffectiveDate) FROM Investment.Portfolio.vForwardContractHolding))

    AND (Broker = ' ')

    AND (CustomGroup = ' ')

    AND (SourceAccount = ' ')

    AND (SourceAccountId = ' ')

    ORDER BY NetGainLoss desc

    THE field NetGainLoss is defined as a float field within the database table 'Investment.Portfolio.vForwardContractExposure'

    What tricks if any would return the correct results for example with whole numbers 1, 55, 9

    it would return results in this order ...

    9

    55

    1


    jcollins

  • I don't quite understand how you determined that "order". Can you be more specific ?


    * Noel

  • Your problem is that you converted the number to a string, which sorts alphabetically, not numerically.

    I would use a different name for the alias of the sort column so you can return the nicely formatted value,

    then sort by the underlying numerical column. (Your alias is hiding the underlying column from the scope of the order by, I think.)

    select top 5 left(convert(varchar(10),EffectiveDate,101),10) [Date]

    , convert(varchar,cast(NetMarketValue as money),1) [NetMarketValue]

    , convert(varchar,cast(NetGainLoss as money),1) [NetGainLossFormatted]

    FROM Investment.Portfolio.vForwardContractExposure

    WHERE (EffectiveDate IN (SELECT MAX(EffectiveDate) FROM Investment.Portfolio.vForwardContractHolding))

    AND (Broker = ' ')

    AND (CustomGroup = ' ')

    AND (SourceAccount = ' ')

    AND (SourceAccountId = ' ')

    ORDER BY NetGainLoss desc

    - Paul

  • Thanks Everyone,

    Paul is correct I figured this out shortly after the posting.

    After renaming the Alias and sorting by the actual database field

    the sort order works fine.

    Thanks again,

    Jeff


    jcollins

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply