Select and Join question?

How do I get this to work. I am getting an error on the 'RecordKey=h.SalesHeaderID', it does not like the h.SalesHeaderID.

select h.salesheaderid, h.ship_customer, h.DeliveryDate, x.oldvalue,x.newvalue,x.updatedate
from tblSalesHeader h left join (select top 1 * from Audit where RecordKey=h.SalesHeaderID order by updatedate desc) x on x.recordkey = h.salesheaderid
LVL 1
Jess31Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony PerkinsCommented:
Try it this way:

SELECT  h.salesheaderid,
        h.ship_customer,
        h.DeliveryDate,
        x.oldvalue,
        x.newvalue,
        x.updatedate
FROM    tblSalesHeader h
        LEFT JOIN (SELECT      TOP 1
                            oldvalue,
                                          newvalue,
                                          updatedate
                   FROM     Audit
                   -- WHERE    RecordKey = h.SalesHeaderID
                   ORDER BY updatedate DESC
                  ) x ON x.recordkey = h.salesheaderid
0
Anthony PerkinsCommented:
While my solution is syntactically correct, I suspect you are looking for something quite different using a CTE.
0
chinawalCommented:
Try this

select h.salesheaderid, h.ship_customer, h.DeliveryDate, x.oldvalue,x.newvalue,x.updatedate
from tblSalesHeader h
      left join
            (select top 1 a.*
             from Audit a
             inner join tblSalesHeader h
             on a.RecordKey = h.SalesHeaderID
             order by a.updatedate desc) x
 on x.recordkey = h.salesheaderid
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Anthony PerkinsCommented:
On second thoughts, I suspect this is what you want:
SELECT  h.salesheaderid,
        h.ship_customer,
        h.DeliveryDate,
        x.oldvalue,
        x.newvalue,
        x.updatedate
FROM    tblSalesHeader h
            LEFT JOIN (
                        SELECT      oldvalue,
                                    newvalue,
                                    updatedate
                        FROM      Audits a
                                    INNER JOIN (
                                          SELECT      MAX(updatedate) Lastupdatedate
                                          FROM      Audits
                                          GROUP BY
                                                      RecordKey) d ON a.updatedate = d.Lastupdatedate
                        ) x ON h.salesheaderid = x.recordkey
0
Jess31Author Commented:
For some reason non of these seem to work.
0
Anthony PerkinsCommented:
Fair enough.
0
SharathData EngineerCommented:
try this.
;with CTE as (
select salesheaderid, ship_customer, DeliveryDate, oldvalue,newvalue,updatedate,
       row_number() over (partition by x.SalesHeaderID order by x.updatedate desc) as rn
  from tblSalesHeader h
  left join Audit x on x.recordkey = h.salesheaderid)
select h.salesheaderid, h.ship_customer, h.DeliveryDate, x.oldvalue,x.newvalue,x.updatedate
  from CTE 
 where rn = 1

Open in new window

0
SharathData EngineerCommented:
sorry, this one.
;with CTE as (
select h.salesheaderid, h.ship_customer, h.DeliveryDate, x.oldvalue,x.newvalue,x.updatedate,
       row_number() over (partition by x.SalesHeaderID order by x.updatedate desc) as rn
  from tblSalesHeader h
  left join Audit x on x.recordkey = h.salesheaderid)
select salesheaderid, ship_customer, DeliveryDate, oldvalue,newvalue,updatedate
  from CTE 
 where rn = 1

Open in new window

0
Anthony PerkinsCommented:
Jess31,

A word of caution, before you choose to use ROW_NUMBER() over a simple derived table, I would urge you to read the following article:

Using row_number() vs. a correlated sub-select
http://sqlservernation.com/blogs/brandongalderisi/archive/2009/09/20/using-row-number-vs-a-correlated-sub-select.aspx
0
SharathData EngineerCommented:
try this if you do not want to use ROW_NUMBER
select h.salesheaderid, h.ship_customer, h.DeliveryDate, x.oldvalue,x.newvalue,x.updatedate,
       row_number() over (partition by x.SalesHeaderID order by x.updatedate desc) as rn
  from tblSalesHeader h
  left join (select t1.recordkey,t1.oldvalue,t1.newvalue,t1.updatedate 
               from Audit t1 
               join (select recordkey,max(updatedate) as updatedate from Audit group by recordkey) as t2 
                 on t1.recordkey = t2.recordkey and t1.updatedate = t2.updatedate) as x 
    on x.recordkey = h.salesheaderid

Open in new window

0
SharathData EngineerCommented:
This is another approach.
select h.salesheaderid, h.ship_customer, h.DeliveryDate, x.oldvalue,x.newvalue,x.updatedate,
       row_number() over (partition by x.SalesHeaderID order by x.updatedate desc) as rn
  from tblSalesHeader h
  left join (select t1.recordkey,t1.oldvalue,t1.newvalue,t1.updatedate 
               from Audit t1 
              where updatedate = (select max(updatedate) from Audit t2 where t1.recordkey = t2.recordkey)) as x 
    on x.recordkey = h.salesheaderid

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.