2015-09-04 156 views
0

回顾一些代码,我绊倒这个SQL代码,我不明白它在做什么。解释代码

INNER JOIN PPORDFIL_SQL 
    on dbo.att_Synergy_Absences.ItemCode = PPORDFIL_SQL.item_no 
    and PPORDFIL_SQL.id in 
     (Select top 1 p2.ID 
     from PPORDFIL_SQL as p2 
     where p2.item_no = PPORDFIL_SQL.item_no 
      and p2.ord_status = PPORDFIL_SQL.ord_status 
      and dbo.att_Synergy_Absences.syscreated <= ppordfil_sql.entered_dt 
     order by p2.id) 

据我的理解,假设无论最早的条目是什么,都会显示1条记录。

例如,同一个item_no有多个项目,但是订单号码不同,所以即使存在多个相同item_no的订单号,它也只会显示一个项目。

我试图运行代码,但它没有奏效。

对它做了什么或做了什么评论我实际上是否正确?大声笑

+1

这是一个伟大的地方开始得到答案。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –

+0

你的子查询按ID排序 - 也许应该按日期排序而不是? –

回答

0

你的两个连接条件互相干扰。

试试这样说:

INNER JOIN PPORDFIL_SQL on PPORDFIL_SQL.id = (
    Select top 1 p2.ID 
    from PPORDFIL_SQL as p2 
    where p2.item_no = dbo.att_Synergy_Absences.ItemCode 
    and p2.ord_status = PPORDFIL_SQL.ord_status 
    and dbo.att_Synergy_Absences.syscreated <= ppordfil_sql.entered_dt 
    order by p2.id 
)