• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

How to combine count(*) and top # rows in SQL

I'm trying to do this, but I get an error "Line 1: Incorrect syntax near ')'."
Select count(*) from (select top 100 * from MyTable MT order by ID desc) where MT.Column2 = 'FALSE'

MyTable
ID  Column2
1  TRUE
2  TRUE
3  FALSE
0
SESImage
Asked:
SESImage
2 Solutions
 
Aneesh RetnakaranDatabase AdministratorCommented:
Select count(*) from
(select top 100 * from MyTable MT order by ID desc)t where t.Column2 = 'FALSE'

0
 
dbbishopCommented:
SELECT COUNT(*)
FROM (
            SELECT TOP *
             FROM MyTable ORDER BY ID desc
          ) MT
            WHERE MT.Column2 = 'FALSE'

The entire inner table - subquery requres an alisa *MT in order to reference columns within it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now