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


SORTING with ORDER by for a FLOAT field


SORTING with ORDER by for a FLOAT field

Author
Message
jcollins
jcollins
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 63
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
noeld
noeld
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6320 Visits: 2048
I don't quite understand how you determined that "order". Can you be more specific ?


* Noel
paulchernoch
paulchernoch
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 70
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



jcollins
jcollins
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 63
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
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