SQL错误(1111):组函数的使用无效

时间:2013-02-05 12:41:50

标签: mysql

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

表1:总共有953个 enter image description here

表2:总共有400个 enter image description here

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

    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)

3 个答案:

答案 0 :(得分: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 Fiddle Demo

答案 1 :(得分: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)

(有关wherehaving的解释,请参阅MySQL: Invalid use of group function的已接受答案)

答案 2 :(得分: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);