?
Solved

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

Posted on 2007-10-06
2
Medium Priority
?
377 Views
Last Modified: 2008-01-09
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
Comment
Question by:SESImage
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1000 total points
ID: 20029118
Select count(*) from
(select top 100 * from MyTable MT order by ID desc)t where t.Column2 = 'FALSE'

0
 
LVL 15

Assisted Solution

by:dbbishop
dbbishop earned 1000 total points
ID: 20029133
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

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question