Declare variable

I get an error message:

Msg 164, Level 15, State 1, Line 18
Each GROUP BY expression must contain at least one column that is not an outer reference

  • Create Azure web site with Django and SQL server
  • What is API Usage?
  • MS SQL: Call function with parameter from linked Oracle server
  • How to stop a cpu/time consuming running query on SQL Server!
  • How do I use a computed column specification to persist a datetime value using getdate()?
  • Is this stored procedure thread-safe? (or whatever the equiv is on SQL Server)
  • from this T-SQL code:

    DECLARE @CLIENT_COUNT INT
    
    SELECT @CLIENT_COUNT = COUNT(CLT_NBR) FROM CLIENT
    
    SELECT 
        CASE 
           WHEN STATUS = 3 
              THEN 'CATEGORY1' 
              ELSE 'CATEGORY2' 
        END AS Category,
        COUNT(*) AS COUNT,
        @CLIENT_COUNT as [Total CLIENT],
        COUNT(*) / @CLIENT_COUNT as PERCENTAGE
    FROM 
        CLIENT_STATUS
    WHERE 
        STATUS IN (3, 8)
    GROUP BY 
        STATUS, @CLIENT_COUNT 
    

    Can you help me to fix it?

    Thank you!

    One Solution collect form web for “Declare variable”

    Your syntax is wrong. You need to put all of the values that need to be returned before FROM.

    DECLARE @CLIENT_COUNT INT
    
    SELECT @CLIENT_COUNT = COUNT(CLT_NBR)
    FROM CLIENT
    
    SELECT CASE 
        WHEN STATUS = 3
            THEN 'CATEGORY1'
        ELSE 'CATEGORY2'
        END AS Category
         , COUNT(STATUS) AS StatusCount
         , @CLIENT_COUNT AS [Total CLIENT]
         , COUNT(STATUS) / @CLIENT_COUNT AS PERCENTAGE
    FROM CLIENT_STATUS
    WHERE STATUS IN (
           3
          ,8
        )
    GROUP BY STATUS
    
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.