0

我正在尝试在(entrydate 列中的最大日期)和(entrydate 列中的最大日期-15 天)之间获取数据!!

表1:共有953行 在此处输入图像描述

表 2:总共有 400 行 在此处输入图像描述

我得到了这个错误,请帮助我!

    Select  
o_material_transaction_inward.Mat_Code,
s_material_details.Mat_Spec,
s_material_details.Mat_Make,
o_material_transaction_inward.Sup_Name,
o_material_transaction_inward.Entry_Date,
o_material_transaction_inward.DC_qty,
o_material_transaction_inward.Received_qty,
    from 
o_material_transaction_inward 
    join 
s_material_details 
    on 
s_material_details.Mat_Code=o_material_transaction_inward.Mat_Code and s_material_details.Mat_Group_Id=o_material_transaction_inward.Mat_Group_id
    where 
o_material_transaction_inward.Entry_Date between Max(o_material_transaction_inward.Entry_Date) and Max(o_material_transaction_inward.Entry_Date - 15)
4

3 回答 3

2

更新:固定BETWEEN谓词:

它不起作用的原因是最低值必须是BETWEEN谓词中的第一个值,因此为了从两个表中获取这些行,它们entry_date的最大输入日期介于 - 15 和最大输入日期之间,请尝试以下操作:

SELECT
  o.Mat_Code,
  s.Mat_Spec,
  s.Mat_Make,
  o.Sup_Name,
  DATE_FORMAT(o.Entry_Date, '%Y-%m-%d') AS Entry_Date,
  o.DC_qty,
  o.Received_qty
FROM o_material_transaction_inward AS o
INNER JOIN s_material_details      AS s  ON s.Mat_Code     = o.Mat_Code
WHERE o.Entry_Date BETWEEN ((SELECT Max(Entry_Date) 
                            FROM o_material_transaction_inward) - 15)
                       AND (SELECT Max(Entry_Date) 
                            FROM o_material_transaction_inward) ;

SQL 小提琴演示

于 2013-02-05T12:46:06.050 回答
0

这应该有效:

SELECT
  o.Mat_Code,
  s.Mat_Spec,
  s.Mat_Make,
  o.Sup_Name,
  o.Entry_Date,
  o.DC_qty,
  o.Received_qty,
FROM o_material_transaction_inward AS o
INNER JOIN s_material_details      AS s  ON s.Mat_Code     = o.Mat_Code
                                        AND s.Mat_Group_Id = o.Mat_Group_id
WHERE o.Entry_Date BETWEEN (SELECT Max(Entry_Date) 
                            FROM o_material_transaction_inward) 
                       AND (SELECT Max(Entry_Date) 
                            FROM o_material_transaction_inward) - 15);
于 2013-02-05T12:58:39.713 回答
0

尝试:

    Select  
o_material_transaction_inward.Mat_Code,
s_material_details.Mat_Spec,
s_material_details.Mat_Make,
o_material_transaction_inward.Sup_Name,
o_material_transaction_inward.Entry_Date,
o_material_transaction_inward.DC_qty,
o_material_transaction_inward.Received_qty,
    from 
o_material_transaction_inward 
    join 
s_material_details 
    on 
s_material_details.Mat_Code=o_material_transaction_inward.Mat_Code and s_material_details.Mat_Group_Id=o_material_transaction_inward.Mat_Group_id
    having 
o_material_transaction_inward.Entry_Date between Max(o_material_transaction_inward.Entry_Date) and Max(o_material_transaction_inward.Entry_Date - 15)

(参见MySQL 的公认答案:Invalid use of group function来解释wherevs having

于 2013-02-05T12:48:54.107 回答