2012-07-19 114 views
2

我正在運行此查詢。從加入表中選擇MAX(日期)

SELECT 
    t1.quantity, 
    t2.transaction_date, 
    t1.product_id 
FROM 
    ezsystem_usah.invoice_line AS t1  
    INNER JOIN ezsystem_usah.invoices AS t2 
    ON t1.invoice_id = t2.id_invoices 
WHERE 
    t2.customer_id = '8000004C-1325619329' 
    AND (product_id = '8000016F-1325198704' 
    OR product_id = '80000027-1324422404' OR ...); 

所得表是:

quantity | transaction_date | product_id 
7   2012-01-04   8000016F-1325198704 
8   2012-03-05   8000016F-1325198704 
1   2012-01-05   11111111-1324422404 
...  ...    ... 

我想選擇用於特定PRODUCT_ID基於MAX(TRANSACTION_DATE)行,以產生這樣的事情:

quantity | transaction_date | product_id 
8   2012-03-05   8000016F-1325198704 
1   2012-01-05   11111111-1324422404 

換句話說,從連接表中,我想選擇每個產品ID的最新條目。

我曾嘗試:

SELECT 
    t1.quantity, 
    MAX(t2.transaction_date), 
    t1.product_id 
FROM 
    ezsystem_usah.invoice_line AS t1 
    INNER JOIN ezsystem_usah.invoices AS t2 ON t1.invoice_id = t2.id_invoices 
WHERE 
    t2.customer_id = '8000004C-1325619329' 
    AND (product_id = '8000016F-1325198704' OR product_id = '80000027-1324422404' OR ...)  
GROUP BY 
t1.product_id; 

但是數量是不正確。

另一種方法,我沒有嘗試過,但我認爲可能的工作是使用HAVING MAX(transaction_date) = ([sub query]),但它似乎代價高昂。

最糟糕的情況是,我可以針對每個產品逐個運行查詢,但是如果可能的話,我想避免這種查詢。

感謝, 丹麥

+2

哪個表是transaction_date和product_id中的? – 2012-07-19 21:52:37

+0

謝謝你的擡頭,我添加了信息 – Danedo 2012-07-20 14:28:09

回答

0

這是否做的伎倆?

SELECT 
    t1.quantity, 
    transaction_date, 
    product_id 
FROM 
    ezsystem_usah.invoice_line AS t1 
    INNER JOIN ezsystem_usah.invoices AS t2 ON t1.invoice_id = t2.id_invoices 
WHERE 
    t2.customer_id = '8000004C-1325619329' 
    AND product_id IN ('8000016F-1325198704', '80000027-1324422404') 
    AND transaction_date = (SELECT MAX(inv.transaction_date) 
          FROM ezsystem_usah.invoices inv 
          INNER JOIN ezsystem_usah.invoice_line ln 
           ON inv.customer_id = t2.customer_id 
           AND ln.invoice_id = inv.id_invoices 
           AND ln.product_id = t1.product_id 
         ) 

我花了猜測哪些表包含的字段product_idtransaction_date因爲它不是從原來的查詢清楚。

2

這應該做你所要求的並且合理有效。產品在內循環中使用IN()進行檢查,因此如果您在product_id和customer_id上建立索引,它應該運行得非常快。建議在t1.invoice_id上​​建立索引。

SELECT 
    t1.quantity, 
    transaction_date, 
    product_id 
FROM 
    ezsystem_usah.invoice_line AS t1  
    JOIN (
    SELECT t3.transaction_date, t3.product_id, t3.id_invoices 
     FROM ezsystem_usah.invoices t3 
     JOIN 
(SELECT MAX(transaction_date) AS max_transaction_date, 
    product_id 
    FROM ezsystem_usah.invoices 
    WHERE product_id IN (
     '8000016F-1325198704', '80000027-1324422404' 
    ) 
    AND customer_id = '8000004C-1325619329' 
    GROUP BY product_id) AS uniqued 
    ON (t3.transaction_date = uniqued.max_transaction_date 
     AND t3.product_id = uniqued.product_id 
    ) 
) AS t2 
ON t1.invoice_id = t2.id_invoices; 
0

這是一個比較簡單的配方

with t as (<your query here>) 
select t.* 
from t join 
    (select t.product_id, max(t.transaction_date) as maxdate 
     from t 
     group by t.product_id 
    ) tmax 
    on t.product_id = tmax.product_id and 
     t.transaction_date = tmax.maxdate 

此解決方案只需要你,包括你的查詢一次,使用「與」條款。

0

你的替代品子查詢(如在其他的答案提供),或下面的反連接(自連接):

SELECT 
    t1.quantity, 
    t2.transaction_date, 
    t1.product_id 
FROM 
    ezsystem_usah.invoice_line AS t1 
    INNER JOIN ezsystem_usah.invoices AS t2 
    ON t1.invoice_id = t2.id_invoices 
    LEFT JOIN ezsystem_usah.invoices AS t3 
    ON t3.transaction_date > t2.transaction_date 
    AND t3.id_invoices = t2.id_invoices 
WHERE 
    t2.customer_id = '8000004C-1325619329' AND 
    (t1.product_id = '8000016F-1325198704' OR t1.product_id = '80000027-1324422404' OR ...) AND 
    t3.id_invoices IS NULL 
GROUP BY 
t1.product_id 

的想法是,如果有比當前行日期更大的發票被發現,它不包括該行。我可能沒有完全正確的額外連接。我對主鍵進行了一些猜測。