Home All Groups Group Topic Archive Search About
Author
17 Feb 2006 1:44 PM
Daviso
Hi.
I have a table with 2 fields (personalName, companyName).
I can insert person Names or company Names and one field tells me about what
kind of field is.

I want to retrieve the data, but ordered by person and company.
Is there a way to do that???
Thanks

Author
17 Feb 2006 1:51 PM
Aaron Bertrand [SQL Server MVP]
Can you give table structure, some sample data and desired results?

I can't really understand what "ordered by person and company" means.


Show quote
"Daviso" <dhernan***@alumno.uned.es> wrote in message
news:dt4jsk$db7$1@hefestos.uned.es...
> Hi.
> I have a table with 2 fields (personalName, companyName).
> I can insert person Names or company Names and one field tells me about
> what kind of field is.
>
> I want to retrieve the data, but ordered by person and company.
> Is there a way to do that???
> Thanks
>
Author
17 Feb 2006 2:22 PM
KBuser
SELECT personalName, companyName
FROM table_name
ORDER BY personalName,companyName
Author
20 Feb 2006 9:22 AM
Daviso
Here is part of the structure:

Pet_CIF varchar 15
Pet_Name varchar 50
Pet_Surname varchar 50
Pet_CompanyName varchar 200
Pet_Particular bit 1
Pet_PostalCode char 5

Pet_Particular indicate if I have a person or a company, so I can know what
kind of  is the data.

for example
(10000000, John, Smith, '', 1, 25000)    Its a Person
(10000001, John, Smith2, '', 1, 25000)  Its a Person
(10000002, John, Smith3, '', 1, 25000)  Its a Person
(10000003, John, Smith4, '', 1, 25000)  Its a Person
(10000004, '', '','TransWorld', 0, 25000)  Its a Company
(10000005, '', '','Aviation Service', 0, 25000)  Its a Company
(10000006, '', '','TransWorld', 0, 25000)  Its a Company
I want to order by name and surname so I have:
Aviation Service
John Smith
John Smith2
John Smith3
John Smith4

I hope it will be clear...
Thank for answer
Author
17 Feb 2006 2:38 PM
Jens
If you mean first ordered by PersonalName, then by Companyname this
should be:

ORDER BY PersonalName,Companyname

Keep in minde that this is ASC as default, the opposite ordering can be
achieved via:

DESC also combined with ASC

ORDER BY PersonalName ASC,Companyname DESC


HTH, Jens Suessmeyer.
Author
17 Feb 2006 4:03 PM
Michael Abraham
If you mean that there is a field which indicates which name field to use,
you can do something like

SELECT * FROM <table> ORDER BY CASE WHEN TypeOfName = 'Personal' THEN
personalName ELSE companyName END

HTH,

Mike
Show quote
"Daviso" <dhernan***@alumno.uned.es> wrote in message
news:dt4jsk$db7$1@hefestos.uned.es...
> Hi.
> I have a table with 2 fields (personalName, companyName).
> I can insert person Names or company Names and one field tells me about
> what kind of field is.
>
> I want to retrieve the data, but ordered by person and company.
> Is there a way to do that???
> Thanks
>
Author
20 Feb 2006 12:57 PM
Daviso
Hi.

This query works but only wiht one field, it would be great for me
something like
SELECT * FROM <table> ORDER BY CASE WHEN TypeOfName = 'Personal' THEN
personalName, personalSurname ELSE companyName END
but it give an error.....


Greetings


Show quote
"Michael Abraham" <mabraham36@newsgroup.nospam> escribió en el mensaje
news:uLsGsu9MGHA.3064@TK2MSFTNGP10.phx.gbl...
> If you mean that there is a field which indicates which name field to use,
> you can do something like
>
> SELECT * FROM <table> ORDER BY CASE WHEN TypeOfName = 'Personal' THEN
> personalName ELSE companyName END
>
> HTH,
>
> Mike
> "Daviso" <dhernan***@alumno.uned.es> wrote in message
> news:dt4jsk$db7$1@hefestos.uned.es...
>> Hi.
>> I have a table with 2 fields (personalName, companyName).
>> I can insert person Names or company Names and one field tells me about
>> what kind of field is.
>>
>> I want to retrieve the data, but ordered by person and company.
>> Is there a way to do that???
>> Thanks
>>
>
>

AddThis Social Bookmark Button