views:

67

answers:

4

Tried my usual references at w3schools and google. No luck

I'm trying to produce the following results. QTY is a derived column

   | Position | QTY
 --------------------
 1   Clerk      2
 2   Mgr        2   

Here's what I'm not having luck with:

  SELECT Position, Count(position) AS 'QTY'
    FROM tblemployee
   Where ('QTY' != 1)
GROUP BY Position

I know that my Position is set up as varchar(255) Count produces a integer data and my where clasue is accurate so that leads me to believe that that Count() is jamming me up. Please throw up an example so I can reference later. Thanks for the help!

+2  A: 

That's what the HAVING clause is for:

SELECT Position, Count(position) AS 'QTY'
FROM tblemployee
GROUP BY Position
HAVING QTY != 1
VoteyDisciple
You beat me to it!
Huy Tran
+2  A: 
SELECT Position, Count(position) AS 'QTY'
FROM tblemployee
GROUP BY Position
HAVING Count(Position) != 1

Note: this is how it will be in SQL Server & should also be the case with mysql (I guess).

shahkalpesh
thanks for the help! Makes a big difference when your looking at the wrong clause.
Matt
Try to use the standard <> for "not equals" http://msdn.microsoft.com/en-us/library/ms188074.aspx
gbn
+1  A: 

In SQL Server, I'd do:

SELECT Position, Count(position) AS 'QTY' 
FROM tblemployee 
GROUP BY Position 
HAVING COUNT(position) <> 1

I don't know if that works in MySql, but I believe it is standard SQL syntax.

Paul Kearney - pk
A: 

There's a couple of issues. In SQL you can't reference the QTY column by name (in this case). Also, since you're grouping and want to exclude a certain value you have to use the "HAVING" clause instead of the where clause.

The where clause CAN be used to twit out thins that you don't want to count. (see example 2)

ST

SELECT Position, Count(position) AS QTY
FROM tblemployee
GROUP BY Position
HAVING Count(position) <> 1

-- Example 2
-- Don't count poor old Johnny
SELECT Position, Count(position) AS QTY
FROM tblemployee
WHERE Position <> 'Johnny'
GROUP BY Position
HAVING Count(position) <> 1
souLTower