SQL subquery with COUNT help

前端 未结 4 1690
挽巷
挽巷 2021-02-01 13:41

I have an SQL statement that works

SELECT * FROM eventsTable WHERE columnName=\'Business\'

I want to add this as a subquery...

         


        
相关标签:
4条回答
  • 2021-02-01 14:00

    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'
    
    0 讨论(0)
  • 2021-02-01 14:05

    Assuming there is a column named business:

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

    0 讨论(0)
  • 2021-02-01 14:08
    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'
    

    -- Added space

    0 讨论(0)
  • 2021-02-01 14:12

    Do you want to get the number of rows?

    SELECT columnName, COUNT(*) AS row_count
    FROM eventsTable
    WHERE columnName = 'Business'
    GROUP BY columnName
    
    0 讨论(0)
提交回复
热议问题