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

  • Hi,

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

    Scenario is three tables :-

    Client Table

    ----------------

    autClientIdvchName

    1-----------C1

    2-----------C2

    3-----------C3

    Order Table :- Each Client has multiple Order

    ----------------

    autOrderIdintClientIdvchOrderName

    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.

  • 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[/url]
    Before posting a performance problem please read[/url]

  • 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply