0

I want to retrieve this kind of table in my sql:

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

I am curretly trying to use this query but it has an error:

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;

I'm current new to this one, hope someone helps me with this. thanks!!

2
  • State the error returned. Commented Jan 17, 2013 at 10:11
  • select Type, Qad, Correct, Accuracy from bartran where QASample = '2' AND other stuff you want the data to be like GROUP BY Type; Commented Jan 17, 2013 at 10:13

1 Answer 1

3

You had the brackets ) missing in the query, correct is:

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; 

Much better Query would be:

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;
Sign up to request clarification or add additional context in comments.

4 Comments

i think this now solve my problem, but why does the execution takes a lot of time? 1 minute plus. :(
@JRC Try the new Query that i have added
@Ankur it a good point to use SUM instead Count SUM(CASE WHEN qaerrorid = '1' THEN 1 ELSE 0 end). Also use COUNT(1), not COUNT(*). It will be a bit faster
@AndreyVoloshin count(1) and count(*) are same, see this and also I did not find anywhere if sum is better than count, so I would prefer count for code clarity point of view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.