2017-03-07 63 views
0

我需要構建下表。如何使用不同的where子句檢索多個列計數?

<style type="text/css"> 
 
.tg {border-collapse:collapse;border-spacing:0;} 
 
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;} 
 
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;} 
 
.tg .tg-baqh{text-align:center;vertical-align:top} 
 
.tg .tg-lap0{font-size:100%;text-align:center;vertical-align:top} 
 
.tg .tg-yw4l{vertical-align:top} 
 
</style> 
 
<table class="tg" style="undefined;table-layout: fixed; width: 593px"> 
 
<colgroup> 
 
<col style="width: 67px"> 
 
<col style="width: 72px"> 
 
<col style="width: 116px"> 
 
<col style="width: 116px"> 
 
<col style="width: 116px"> 
 
<col style="width: 106px"> 
 
</colgroup> 
 
    <tr> 
 
    <th class="tg-lap0">State</th> 
 
    <th class="tg-baqh">City</th> 
 
    <th class="tg-baqh">Three Days Ago</th> 
 
    <th class="tg-baqh">Two Days Ago</th> 
 
    <th class="tg-baqh">One Day Ago</th> 
 
    <th class="tg-baqh">Total</th> 
 
    </tr> 
 
    <tr> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    </tr> 
 
    <tr> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    </tr> 
 
    <tr> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    </tr> 
 
    <tr> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    <td class="tg-yw4l"></td> 
 
    </tr> 
 
</table>

狀態城市是簡單的選擇,但three_days_ago,two_days_ago,one_day_ago是DB條目同一城市名稱的次數。 所有這些都在DB中的同一個表中。

所以我像我的查詢像這樣的事:

$union = pg_query($db, 
" 
(
SELECT estado, municipio, COUNT(*) 
FROM focos_bdq 
WHERE bioma LIKE 'Amazônia' 
AND satelite LIKE 'AQUA_M-T' 
AND data_hora_gmt::date='$three_days_ago' 
GROUP BY municipio, estado 
ORDER BY COUNT(*) DESC 
) 
UNION ALL 
(
SELECT estado, municipio, COUNT(*) 
FROM focos_bdq 
WHERE bioma LIKE 'Amazônia' 
AND data_hora_gmt::date='$two_days_ago' 
GROUP BY municipio, estado 
ORDER BY COUNT(*) DESC 
) 
UNION ALL 
(
SELECT estado, municipio, COUNT(*) 
FROM focos_bdq 
WHERE bioma LIKE 'Amazônia' 
AND data_hora_gmt::date='$one_day_ago' 
GROUP BY municipio, estado 
ORDER BY COUNT(*) DESC 
) 
" 

但這查詢導致一個很長的表,當我需要把日期在同一行。

+0

編輯你的問題,並提供樣本數據和期望的結果*文本格式*所以它是可讀的。 –

+0

夥計們,感謝您的關注。 它已通過PHP中的後處理解決了... –

回答

0

很難說沒有實際的數據,這樣的事情應該做的伎倆:

with 
3d as (
SELECT estado, municipio, COUNT(*) 
FROM focos_bdq 
WHERE bioma LIKE 'Amazônia' 
AND satelite LIKE 'AQUA_M-T' 
AND data_hora_gmt::date='$three_days_ago' 
GROUP BY municipio, estado 
ORDER BY COUNT(*) DESC 
) 
, 2d as (
SELECT estado, municipio, COUNT(*) 
FROM focos_bdq 
WHERE bioma LIKE 'Amazônia' 
AND data_hora_gmt::date='$two_days_ago' 
GROUP BY municipio, estado 
ORDER BY COUNT(*) DESC 
) 
, 1d as (
SELECT estado, municipio, COUNT(*) 
FROM focos_bdq 
WHERE bioma LIKE 'Amazônia' 
AND data_hora_gmt::date='$one_day_ago' 
GROUP BY municipio, estado 
ORDER BY COUNT(*) DESC 
) 
select estado, municipio, d3.count,d2.count,d1.count,d3.count+d2.count+d1.count 
from d3 
left outer join d2 on d3.estado = d2.estado and d3.municipio = d2.municipio 
left outer join d1 on d1.estado = d2.estado and d1.municipio = d2.municipio