0

私のSQLでこの種のテーブルを取得したい:

    Type     Qad (type count)     Correct(No. of item without error)     Accuracy(%)
   type 1        3                                 3                            100
   type2         6                                 3                             50

私は現在このクエリを使用しようとしていますが、エラーがあります:

SELECT `type` AS 'ReturnType', COUNT(*) AS 'QAd',
(SELECT COUNT(*) FROM bartran WHERE QASample='2' AND QAErrorID='1' AND `type`=ReturnType AND TimefileDate BETWEEN '2012-01-01' AND '2012-12-31' AS 'Correct', 
(SELECT COUNT(*) FROM bartran WHERE QASample='2' AND QAErrorID='1' AND `type`=ReturnType AND TimefileDate BETWEEN '2012-01-01' AND '2012-12-31') / COUNT(*) * 100) AS 'Accuracy' 
FROM bartran WHERE QASample='2'
AND TimefileDate BETWEEN '2012-01-01' AND '2012-12-31'
GROUP BY TYPE;

私は現在これに不慣れです、誰かがこれで私を助けてくれることを願っています。ありがとう!!

4

1 に答える 1

3

クエリに角かっこがありません。正しいのは次のとおりです。

SELECT `type`  AS 'ReturnType', 
       Count(*)   AS 'QAd', 
       (SELECT Count(*) 
        FROM   bartran 
        WHERE  qasample = '2' 
               AND qaerrorid = '1' 
               AND `type` = returntype 
               AND timefiledate BETWEEN '2012-01-01' AND '2012-12-31') AS 
       'Correct', 
       (SELECT Count(*) 
        FROM   bartran 
        WHERE  qasample = '2' 
               AND qaerrorid = '1' 
               AND `type` = returntype 
               AND timefiledate BETWEEN '2012-01-01' AND '2012-12-31') / Count(* 
       ) * 100 AS 'Accuracy' 
FROM   bartran 
WHERE  qasample = '2' 
       AND timefiledate BETWEEN '2012-01-01' AND '2012-12-31' 
GROUP  BY type; 

はるかに優れたクエリは次のようになります。

SELECT `type` AS 'ReturnType', 
       Count(*)  AS 'QAd', 
       Count(CASE 
               WHEN qaerrorid = '1' THEN 1 
               ELSE NULL 
             end)  AS 'Correct', 
       Count(CASE 
               WHEN qaerrorid = '1' THEN 1 
               ELSE NULL 
             end) / Count(*) * 100 AS 'Accuracy' 
FROM   bartran 
WHERE  qasample = '2' 
       AND timefiledate BETWEEN '2012-01-01' AND '2012-12-31' 
GROUP  BY type;
于 2013-01-17T10:13:37.623 に答える