需要来自一个表的记录,但需要来自另一个表的列

时间:2016-10-19 13:06:29

标签: sql ms-access

我正在尝试创建一个查询,该查询将返回一个只包含一个表Current Open Orders Today的所有行的表,但是从yesterday返回每个记录的发货日期Open Orders From YesterdayCurrent Open Orders Today表中。我不想要Open Orders From Yesterday的任何多余记录。但是,由于我的加入或其他原因,它现在正在返回错误的行数。

有关如何仅从Current Open Orders Today表格返回记录但仍然从yesterday返回Open Orders From Yesterday的发货日期的任何建议,我们将不胜感激!

以下是我的查询的SQL代码:

SELECT [Current Open Orders Today].[Customer Code], 
       [Current Open Orders Today].[Customer Name], 
       [Current Open Orders Today].[Sales Order Number], 
       [Current Open Orders Today].[Input By], 
       [Current Open Orders Today].[Customer PO Number], 
       [Current Open Orders Today].[Date Received], 
       [Current Open Orders Today].[Date Entered], 
       [Current Open Orders Today].[Date Required], 
       [Current Open Orders Today].[Date Promised], 
       [Current Open Orders Today].[Ship Date as of Today],
       [Open Orders From Yesterday].[Ship Date] AS [Ship Date from Yesterday], 
       [Current Open Orders Today].WAREHOUSE, 
       [Current Open Orders Today].Product, 
       [Current Open Orders Today].Alpha, 
       [Current Open Orders Today].Description, 
       [Current Open Orders Today].[Long Description], 
       [Current Open Orders Today].[Order Quantity], 
       [Current Open Orders Today].[Order Value], 
       [Current Open Orders Today].[Customer Analysis], 
       [Current Open Orders Today].Territory, 
       [Current Open Orders Today].Engineer, 
       [Current Open Orders Today].[Analysis Code 5], 
       [Current Open Orders Today].[Industrial Class], 
       [Current Open Orders Today].Region, 
       [Current Open Orders Today].Status
FROM [Current Open Orders Today] 
INNER JOIN [Open Orders From Yesterday] 
   ON ([Current Open Orders Today].[Sales Order Number] = [Open Orders From Yesterday].[Sales Order Number]) 
  AND ([Current Open Orders Today].Product = [Open Orders From Yesterday].Product);

3 个答案:

答案 0 :(得分:0)

您今天所有订单都加入了所有订单。

在没有连接部分的情况下构建命令

select
    today.*
    , (select top 1 yesterday.[ship_date] 
        from [yesterday open orders] 
        where yesterday.order number = today.order number 
        and yesterday.product code = today.product code)
from
    [current open orders today]

答案 1 :(得分:0)

尝试LEFT JOIN,这样它就会从[Current Open Orders Today]中选择所有行,并且仅匹配来自[Open Orders From Yesterday]的行。

SELECT C.[Customer Code], 
       C.[Customer Name], 
       C.[Sales Order Number], 
       C.[Input By], 
       C.[Customer PO Number], 
       C.[Date Received], 
       C.[Date Entered], 
       C.[Date Required], 
       C.[Date Promised], 
       C.[Ship Date as of Today],
       O.[Ship Date] AS [Ship Date from Yesterday], 
       C.WAREHOUSE, 
       C.Product, 
       C.Alpha, 
       C.Description, 
       C.[Long Description], 
       C.[Order Quantity], 
       C.[Order Value], 
       C.[Customer Analysis], 
       C.Territory, 
       C.Engineer, 
       C.[Analysis Code 5], 
       C.[Industrial Class], 
       C.Region, 
       C.Status
FROM [Current Open Orders Today] C
LEFT JOIN [Open Orders From Yesterday] O
   ON (C.[Sales Order Number] = O.[Sales Order Number]) 
  WHERE O.[Ship Date] = getdate() - 1 /*Gets All records from Yeterday's date*/
  AND (C.Product = O.Product);

答案 2 :(得分:0)

SELECT [coot].[Customer Code], 
   [coot].[Customer Name], 
   [coot].[Sales Order Number], 
   [coot].[Input By], 
   [coot].[Customer PO Number], 
   [coot].[Date Received], 
   [coot].[Date Entered], 
   [coot].[Date Required], 
   [coot].[Date Promised], 
   [coot].[Ship Date as of Today],

   (select max([coft].[Ship Date]) from [Open Orders From Yesterday] as coft
     where ([coot].[Sales Order Number] = [coft].[Sales Order Number]) 
        AND ([coot].Product = [coft].Product)) AS [Ship Date from Yesterday] 
FROM [Current Open Orders Today] as coot