0

我在 SQL 中创建了一个查询。执行它时,它运行良好,除非我添加t4.CreatedOn要显示的。这是查询

select 
    t1.OrderNo, 
    t1.ProdLineNo, 
    t1.ProdNo, 
    t1.LotNo,
    t2.ProductNo,
    t3.LotNo,
    convert(double precision, t3.QntCons),
    convert(varchar, t4.CreatedOn, 20),
    convert(varchar, t6.CreatedOn, 105)

from table1 t1
    join table2 t2 on 
        t1.ID = t2.ID 
    join table3 t3 on 
        t2.ID = t3.ID
    join table4 t4 on 
        t4.OrderNo = t1.OrderNo 
        and 
        t4.SrcLotNo = t3.LotNo 
        and 
        t4.SrcProdNo = t2.ProdNo
    join table5 t5 on 
        t2.ProdNo = t5.ProdNo 
    join table6 t6 on 
        t3.LotNo = t6.LotNo and t6.ProdID = t5.ID

where t1.OrderNo = '1020' and t4.Context like 'allocate%' 

问题是,在使用上述参数执行后,当有 2 个或多个相同的产品时,LotNo每个产品都与相同的LotNo. 只有一个日期是正确的,而另一个应该只在正确的产品上。

为了更好地理解问题,这就是我得到的输出

OrderNo | ... | ProdNo | ... | LotNo | QntCons | t4.CreatedOn | ...
1020    | ... |   39   | ... | 48-02 |    12   |  2020-02-03  | ...
1020    | ... |   39   | ... | 48-02 |    12   |  2020-02-04  | ...
1020    | ... |   40   | ... | 48-02 |    15   |  2020-02-03  | ...
1020    | ... |   40   | ... | 48-02 |    15   |  2020-02-04  | ...
1020    | ... |   41   | ... | 22-15 |    10   |  2020-01-20  | ...

正确的输出应该是:

OrderNo | ... | ProdNo | ... | LotNo | QntCons | t4.CreatedOn | ...
1020    | ... |   39   | ... | 48-02 |    12   |  2020-02-04  | ...
1020    | ... |   40   | ... | 48-02 |    15   |  2020-02-03  | ...
1020    | ... |   41   | ... | 22-15 |    10   |  2020-01-20  | ...

我不知道这是因为我的表之间缺少关系造成的,还是有一个额外的关系或查询不正确?

4

0 回答 0