2013-08-22 86 views
1

我需要獲得GROUPed BY值, 但我需要看到的不是隨機價格值(當我選擇價格時),而是最新價格值(價格與最高的ID一排在該組中)SELECT MIN,MAX,AVG and current(for latest insert ID)value all together

SELECT ID, price, 
     ROUND(AVG(price)), 
     MIN(price), 
     MAX(price), 
     ROUND((AVG(price)-MIN(price))/AVG(price) * 100) as differenceinprices 
FROM `m-orbitzone` 
WHERE dep = 'MOW' 
    AND returnornot = 1 
GROUP BY arv, date1, date2 
ORDER BY differenceinprices DESC LIMIT 1000 



ID price <--  ROUND(AVG(price)) MIN(price) MAX(price) differenceinprices 
122841 834 816 534 834 35 
122708 783 790 524 821 34 
122754 766 796 529 815 34 
28528 810 766 512 810 33 
28529 799 765 512 799 33 
122603 766 798 534 841 33 
122848 766 794 529 810 33 
122589 778 765 519 778 32 
122591 778 768 519 778 32 
122749 766 775 529 814 32 
28527 752 749 512 773 32 
122744 766 773 529 814 32 
122843 766 771 529 802 31 

極品「價格」成爲這個團體(行具有最高ID)最新價格

可能需要做選擇,然後做從結果中多選一個?

謝謝!

回答

1

這應該做的伎倆:

SELECT m.ID, price, 
     ROUND(AVG(price)), 
     MIN(price), 
     MAX(price), 
     ROUND((AVG(price)-MIN(price))/AVG(price) * 100) as differenceinprices 
FROM `m-orbitzone` m 
INNER JOIN (
    SELECT 
    ID 
    FROM 
    `m-orbitzone` m 
    WHERE ID = (SELECT MAX(ID) FROM `m-orbitzone` sm WHERE m.arv = sm.arv AND m.date1 = sm.date1 AND m.date2 = sm.date2) 
) s ON m.ID = s.ID 
WHERE dep = 'MOW' 
    AND returnornot = 1 
GROUP BY arv, date1, date2 
ORDER BY differenceinprices DESC LIMIT 1000 

的話題很好看的是this manual entry