Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Distinct Data but order by on Priority Column Which is not present in select result. Expand / Collapse
Posted Wednesday, December 26, 2012 5:27 AM


Group: General Forum Members
Last Login: Monday, September 9, 2013 4:29 AM
Points: 18, Visits: 21

Can you please help me how to get result in following format :-

Scenario is three tables :-

Client Table
autClientId vchName

Order Table :- Each Client has multiple Order
autOrderId intClientId vchOrderName
1-----------1----------- O1
2-----------1----------- O2
3-----------2----------- O3
5-----------2----------- O4
7-----------3----------- O5
8-----------3----------- O6

OrderInformation Table :- Each Order has multiple Information
autOrderXmlId intOrderId vchOrderXmlName intPriority
1----------- 1----------- OX1----------- 7
2----------- 1----------- OX2----------- 1
3----------- 2----------- OX3----------- 9
4----------- 2----------- OX4----------- 3
5----------- 3----------- OX5----------- 4
6----------- 5----------- OX6----------- 2
7----------- 7----------- OX7----------- 5
8----------- 7----------- OX8----------- 8
9----------- 8----------- OX9----------- 0

I want distinct clients on the basis of order information priority.

Result Should be Like this :-

Thanks in Advance.
Post #1400213
Posted Wednesday, December 26, 2012 9:19 AM



Group: General Forum Members
Last Login: Sunday, January 4, 2015 7:55 AM
Points: 5,333, Visits: 25,280
In order to receive a rapid answer to your question, it is beholden on you to present your table definition(s), sample data and required answer(s), in a readily consumable format. In other words you should help those who can and will help you.

To do this please click on the first link in my signature block ... the article that will be displayed contains T-SQL statements and instructions in their use ... to make it easy and quick for you to help those who wish to help you.

If everything seems to be going well, you have obviously overlooked something.


Please help us, help you -before posting a question please read

Before posting a performance problem please read
Post #1400275
Posted Wednesday, December 26, 2012 9:46 AM



Group: General Forum Members
Last Login: Today @ 8:42 AM
Points: 16,138, Visits: 16,842
This seems to be a representation of what you are looking for.

create table #Client
ClientID int identity,
Name char(2)

insert #Client
select 'C1' union all
select 'C2' union all
select 'C3'

create table #Orders
OrderID int identity,
ClientID int,
OrderName char(2)

insert #Orders
select 1, 'O1' union all
select 1, 'O2' union all
select 2, 'O3' union all
select 2, 'O4' union all
select 3, 'O5' union all
select 3, 'O6'

create table #OrderInfo
OrderInfoID int identity,
OrderID int,
Priority int

insert #OrderInfo
select 1, 7 union all
select 1, 1 union all
select 2, 9 union all
select 2, 3 union all
select 3, 4 union all
select 5, 2 union all
select 7, 5 union all
select 7, 8 union all
select 8, 0

select c.Name
from #OrderInfo oi
join #Orders o on oi.OrderID = o.OrderID
join #Client c on c.ClientID = o.ClientID
group by c.Name
order by max(oi.Priority)

drop table #Client
drop table #Orders
drop table #OrderInfo

Notice how I posted this in a consumable format?

I would HIGHLY recommend you change your naming conventions. You should not use a datatype prefix on your columns. I am assuming that things like "autClientId" and "vchName" mean "AutoNumberClientID" and "VarcharName". Not only does that make it difficult to find your columns, it will cause you untold pain when you have to change a datatype. You will end up with either LOTS of code to rework or prefixes that don't match anymore. Just don't it at all. It may seem like a good idea at first but you will soon realize how horrible that really is.


Need help? Help us help you.

Read the article at for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1400287
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse