简体   繁体   中英

Error-Select inside count

I have this procedure

SELECT COUNT( a.id_badania ) AS iloscBadan, COUNT( b.zdjecie ) AS iloscZdjec, COUNT (SELECT id_badania FROM Badania WHERE status='W trakcie realizacji';)
FROM Badania a
INNER JOIN Zdjecia b ON a.id_badania = b.id_badania

And this error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT id_badania FROM Badania WHERE status='W trakcie realizacji') FROM Badani' at line 1

I don't know sql well.

You can use sum with case statement for to count the values for a particular status

    SELECT COUNT( a.id_badania )   AS iloscBadan, COUNT( b.zdjecie ) AS iloscZdjec, Sum( case when status='W trakcie realizacji' then 1 else 0 end ) 
     FROM Badania a
     INNER JOIN Zdjecia b ON a.id_badania = b.id_badania

You can't put a subquery in an aggregation function. And you have a semicolon in the middle of the query. I am guessing that you just want condtiional aggregation. Either this:

SELECT COUNT( a.id_badania ) AS iloscBadan, COUNT( b.zdjecie ) AS iloscZdjec,
       SUM(status = 'W trakcie realizacji')
FROM Badania a INNER JOIN
     Zdjecia b
     ON a.id_badania = b.id_badania;

Or:

SELECT COUNT( a.id_badania ) AS iloscBadan, COUNT( b.zdjecie ) AS iloscZdjec,
       COUNT(DISTINCT case when status = 'W trakcie realizacji' then a.id_badania end)
FROM Badania a INNER JOIN
     Zdjecia b
     ON a.id_badania = b.id_badania;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM