Group By Count

  • Hello Everyone

    I am just playing around with some code. I would like to figure out how to finish the query so that the query will return only the DealerNames that are listed more than 3 time in the table. I know this is simple, but my mind will not come up with the answer.

    Thanks in advance for your help

    Andrew SQLDBA

    DECLARE @Dealer Table

    (

    DealerName varchar(50)

    )

    GO

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'stevescars'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'stevescars'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'stevescars'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'stevescars'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'stevescars'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'simonauto'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'simonauto'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'rickmotors'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'rickmotors'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'rickmotors'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'rickmotors'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'rickmotors'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'rickmotors'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'davevans'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'davevans'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'Andrew'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'Andrew'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'Andrew'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'Andrew'

    )

    INSERT INTO @Dealer

    (DealerName)

    VALUES

    (

    'Andrew'

    )

    GO

    SELECT

    DealerName

    , COUNT(*) AS DealerCount

    FROM

    Dealer

    GROUP BY

    DealerName

    GO

  • Since you have already Grouped them by dealer's;

    You just had to add the Having clause to get the count > 3

    SELECT DealerName

    , COUNT(DealerName) AS DealerCount

    FROM Dealer

    GROUP BY DealerName

    HAVING COUNT(DealerName) > 3

  • Having COUNT(*) > 3?


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Thanks Everyone

    I knew it was something really simple that I could not think of

    Andrew SQLDBA

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply