2016-10-20 32 views
0

我想要得到的疾病總上週共從該表(tb_data):的Sql - 合併兩個查詢,使之行

P_ID Disease    Date 
1  A      2016-10-11 
2  A      2016-10-11 
3  A      2016-10-14 
4  A      2016-10-19 

,而我有這樣的查詢:

SELECT Disease AS DT, 
COUNT(P_ID) AS PT 
FROM tb_data 
GROUP BY Disease 
union 
SELECT Disease AS DLW, 
COUNT(P_ID) AS PLW 
FROM tb_data 
WHERE Date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY 
AND Date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY 
GROUP BY Disease 
ORDER BY 2 DESC, 1; 

我想結果是什麼樣的這個

DT PT DLW PLW 
A 4 A 3 

不是這個

​​

,因爲我想我的程序讀取這樣的:提前

回答

1

foreach (DataRow kolom in table.Rows) 
       { 
        Total = kolom["PT"].ToString(); 
        Lastweek = kolom["PLW"].ToString(); 
       } 
labelTotal.Text = Total; 
labelLastWeek.Text = Lastweek; 

THX我認爲有條件的聚集是你所需要的。

select disease as dt, 
     sum(1) as pt, 
     disease as dlw, 
     sum(case when date between curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY and curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY then 1 else 0 end) as plw 
from tb_data 
group by disease; 

結果

+------+------+------+------+ 
| dt | pt | dlw | plw | 
+------+------+------+------+ 
| A | 4 | A | 3 | 
+------+------+------+------+ 
+0

哇,我今天新學的東西,謝謝! –