Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Help with distinct + sort order

 
   Database Help (Home) -> Programming RSS
Next:  Building a dynamic Select Statement Problem  
Author Message
Francis

External


Since: Jan 30, 2008
Posts: 2



(Msg. 1) Posted: Wed Jan 30, 2008 9:45 pm
Post subject: Help with distinct + sort order
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi

I've the following set of un-normalised records.
SortID Customer
1 ccc
2 ccc
3 aaa
4 aaa
5 bbb
6 bbb

I need to perform a select which will return distinct Customer name while
maintaining the sort order.
How can i achieve this using just Stored procedures?

Many thanks in advance!

Regards,
Francis

 >> Stay informed about: Help with distinct + sort order 
Back to top
Login to vote
Tom Moreau

External


Since: Apr 21, 2004
Posts: 502



(Msg. 2) Posted: Wed Jan 30, 2008 9:45 pm
Post subject: Re: Help with distinct + sort order [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Try:

select
min (SortID) SortID
, Customer
from
Customers
order by
SortID

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Francis" wrote in message

Hi

I've the following set of un-normalised records.
SortID Customer
1 ccc
2 ccc
3 aaa
4 aaa
5 bbb
6 bbb

I need to perform a select which will return distinct Customer name while
maintaining the sort order.
How can i achieve this using just Stored procedures?

Many thanks in advance!

Regards,
Francis

 >> Stay informed about: Help with distinct + sort order 
Back to top
Login to vote
David Portas

External


Since: Nov 11, 2003
Posts: 854



(Msg. 3) Posted: Wed Jan 30, 2008 9:45 pm
Post subject: Re: Help with distinct + sort order [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Francis" wrote in message

> Hi
>
> I've the following set of un-normalised records.
> SortID Customer
> 1 ccc
> 2 ccc
> 3 aaa
> 4 aaa
> 5 bbb
> 6 bbb
>
> I need to perform a select which will return distinct Customer name while
> maintaining the sort order.
> How can i achieve this using just Stored procedures?
>
> Many thanks in advance!
>
> Regards,
> Francis
>

Try the following. Depending on your data you could get a different sort
order if you use MAX(SortId) instead of MIN(SortId). That's not the case
with your sample but it could be relevant if you have other data. Whether
MIN or MAX is more appropriate may depend on what you mean by "maintaining
the sort order".

CREATE TABLE tbl (SortId INT NOT NULL PRIMARY KEY, Customer CHAR(3) NOT
NULL);

INSERT INTO tbl VALUES (1, 'ccc');
INSERT INTO tbl VALUES (2, 'ccc');
INSERT INTO tbl VALUES (3, 'aaa');
INSERT INTO tbl VALUES (4, 'aaa');
INSERT INTO tbl VALUES (5, 'bbb');
INSERT INTO tbl VALUES (6, 'bbb');

SELECT Customer
FROM tbl
GROUP BY Customer
ORDER BY MIN(SortId);


Hope this helps.

--
David Portas
 >> Stay informed about: Help with distinct + sort order 
Back to top
Login to vote
Plamen Ratchev

External


Since: Jan 10, 2008
Posts: 1007



(Msg. 4) Posted: Wed Jan 30, 2008 9:45 pm
Post subject: Re: Help with distinct + sort order [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Here is one way:

SELECT Customer
FROM Customers
GROUP BY Customer
ORDER BY MIN(SortId)

HTH,

Plamen Ratchev
http://www.SQLStudio.com
 >> Stay informed about: Help with distinct + sort order 
Back to top
Login to vote
Tom Moreau

External


Since: Apr 21, 2004
Posts: 502



(Msg. 5) Posted: Wed Jan 30, 2008 9:45 pm
Post subject: Re: Help with distinct + sort order [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Oops, typo:

select
min (SortID) SortID
, Customer
from
Customers
group by
Customer
order by
SortID


--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Tom Moreau" wrote in message

Try:

select
min (SortID) SortID
, Customer
from
Customers
order by
SortID

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Francis" wrote in message

Hi

I've the following set of un-normalised records.
SortID Customer
1 ccc
2 ccc
3 aaa
4 aaa
5 bbb
6 bbb

I need to perform a select which will return distinct Customer name while
maintaining the sort order.
How can i achieve this using just Stored procedures?

Many thanks in advance!

Regards,
Francis
 >> Stay informed about: Help with distinct + sort order 
Back to top
Login to vote
Francis

External


Since: Jan 30, 2008
Posts: 2



(Msg. 6) Posted: Wed Jan 30, 2008 10:29 pm
Post subject: Re: Help with distinct + sort order [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi all

Thanks very much for the help!
Works like magic.

Regards,
Francis

"Plamen Ratchev" wrote in message

> Here is one way:
>
> SELECT Customer
> FROM Customers
> GROUP BY Customer
> ORDER BY MIN(SortId)
>
> HTH,
>
> Plamen Ratchev
> http://www.SQLStudio.com
 >> Stay informed about: Help with distinct + sort order 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
select distinct and order by w/ case - Can anyone tell me why this fails in Northwind? SELECT DISTINCT CustomerID, ContactName FROM Customers ORDER BY CASE WHEN 1 = 1 THEN CustomerID END ASC, CASE WHEN 0 = 1 THEN ContactName END ASC I get the old "ORDER BY items must appear in the sele...

Sort order and view - HI all, Using SQL 2005, I create a simple view Select * from employee order by name But when I select the view with select * from employeeview the sort order is not adheered to. I have to specificcally issue a order by on the view Can someone explain...

Sort order in SQL 2005 - In a 2005 database table function that has SELECT TOP 100 Percent .... ORDER BY TextField This displays it in the primary key (numeric field) order and not the specified. Is there a setting change that is required to make the function return the order as...

Sort order in SQL 2005? - Hi, When sorting a numeric field in SQL 2005, I am finding that it is sorting it differently than it was in 2000. i.e. 1, 10, 1000, 1001, 1002, 2, 20, 2000, 2002. Is there a reason for this? Appreciate the help. Damon

DISTINCT - Hi SELECT [column],[column],[column],[column],[column],DISTINCT([column]) FROM table The above query fails, I'm guessing because the DISTINCT needs to be at the start. I do a INSERT INTO <table> SELECT... with the data later on so I'd rather not...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]