带有COUNT帮助的SQL子查询

时间:2010-10-07 18:35:02

标签: sql

我有一个有效的SQL语句

SELECT * FROM eventsTable WHERE columnName='Business'

我想将其添加为子查询...

COUNT(Business) AS row_count

我该怎么做?

4 个答案:

答案 0 :(得分:61)

这可能是最简单的方法,但不是最漂亮的方式:

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

这也可以在不使用group by

的情况下运行
SELECT *, COUNT(*) OVER () as RowCount
    FROM eventsTables
    WHERE columnName = 'Business'

答案 1 :(得分:10)

您想获得行数吗?

SELECT columnName, COUNT(*) AS row_count
FROM eventsTable
WHERE columnName = 'Business'
GROUP BY columnName

答案 2 :(得分:9)

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
WHERE e.columnName='Business'

- 增加了空间

答案 3 :(得分:1)

假设有一个名为business的列:

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