Solved

Sorting on Decimal Value

Posted on 2003-11-19
7
435 Views
Last Modified: 2008-03-06
Have a look at this query :

Select       Id,  
      sum(Day0) As Day0,
      sum(Day0) As Day1,
      sum(Day0) As Day2,
      sum(Day0) As Day3,
      sum(Day0) As Day4,      
      sum(Total) As Total,
      Cast ( (sum(Day0) *100/ sum(Total)) as decimal(18,3)) As Schedule
From tblABCD
Group by  Id
Order by  Schedule DESC

Output which I get :
---------------------------------------------------
01234 58.45% 34.40% 6.99% 0.13% 0.03%
07729 58.35% 41.47% 0.00% 0.00% 0.18%
07774 58.93% 41.07% 0.00% 0.00% 0.00%
07602 58.99% 41.01% 0.00% 0.00% 0.00%
01042 58.86% 33.21% 1.38% 6.51% 0.05%
00148 58.13% 41.81% 0.00% 0.03% 0.03%
----------------------------------------------------

I have run this query in ASP PAGE and how output was this ?
If u look Percentage after IDS (58.45, 58.35, 58.93, 58.99, 58.86, 58.13) they r not sorted.

Can anyone pls help urgently.

0
Comment
Question by:radhakrishan1
  • 4
  • 2
7 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 9778366
Which columns need to be sorted? Do you want to sort only on 1 column or on multiple columns>

You are currently sorting by Schedule DESC - I don't see a schedule column in your results set (in fact, you have 7 columns in your select statement and 6 in your results set)

To sort by Day0, do this:

Select      Id,  
     sum(Day0) As Day0,
     sum(Day0) As Day1,
     sum(Day0) As Day2,
     sum(Day0) As Day3,
     sum(Day0) As Day4,    
     sum(Total) As Total,
     Cast ( (sum(Day0) *100/ sum(Total)) as decimal(18,3)) As Schedule
From tblABCD
Group by  Id
Order by  sum(Day0) DESC --Add multiple columns and order on which to sort here.

Cheers
Night
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 9778456
Select      Id,  
     sum(Day0) As Day0,
     sum(Day0) As Day1,
     sum(Day0) As Day2,
     sum(Day0) As Day3,
     sum(Day0) As Day4,    
     sum(Total) As Total,
     Cast ( (sum(Day0) *100/ sum(Total)) as decimal(18,3)) As Schedule
From tblABCD
Group by  Id
Order by Cast ( (sum(Day0) *100/ sum(Total)) as decimal(18,3)) DESC

Hilaire
0
 

Author Comment

by:radhakrishan1
ID: 9778466
I want to sort out on Schedule which is not column, the value is calculated by dividing values from sum(day0)/sum(total)

Again I don't use Schedule, to display in ASP page, Im just using to sorting it.

Can we sort on just on SCHEDULE.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 26

Expert Comment

by:Hilaire
ID: 9778478
A little bit of explanation

-you can NOT use a column alias to sort results
-you can use the column# of the resultset
eg order by 3 desc sorts on the third column descending
or you can repeat the column definition, as in my previous post

Order by Cast ( (sum(Day0) *100/ sum(Total)) as decimal(18,3)) DESC

HTH

Hilaire
0
 

Author Comment

by:radhakrishan1
ID: 9778500
Hi Hilaire,

Just tried what u said, but it doesn't sort on decimal places. Means all 97% will be together but it wont take in account the decimal places after the number
0
 
LVL 26

Accepted Solution

by:
Hilaire earned 100 total points
ID: 9778586
Do the decimal places appear in the schedules col ?

If so, use
"order by n desc"
where n is the place of the schedule col in your query

eg try this simplified version

Select      Id,  
     Cast ( (sum(Day0) *100/ sum(Total)) as decimal(18,3)) As Schedule
From tblABCD
Group by  Id
Order by  2 DESC

if the decimal places do not appear in the schedule col in QA,
you 'll have to change the query

(cast(sum(Day0) as decimal(18,3)) * 100.00 / cast(sum(Total)) as decimal(18,3))) as schedule
...
order by (cast(sum(Day0) as decimal(18,3)) * 100.00 / cast(sum(Total)) as decimal(18,3)))

Hilaire
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 9778616
Oops typo

(cast(sum(Day0) as decimal(18,3)) * 100.00 / cast(sum(Total) as decimal(18,3))) as schedule
...
order by (cast(sum(Day0) as decimal(18,3)) * 100.00 / cast(sum(Total) as decimal(18,3)))
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now