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

Posted on 2007-10-06
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'

ID  Column2
Question by:SESImage
    LVL 75

    Accepted Solution

    Select count(*) from
    (select top 100 * from MyTable MT order by ID desc)t where t.Column2 = 'FALSE'

    LVL 15

    Assisted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now