[Err] 42000 - [SQL Server] Column 'dbo_40.TZZ' is invalid...

  • Hi all, I need your help.

    I try this query but i have this error, why?

    Can you help me?

    Thanks in advance.

    [Err] 42000 - [SQL Server] Column 'dbo_40.TZZ' is invalid in the select list

    because it is not contained in either an aggregate function or the GROUP BY clause.

    SELECT

    (

    CASE

    WHEN (GROUPING([MAT]) = 1) THEN

    'Tot'

    ELSE

    [MAT]

    END

    ) AS MAT,

    [myNUmber]

    FROM

    (

    SELECT DISTINCT

    CASE

    WHEN LEFT (TZZ, 2) = '1D'

    OR LEFT (TZZ, 2) = '1F' THEN

    'MAO'

    WHEN LEFT (TZZ, 2) = '1G'

    OR LEFT (TZZ, 2) = '1H' THEN

    'MAE'

    WHEN LEFT (TZZ, 2) = '1I'

    OR LEFT (TZZ, 2) = '1M'

    OR LEFT (TZZ, 2) = '1S'

    OR LEFT (TZZ, 2) = '1O' THEN

    'MAC'

    WHEN LEFT (TZZ, 2) = '1P'

    OR LEFT (TZZ, 2) = '1Q'

    OR LEFT (TZZ, 2) = '1R' THEN

    'MAS'

    END AS [MAT],

    COUNT (*) AS myNUmber]

    FROM

    dbo_40

    WHERE

    1 = 1

    AND LEFT (TZZ, 2) NOT LIKE 'LG%'

    ) AS SubQ

    WHERE

    1 = 1

    GROUP BY

    [MAT] WITH ROLLUP

    ORDER BY

    CASE [MAT]

    WHEN 'MAO' THEN

    1

    WHEN 'MAE' THEN

    2

    WHEN 'MAC' THEN

    3

    ELSE

    4

    END;

    Version SQL Server:

    9.00.1399.06RTMStandard Edition

  • You need to group by in the sub select as your using an aggregate function

    SELECT

    (

    CASE

    WHEN (GROUPING([MAT]) = 1) THEN

    'Tot'

    ELSE

    [MAT]

    END

    ) AS MAT,

    [myNUmber]

    FROM

    (

    SELECT DISTINCT

    CASE

    WHEN LEFT (TZZ, 2) = '1D'

    OR LEFT (TZZ, 2) = '1F' THEN

    'MAO'

    WHEN LEFT (TZZ, 2) = '1G'

    OR LEFT (TZZ, 2) = '1H' THEN

    'MAE'

    WHEN LEFT (TZZ, 2) = '1I'

    OR LEFT (TZZ, 2) = '1M'

    OR LEFT (TZZ, 2) = '1S'

    OR LEFT (TZZ, 2) = '1O' THEN

    'MAC'

    WHEN LEFT (TZZ, 2) = '1P'

    OR LEFT (TZZ, 2) = '1Q'

    OR LEFT (TZZ, 2) = '1R' THEN

    'MAS'

    END AS [MAT],

    COUNT (*) AS [myNUmber]

    FROM

    dbo_40

    WHERE

    1 = 1

    AND LEFT (TZZ, 2) <> 'LG'

    GROUP BY

    dbo_40.TZZ

    ) AS SubQ

    WHERE

    1 = 1

    GROUP BY

    [MAT] WITH ROLLUP

    ORDER BY

    CASE [MAT]

    WHEN 'MAO' THEN

    1

    WHEN 'MAE' THEN

    2

    WHEN 'MAC' THEN

    3

    ELSE

    4

    END;

  • SELECT

    CASE

    WHEN (GROUPING([MAT]) = 1) THEN 'Tot'

    ELSE [MAT] END

    AS MAT,

    --[myNUmber]

    COUNT (*) AS [myNUmber]

    FROM (

    SELECT DISTINCT

    CASE

    WHEN LEFT (TZZ, 2) IN ('1D','1F') THEN 'MAO'

    WHEN LEFT (TZZ, 2) IN ('1G','1H') THEN 'MAE'

    WHEN LEFT (TZZ, 2) IN ('1I','1M','1S','1O') THEN 'MAC'

    WHEN LEFT (TZZ, 2) IN ('1P','1Q','1R') THEN 'MAS'

    END AS [MAT]--,

    --COUNT (*) AS [myNUmber] -- missing left square bracket, no GROUP BY

    FROM dbo_40

    WHERE 1 = 1

    AND LEFT (TZZ, 2) <> 'LG' --NOT LIKE 'LG%'

    ) AS SubQ

    WHERE 1 = 1

    GROUP BY [MAT] WITH ROLLUP

    ORDER BY CASE [MAT]

    WHEN 'MAO' THEN 1

    WHEN 'MAE' THEN 2

    WHEN 'MAC' THEN 3

    ELSE 4

    END;

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (8/28/2012)


    SELECT

    CASE

    WHEN (GROUPING([MAT]) = 1) THEN 'Tot'

    ELSE [MAT] END

    AS MAT,

    --[myNUmber]

    COUNT (*) AS [myNUmber]

    FROM (

    SELECT DISTINCT

    CASE

    WHEN LEFT (TZZ, 2) IN ('1D','1F') THEN 'MAO'

    WHEN LEFT (TZZ, 2) IN ('1G','1H') THEN 'MAE'

    WHEN LEFT (TZZ, 2) IN ('1I','1M','1S','1O') THEN 'MAC'

    WHEN LEFT (TZZ, 2) IN ('1P','1Q','1R') THEN 'MAS'

    END AS [MAT]--,

    --COUNT (*) AS [myNUmber] -- missing left square bracket, no GROUP BY

    FROM dbo_40

    WHERE 1 = 1

    AND LEFT (TZZ, 2) <> 'LG' --NOT LIKE 'LG%'

    ) AS SubQ

    WHERE 1 = 1

    GROUP BY [MAT] WITH ROLLUP

    ORDER BY CASE [MAT]

    WHEN 'MAO' THEN 1

    WHEN 'MAE' THEN 2

    WHEN 'MAC' THEN 3

    ELSE 4

    END;

    Thanks a lot! ๐Ÿ™‚

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

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