以下是我對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
- 現在,如果你看到我上面的輸出,
1
和23
即將多次的輸出在col1
和col2
中。我想將它們組合在一行中,以便我將SUMcol3, col4, col5, dc and cb
。
所以我想輸出,看看它是 -
col1 col2 col3 col4 col5 dc cb
1 23 48108 2241230 278 0.00000000000000000000 0.00000000000000000000
這是可能在SQL辦?
P.S任何小提琴的例子都會很棒。
你爲什麼用'md.col3,ad.col4,mcd.col5'分組,你可以採取COL5的COL3和COL4和平均的'max'並獲得你所需要的。 – user2989408
你說得對,SQLfiddle總是很好用。繼續並創建一個。 –