arhicoc
May 21st, 2009, 01:53 AM
SELECT inspectors.id, inspectors.name, COUNT(persons.cnp) AS tickets_total,
SUM(CASE WHEN (dbo.persons.received = 1 AND (dbo.persons.cupon NOT IN (SELECT name FROM dbo.cupons))) THEN 1 ELSE 0 END) AS tickets_another_cupon
FROM inspectors INNER JOIN
persons ON inspector.id = persons.id_inspector
GROUP BY inspectors.id, inspectors.name ORDER BY inspectors.name
Message:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
How can I get this ?
I need to find the number of tickets for an inspector with a cupon other than those listed in the cupons table.
Another way
SELECT inspectors.id, inspectors.name, count(persons.cnp) as tickets_total,
(SUM(CASE WHEN persons.cupon IN ('Cupon January','Cupon February')
THEN 1 ELSE 0 END))
FROM inspectors INNER JOIN
persons ON inspectors.id = persons.id_inspector
GROUP BY inspectors.id, inspectors.name ORDER BY inspectors.name
Message
Column 'persons.cupon' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If I do this
SELECT inspectors.id, inspectors.name, count(persons.cnp) as tickets_total,
(SUM(CASE WHEN persons.cupon IN ('Cupon January','Cupon February')
THEN 1 ELSE 0 END))
FROM inspectors INNER JOIN
persons ON inspectors.id = persons.id_inspector
GROUP BY inspectors.id, inspectors.name,persons.cupon ORDER BY inspectors.name
Every inspector is repeated for each type of cupon and that is not what I want.
SUM(CASE WHEN (dbo.persons.received = 1 AND (dbo.persons.cupon NOT IN (SELECT name FROM dbo.cupons))) THEN 1 ELSE 0 END) AS tickets_another_cupon
FROM inspectors INNER JOIN
persons ON inspector.id = persons.id_inspector
GROUP BY inspectors.id, inspectors.name ORDER BY inspectors.name
Message:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
How can I get this ?
I need to find the number of tickets for an inspector with a cupon other than those listed in the cupons table.
Another way
SELECT inspectors.id, inspectors.name, count(persons.cnp) as tickets_total,
(SUM(CASE WHEN persons.cupon IN ('Cupon January','Cupon February')
THEN 1 ELSE 0 END))
FROM inspectors INNER JOIN
persons ON inspectors.id = persons.id_inspector
GROUP BY inspectors.id, inspectors.name ORDER BY inspectors.name
Message
Column 'persons.cupon' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If I do this
SELECT inspectors.id, inspectors.name, count(persons.cnp) as tickets_total,
(SUM(CASE WHEN persons.cupon IN ('Cupon January','Cupon February')
THEN 1 ELSE 0 END))
FROM inspectors INNER JOIN
persons ON inspectors.id = persons.id_inspector
GROUP BY inspectors.id, inspectors.name,persons.cupon ORDER BY inspectors.name
Every inspector is repeated for each type of cupon and that is not what I want.