2014-02-19 86 views
1

以下是我對Postgres數據庫運行的查詢。如何按兩列分組只顯示一行?

SELECT ad.col1 
    ,ad.col2 
    ,md.col3 
    ,ad.col4 
    ,mcd.col5 
    ,AVG(mcd.col5/md.col3) AS dc 
    ,AVG(md.col3/ad.col4) AS cb 
FROM tableCount AS md 
INNER JOIN tablePop AS ad ON ad.col1 = md.col1 
    AND ad.col2 = md.col2 
INNER JOIN tableData AS mcd ON mcd.col1 = md.col1 
    AND mcd.col2 = md.col2 
WHERE md.col2 = 23 
    AND md.col1 = '1' 
GROUP BY ad.col1 
    ,ad.col2 
    ,md.col3 
    ,ad.col4 
    ,mcd.col5 
ORDER BY md.col3 DESC limit 30 
GROUP BY ad.col1 
    ,ad.col2; 

,下面將我找回控制檯上使用上述查詢 -

col1 col2 col3  col4  col5  dc         cb 
1  23  48108  224123  479   0.00000000000000000000    0.00000000000000000000 
1  23  48108  224123  89   0.00000000000000000000    0.00000000000000000000 
1  23  48108  224123  142   0.00000000000000000000    0.00000000000000000000 
1  23  48108  224123  1649  0.00000000000000000000    0.00000000000000000000 
1  23  48108  224123  14   0.00000000000000000000    0.00000000000000000000 
1  23  48108  224123  203   0.00000000000000000000    0.00000000000000000000 
1  23  48108  224123  52   0.00000000000000000000    0.00000000000000000000 
1  23  48108  224123  62   0.00000000000000000000    0.00000000000000000000 
1  23  48108  224123  65   0.00000000000000000000    0.00000000000000000000 
1  23  48108  224123  33   0.00000000000000000000    0.00000000000000000000 
  • 現在,如果你看到我上面的輸出,123即將多次的輸出在col1col2中。我想將它們組合在一行中,以便我將SUM col3, col4, col5, dc and cb

所以我想輸出,看看它是 -

col1 col2 col3  col4  col5  dc         cb 
1  23  48108  2241230  278   0.00000000000000000000    0.00000000000000000000 

這是可能在SQL辦?

P.S任何小提琴的例子都會很棒。

+0

你爲什麼用'md.col3,ad.col4,mcd.col5'分組,你可以採取COL5的COL3和COL4和平均的'max'並獲得你所需要的。 – user2989408

+1

你說得對,SQLfiddle總是很好用。繼續並創建一個。 –

回答

1

你想從group by刪除col5

SELECT ad.col1 
    ,ad.col2 
    ,md.col3 
    ,ad.col4 
    ,sum(mcd.col5) as col5 
    ,AVG(mcd.col5/md.col3) AS dc 
    ,AVG(md.col3/ad.col4) AS cb 
FROM tableCount AS md 
INNER JOIN tablePop AS ad ON ad.col1 = md.col1 
    AND ad.col2 = md.col2 
INNER JOIN tableData AS mcd ON mcd.col1 = md.col1 
    AND mcd.col2 = md.col2 
WHERE md.col2 = 23 
    AND md.col1 = '1' 
GROUP BY ad.col1 
    ,ad.col2 
    ,md.col3 
    ,ad.col4 
ORDER BY md.col3 DESC limit 30 

我認爲最後group byorder by在那裏意外。

0

也許是這樣的:

SELECT ad.col1 
    ,ad.col2 
    ,min(md.col3) 
    ,min(ad.col4) 
    ,min(mcd.col5) 
    ,AVG(mcd.col5/md.col3) AS dc 
    ,AVG(md.col3/ad.col4) AS cb 
FROM tableCount AS md 
INNER JOIN tablePop AS ad ON ad.col1 = md.col1 
    AND ad.col2 = md.col2 
INNER JOIN tableData AS mcd ON mcd.col1 = md.col1 
    AND mcd.col2 = md.col2 
WHERE md.col2 = 23 
    AND md.col1 = '1' 
GROUP BY ad.col1 
    ,ad.col2 
ORDER BY min(md.col3) 
1

可以SUM()或採取MAX(),實際上它只是col5你的樣品是導致多行,GROUP BY任何非集合場,並挑選合適的骨料任何其他字段:

SELECT ad.col1 
    ,ad.col2 
    ,MAX(md.col3) 
    ,MAX(ad.col4) 
    ,AVG(mcd.col5) 
    ,AVG(mcd.col5/md.col3) AS dc 
    ,AVG(md.col3/ad.col4) AS cb 
FROM tableCount AS md 
INNER JOIN tablePop AS ad ON ad.col1 = md.col1 
    AND ad.col2 = md.col2 
INNER JOIN tableData AS mcd ON mcd.col1 = md.col1 
    AND mcd.col2 = md.col2 
WHERE md.col2 = 23 
    AND md.col1 = '1' 
GROUP BY ad.col1 
    ,ad.col2 
ORDER BY md.col3 DESC limit 30 
0

您可以通過使查詢生成具有別名的子查詢來將查詢轉變爲派生表。然後你可以選擇它。

select col1, col2, col3, col4, col5, dc, sum(col3 + col4 + col5 + dc + cb) thesum 
from (query from your question goes here) temp 
group by col1, col2, col3, col4, col5, dc