[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?

TSQL Order by Group then subCompany

Posted on 2017-12-07
2
Medium Priority
?
24 Views
Last Modified: 2017-12-11
Hello,
Trying to create a selection in a specific order.
The table is like this

CompanyName , CompanyStatus, CompanyNumber, GroupNumber
Blackwell , group, A123G, A123
Symphony, subCompany, s678, A123
Blends, subCompany, t123, A123
Tiger, group, T123G, T123
Pepper, group, Z987G, Z987
Great Falls, subCompany, f545, T123
~~~~~~~~~~~~~

What I would like the result to be based on Alpha for the group names, then Alpha for their sub companies

CompanyName , CompanyStatus, CompanyNumber, GroupNumber
Blackwell group, A123G, A123
Blends, subCompany, t123, A123
Symphony subCompany, s678, A123
Pepper group, Z987G, Z987
Great Falls, subCompany, f545, T123
Tiger group, T123G, T123

Any ideas?
Thx
JS
0
Comment
Question by:jshesek
2 Comments
 
LVL 37

Expert Comment

by:Pawan Kumar
Please try this -

Pepper group, Z987G, Z987 -> This records will come at the last since you need order by on the GroupNumber column...

/*------------------------
SELECT * FROM ORDERS
ORDER BY GroupNumber,CompanyName,CompanyStatus
------------------------*/
CompanyName                     CompanyStatus         CompanyNumber       GroupNumber
------------------------------- --------------------- ------------------- -----------------
Blackwell                       group                 A123G               A123
Blends                          subCompany            t123                A123
Symphony                        subCompany            s678                A123
Great Falls                     subCompany            f545                T123
Tiger                           group                 T123G               T123
Pepper                          group                 Z987G               Z987

(6 row(s) affected)

Open in new window

0
 
LVL 54

Expert Comment

by:Ryan Chong
What I would like the result to be based on Alpha for the group names, then Alpha for their sub companies

CompanyName , CompanyStatus, CompanyNumber, GroupNumber
Blackwell group, A123G, A123
Blends, subCompany, t123, A123
Symphony subCompany, s678, A123
Pepper group, Z987G, Z987
Great Falls, subCompany, f545, T123
Tiger group, T123G, T123

there is no "group name" in your table/select statement above

assuming there is, you should try:
ORDER BY GroupName, GroupNumber, CompanyName, CompanyStatus

Open in new window


or:

ORDER BY GroupName, GroupNumber, CompanyStatus, CompanyName

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

867 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