SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Distinct Data but order by on Priority Column Which is not present in select result.


Distinct Data but order by on Priority Column Which is not present in select result.

Author
Message
Turbo_23
Turbo_23
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 21
Hi,

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

Scenario is three tables :-

Client Table
----------------
autClientId vchName
1-----------C1
2-----------C2
3-----------C3

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 :-
-------------------------
vchName
C3
C1
C2

Thanks in Advance.
bitbucket-25253
bitbucket-25253
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15503 Visits: 25280
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.

Ron

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

Before posting a performance problem please read
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61601 Visits: 17954
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 http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens 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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search