?
Solved

changing order of group result

Posted on 2010-08-19
15
Medium Priority
?
295 Views
Last Modified: 2012-06-21
I have a simple query I need to change the order of the results.

Select id from header group by id
returns
1
10
2
25

I need it to return

1
2
10
25

Thanks
0
Comment
Question by:mwootencnc1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +3
15 Comments
 
LVL 11

Expert Comment

by:rajvja
ID: 33473653
Select id from header group by id order by id
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 33473654
Select id from header group by id order by 1
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 33473657
1 being column number
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33473659
the field is varchar data type, you will need to either change the field's data type to numerical data type (for example, int),
or have the on-the-fly cast to int:

order by cast(yourfield as int)

this will fail if you have non-numerical data, in which case you will need a more sophisticated method ...
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33473665
If ID is using a [n][var]char data type, and if the entries can always be converted to a number:
Select id from header group by id order by CONVERT(int, id)

Open in new window

0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33473670
slow fingers :)
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 33473691
instead of order by CONVERT(int, id), you can simply use order by id*1
0
 

Author Comment

by:mwootencnc1
ID: 33474549
I tried the different variations of the Order by statement and none of them worked.  Maybe I was incorrect by saying it was a query, I am actually trying to edit a stored procedure I posted the code.  The problem is when it gets the assemblyid from the the tbl.MbAssemblyHeader the results are only sorted by the 1st number not the actual numeric value.  Sometimes the assemblyid could be alphanumeric.  This is my 1st attemp at something like this so I apologize if I'm not asking the right question.
From dbo.tblSoTransHeader h Inner join dbo.tblSoTransDetail d
		on h.TransId = d.TransId
		Where Coalesce(d.ReqShipDate, h.ReqShipDate) Between @DateFrom And @DateThru
		And h.CustId Between @CustFrom And @CustThru
		And d.ItemId Between @AssemblyFrom And @AssemblyThru
		And d.ItemId in (Select AssemblyId From dbo.tblMbAssemblyHeader group by AssemblyId )
		And (h.TransType = 9 or h.TransType = 3) --9;New;3;Backordered
		And (Select Count(*) From dbo.tblMpOrder o inner join dbo.tblMpOrderReleases r
			On o.OrderNo = r.OrderNo
			Where r.EstCompletionDate Between @DateFrom And @DateThru --44853
			And o.AssemblyId = d.ItemId And r.CustId = h.CustId And r.SalesOrder = h.TransId And isnull(r.PurchaseOrder, '') = isnull(h.CustPoNum, '') and r.Status <> 1) = 0 --pet 43454
		And (Select Count(*) From dbo.tblMpHistoryOrderReleases r  --pet 42494
			Where r.EstCompletionDate Between @DateFrom And @DateThru --44853
			And r.AssemblyId = d.ItemId And r.CustId = h.CustId And r.SalesOrder = h.TransId And isnull(r.PurchaseOrder, '') = isnull(h.CustPoNum, '')) = 0 --pet 43454 
		AND d.Status = 0

Open in new window

0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 33474572
Use *1 on all arguments you want to convert from char to int.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33474702
>Sometimes the assemblyid could be alphanumeric

please clarify with full data sample (with both numerical data and alphanumerical data) how the sorting should happen.

you might want to left-pad the numerical data so it's aligns right to ensure "requested" ordering:
http://www.developerdotstar.com/community/node/319

0
 

Author Comment

by:mwootencnc1
ID: 33475001
example data
11257
556
255888
2378
698745
mk5665
p644446
p858


should sort
556
2378
11257
255888
698745
mk5665
p858
p644446
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33475074
ok, so let's try
order by isnumeric(yourcol) desc
, case when isnumeric(yourcol) = 0 then yourcol 
  else right( '00000000000' + yourcol , 10 ) end

Open in new window

0
 

Author Comment

by:mwootencnc1
ID: 33475367
I get the following error when I modify the code from above and add your code

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
	Insert into #Orders (Type, OrderDate, ProdDate, SOTransId, CustId, PurchaseOrder, AssemblyId, LocId, UOMBase, Qty)
	Select 'C', h.TransDate, Coalesce(d.ReqShipDate, h.ReqShipDate) ReqShipDate, h.TransId, h.CustId, h.CustPONum, d.ItemId, d.LocId, d.UnitsBase 
		, Round((d.QtyOrdSell * Case When isnull(d.ConversionFactor, 0) = 0 Then 1 Else d.ConversionFactor End), @PrecQty) --pet 43453
		From dbo.tblSoTransHeader h Inner join dbo.tblSoTransDetail d
		on h.TransId = d.TransId
		Where Coalesce(d.ReqShipDate, h.ReqShipDate) Between @DateFrom And @DateThru
		And h.CustId Between @CustFrom And @CustThru
		And d.ItemId Between @AssemblyFrom And @AssemblyThru
		And d.ItemId in (Select AssemblyId From dbo.tblMbAssemblyHeader group by AssemblyId order by isnumeric(Assemblyid) desc
, case when isnumeric(assemblyid) = 0 then yourcol 
  else right( '00000000000' + yourcol , 10 ) end
 )
		And (h.TransType = 9 or h.TransType = 3) --9;New;3;Backordered
		And (Select Count(*) From dbo.tblMpOrder o inner join dbo.tblMpOrderReleases r
			On o.OrderNo = r.OrderNo
			Where r.EstCompletionDate Between @DateFrom And @DateThru --44853
			And o.AssemblyId = d.ItemId And r.CustId = h.CustId And r.SalesOrder = h.TransId And isnull(r.PurchaseOrder, '') = isnull(h.CustPoNum, '') and r.Status <> 1) = 0 --pet 43454
		And (Select Count(*) From dbo.tblMpHistoryOrderReleases r  --pet 42494
			Where r.EstCompletionDate Between @DateFrom And @DateThru --44853
			And r.AssemblyId = d.ItemId And r.CustId = h.CustId And r.SalesOrder = h.TransId And isnull(r.PurchaseOrder, '') = isnull(h.CustPoNum, '')) = 0 --pet 43454 
		AND d.Status = 0

	If @@Error <> 0 Set @RetVal = @@Error
	If @RetVal <> 0 Goto ErrorTrap
END
ELSE
BEGIN

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33475673
do you have a reason for the ORDER BY, as it's actually for a INSERT?
        Insert into #Orders (Type, OrderDate, ProdDate, SOTransId, CustId, PurchaseOrder, AssemblyId, LocId, UOMBase, Qty)
        Select TOP 100 PERCENT 'C', h.TransDate, Coalesce(d.ReqShipDate, h.ReqShipDate) ReqShipDate, h.TransId, h.CustId, h.CustPONum, d.ItemId, d.LocId, d.UnitsBase 
                , Round((d.QtyOrdSell * Case When isnull(d.ConversionFactor, 0) = 0 Then 1 Else d.ConversionFactor End), @PrecQty) --pet 43453
                From dbo.tblSoTransHeader h Inner join dbo.tblSoTransDetail d
                on h.TransId = d.TransId
                Where Coalesce(d.ReqShipDate, h.ReqShipDate) Between @DateFrom And @DateThru
                And h.CustId Between @CustFrom And @CustThru
                And d.ItemId Between @AssemblyFrom And @AssemblyThru
                And d.ItemId in (Select AssemblyId From dbo.tblMbAssemblyHeader group by AssemblyId order by isnumeric(Assemblyid) desc
, case when isnumeric(assemblyid) = 0 then yourcol 
  else right( '00000000000' + yourcol , 10 ) end
 )
                And (h.TransType = 9 or h.TransType = 3) --9;New;3;Backordered
                And (Select Count(*) From dbo.tblMpOrder o inner join dbo.tblMpOrderReleases r
                        On o.OrderNo = r.OrderNo
                        Where r.EstCompletionDate Between @DateFrom And @DateThru --44853
                        And o.AssemblyId = d.ItemId And r.CustId = h.CustId And r.SalesOrder = h.TransId And isnull(r.PurchaseOrder, '') = isnull(h.CustPoNum, '') and r.Status <> 1) = 0 --pet 43454
                And (Select Count(*) From dbo.tblMpHistoryOrderReleases r  --pet 42494
                        Where r.EstCompletionDate Between @DateFrom And @DateThru --44853
                        And r.AssemblyId = d.ItemId And r.CustId = h.CustId And r.SalesOrder = h.TransId And isnull(r.PurchaseOrder, '') = isnull(h.CustPoNum, '')) = 0 --pet 43454 
                AND d.Status = 0

        If @@Error <> 0 Set @RetVal = @@Error
        If @RetVal <> 0 Goto ErrorTrap
END
ELSE
BEGIN

Open in new window

0
 

Author Comment

by:mwootencnc1
ID: 33476085
You are correct I was able to get the code you provide working by using Select Top 100 percent but I think I applied to the wrong part of the procedure.  I am trying to look at the prodecure to see if I can use the same format further down.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

764 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