tags:

views:

61

answers:

4

I have an SQL statement that works

SELECT * FROM eventsTable WHERE columnName='Business'

I want to add this as a subquery...

COUNT(Business) AS row_count

How do I do this?

+3  A: 

Do you want to get the number of rows?

SELECT columnName, COUNT(*) AS row_count
FROM eventsTable
WHERE columnName = 'Business'
GROUP BY columnName
eumiro
Can't have `where` clause in group by. Use `Having`
Jim
Unless I am mistaken about what is being asked for, this answer should be what the OP needs.
Mike Cheel
@Jim - You can use have a `where` clause with `group by`. You would need to use `having` if you wanted to filter on the result of an aggregate when using `group by`.
Barry
A: 

Assuming there is a column named business:

SELECT Business, COUNT(*) FROM eventsTable GROUP BY Business

Noel Abrahams
+3  A: 

This is probably the easiest way, not the prettiest though:

SELECT *,
    (SELECT Count(*) FROM eventsTable WHERE columnName='Business')  as RowCount

    FROM eventsTable WHERE columnName='Business'

This will also work without having to use a group by

   Select *, COUNT(*) over() as 'RowCount'
   From eventsTables
   WHERE columnName ='Business'
Barry
+1 Over clause is *elegant*
gbn
+1  A: 
SELECT e.*,
       cnt.colCount 
FROM eventsTable e
INNER JOIN (
           select columnName,count(columnName) as colCount
           from eventsTable e2 
          group by columnName
           ) as cnt on cnt.columnName = e.columnName
WHEREe.columnName='Business'
rugg