DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table (questionRecID INT NOT NULL PRIMARY KEY,value INT NOT NULL,periodMonth INT NOT NULL, periodYear INT NOT NULL, practiceID INT NOT NULL);
INSERT INTO my_table VALUES
(16 ,70 ,11 ,2010 ,475 ),
(28 ,33 ,11 ,2010 ,475),
(14 ,226 ,11 ,2010 ,475);
SELECT * FROM my_table;
+---------------+-------+-------------+------------+------------+
| questionRecID | value | periodMonth | periodYear | practiceID |
+---------------+-------+-------------+------------+------------+
| 14 | 226 | 11 | 2010 | 475 |
| 16 | 70 | 11 | 2010 | 475 |
| 28 | 33 | 11 | 2010 | 475 |
+---------------+-------+-------------+------------+------------+
SELECT x.*
, x.value/y.value pct
FROM my_table x
JOIN my_table y
ON y.practiceid = x.practiceid
AND y.questionrecid = 14;
+---------------+-------+-------------+------------+------------+--------+
| questionRecID | value | periodMonth | periodYear | practiceID | pct |
+---------------+-------+-------------+------------+------------+--------+
| 14 | 226 | 11 | 2010 | 475 | 1.0000 |
| 16 | 70 | 11 | 2010 | 475 | 0.3097 |
| 28 | 33 | 11 | 2010 | 475 | 0.1460 |
+---------------+-------+-------------+------------+------------+--------+
你有什麼試過的?我覺得'questionRecID = 14'具有這樣一個特定目的是很奇怪的。 – 2013-03-12 09:56:33
我什麼都不懂,你做70/226那麼id = 28呢? – 2013-03-12 09:58:44
問題14是計算問題16百分比的分母。像這樣:q14 - 有多少患者有哮喘,q16 - 有多少患者有哮喘歸類爲持久 – user2160174 2013-03-12 11:07:10