Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

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
0
Jess31
Asked:
Jess31
1 Solution
 
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now