Get a count at contacttitle but get 87 row need 22 rows

  • select
    Country
    , Contacttitle
    , COUNT(ContactTitle) as Count
    ,CASE WHEN GROUPING_ID() = 0 THEN ''
    WHEN GROUPING_ID() = 1 THEN 'Subtotal for << Country >>'
    END AS Legend
    from dbo.customers
    group by ROLLUP(country, ContactTitle)

    What im missing  in the syntax.

  • GG_BI_GG - Tuesday, July 24, 2018 12:54 AM

    select
    Country
    , Contacttitle
    , COUNT(ContactTitle) as Count
    ,CASE WHEN GROUPING_ID() = 0 THEN ''
    WHEN GROUPING_ID() = 1 THEN 'Subtotal for << Country >>'
    END AS Legend
    from dbo.customers
    group by ROLLUP(country, ContactTitle)

    What im missing  in the syntax.

    Kindly post at least minimum information to answer your question.

    Saravanan

  • GG_BI_GG - Tuesday, July 24, 2018 12:54 AM

    select
    Country
    , Contacttitle
    , COUNT(ContactTitle) as Count
    ,CASE WHEN GROUPING_ID() = 0 THEN ''
    WHEN GROUPING_ID() = 1 THEN 'Subtotal for << Country >>'
    END AS Legend
    from dbo.customers
    group by ROLLUP(country, ContactTitle)

    What im missing  in the syntax.

    We don't have your tables to work with, and you haven't supplied any sample data, either.   What; exactly; could we possibly do to help, given nothing to work with?  About the only thing I can ask is why are you doing a count on a column you are grouping by...

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • Grouping_ID doesn't work like that, you need to specify the groupings in its argument list. Although personally for such a simple case I'd just use Grouping as it's a bit easier to understand and you don't need the added complexity if you've only got a single level to distinguish between.

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

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