0

我试图加入两个表中没有共同元素的表,但现实生活中的关系很重要。我能够从两个表中获取数据,但它会产生行重复。输出如下:

Job    Release Date   Release Qty  Ship Date    Qty Shipped  
17760  2/15/13        320          2/14/13      320
17760  3/18/13        250          2/14/13      320
17760  4/11/13        180          2/14/13      320
17760  2/15/13        320          3/13/13      250
17760  3/18/13        250          3/13/13      250
17760  4/11/13        180          3/13/13      250
17760  2/15/13        320          4/08/13      180
17760  3/18/13        250          4/08/13      180
17760  4/11/13        180          4/08/13      180 

我的SQL语句如下:

SELECT PD.oqjob AS Job
 , [OR].ordate AS [Release Date]
 , [OR].orrqty AS [Release Qty]
 , PH.opsdte AS [Ship Date]
 , PD.oqaqty AS [Ship Qty]
FROM
  dbo.opakh PH
  LEFT OUTER JOIN dbo.opakd PD
    ON PH.oppack = PD.oqpack
  LEFT OUTER JOIN dbo.orels [OR]
    ON PD.oqord = [OR].orord AND PD.oqolin = [OR].orline
WHERE
  PD.oqjob = '17760'

我希望我的输出看起来像下面这样,发布日期打包日期都是从早到晚排序的。

Job      Release Date   Release Qty  Ship Date      Qty Shipped
17760    2/15/13 0:00   320.00       2/14/13 0:00   320.00
17760    3/18/13 0:00   250.00       3/13/13 0:00   250.00
17760    4/11/13 0:00   180.00       4/08/13 0:00   180.00

同样重要的是要注意,从OR表返回的行数可能与表返回的行数不同PH。任一表都可以返回更多行。

有没有办法做到这一点?

编辑:我已经重做我的 SQL 语句,所以没有显示任何额外的内容,也没有任何额外的表或数据

OR表包含三个发布日期,每个发布日期都有自己的数量

2/15/13 320
3/18/13 250
4/11/13 180

有三个不同的 Packer(发货日期),分别有一个 Packer Header 和 Packer DetailPHPD。我需要“PH”表中的日期和表中的数量PD。加入这两个表后,它看起来像这样:

2/14/13 320
3/13/13 250
4/08/13 180

然后我试图将PH&PD结果与OR结果结合起来。

4

3 回答 3

0

有点难以完全确定我们了解您的需求,这是我最好的尝试。

关键是引入一个来维护你的排序,并从表ROW_NUMBER()中获取DISTINCT记录。OR此外,您还需要分离结果并使用 aFULL OUTER JOIN将它们组合回来——我使用了 2 个公用表表达式来分离。

WITH CTE AS (
  SELECT PD.oqjob AS Job
   , PH.opsdte AS [Ship Date]
   , PD.oqaqty AS [Ship Qty]
   , ROW_NUMBER() OVER (PARTITION BY PD.oqjob ORDER BY PH.opsdte) rn
  FROM
    dbo.opakh PH
    LEFT OUTER JOIN dbo.opakd PD
      ON PH.oppack = PD.oqpack
  ), CTE2 AS (
  SELECT *, ROW_NUMBER() OVER (ORDER BY [Release Date]) rn
  FROM (
    SELECT DISTINCT PD.oqjob AS Job
     , [OR].ordate AS [Release Date]
     , [OR].orrqty AS [Release Qty]
    FROM
      dbo.opakh PH
      LEFT OUTER JOIN dbo.opakd PD
        ON PH.oppack = PD.oqpack
      LEFT OUTER JOIN dbo.orels [OR]
        ON PD.oqord = [OR].orord AND PD.oqolin = [OR].orline
    ) T
  ) 
SELECT CTE.Job, [Release Date], [Release Qty], [Ship Date], [Ship Qty]
FROM CTE 
  FULL OUTER JOIN CTE2 ON CTE.Job = CTE2.Job AND CTE.rn = CTE2.rn
WHERE CTE.Job = 17760 OR CTE2.Job = 17760
ORDER BY COALESCE(CTE.rn,1000000), COALESCE(CTE2.rn,1000000)

OR我在您的表格中添加了一条额外的记录以显示NULL回报。还要注意ORDER BY,我COALESCE在每个 rn 周围都使用了。只要确保您COALESCE不可能退回任何号码。

于 2013-05-27T03:39:14.410 回答
0
SELECT PD.oqjob AS Job
 , [OR].ordate AS [Release Date]
 , [OR].orrqty AS [Release Qty]
 , PH.opsdte AS [Ship Date]
 , PD.oqaqty AS [Ship Qty]
FROM
  dbo.opakh PH
  LEFT OUTER JOIN dbo.opakd PD
    ON PH.oppack = PD.oqpack
  LEFT OUTER JOIN dbo.orels [OR]
    ON PD.oqord = [OR].orord AND PD.oqolin = [OR].orline
WHERE
  PD.oqjob = '17760'
  AND [OR].orrqty = PD.oqaqty
  ORDER BY [Release Date], [Ship Date]

看起来像将 where orrqty 和 oqaqty 添加到 where 会给你想要的结果......

于 2013-05-27T02:59:00.647 回答
0

看起来您正在获得 Any to Any 连接,这表明您的查询没有加入正确的唯一键。

老实说,没有足够的信息来解释为什么会发生这种情况,但鉴于数量和日期(dbo.opakd 和 dbo.jhead)匹配,键控问题似乎与 dbo 的连接有关。奥莱斯表:

LEFT OUTER JOIN dbo.orels [or]
    ON pd.oqord = [or].orord AND pd.oqline = [or].orline
于 2013-05-27T01:22:48.160 回答