2013-02-05 71 views
0

我試圖在(entrydate列中的最大日期)和(entrydate列中的最大日期-15days)之間獲取數據!SQL錯誤(1111):組函數無效使用

表1:完全有953rows enter image description here

表2:完全有400rows 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) 

回答

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

+0

0rows,我使用了具有而不是介於兩者之間,但它提取除我預期的15行之外的所有行。 –

+0

@RanjitKumar - 您可以添加這兩個表'o_material_transaction_inward','s_material_details'的一些示例數據以及您希望從這兩個表中獲得的預期結果。這將非常有幫助。謝謝。 –

+0

@Gamal肯定要等下去 –

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接受的答案爲where VS having的解釋)

+0

它獲取所有行期望15行! –

+0

SQL錯誤(1054):'having子句'中的未知列'o_material_transaction_inward.Entry_Date' –

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); 
+0

沒有錯誤並且還有0行影響輸出。它沒有獲取(最大日期)和(最大日期-15)之間的數據 –

+0

選擇最大(入境日期)我用下面的查詢,但它沒有奏效。 (SELECT DATE_SUB(Max(Entry_Date),INTERVAL 15 DAY)FROM o_material_transaction_inward)在最後一行 –

相關問題