SQL71640: COUNT_BIG(a) is required

aadm_GAdams 0 Reputation points
2023-06-12T02:19:01.63+00:00

I have COUNT_BIG(**) in the script and I got the "SQL71640: COUNT_BIG(a) is required" error.*

enter image description here

SQL Server | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Dan Guzman 9,406 Reputation points
    2023-06-12T11:39:33.3266667+00:00

    This error will occur when you have an indexed view with a GROUP BY clause and the view SELECT clause does not include a COUNT_BIG(*). This is one of the requirements for indexed (materialized) views in SQL Server.

    Ensure the view includes COUNT_BIG(), not COUNT(). For example:

    CREATE VIEW dbo.ExampleView

    WITH SCHEMABINDING

    AS

    SELECT

    Column1

    ,COUNT_BIG(*) AS row_count

    FROM dbo.ExampleTable

    GROUP BY Column1;

    1 person found this answer helpful.

  2. Dan Guzman 9,406 Reputation points
    2023-06-16T15:29:21.0866667+00:00

    I see from the view you provided that it contains several SUMs of various expressions like:

    	, SUM(CAST (ISNULL(Column_Name1, 0) AS BIGINT)) AS Column_Name1
    	, SUM(CAST (ISNULL(Column_Name2, 0) AS MONEY)) AS Column_Name2
    

    The error message indicates that a COUNT_BIG() is also needed on the same expression as the SUM function. You can avoid the error by adding those to the SELECT list along with COUNT(BIG*) like below:

    	, SUM(CAST (ISNULL(Column_Name1, 0) AS BIGINT)) AS Column_Name1
    	, SUM(CAST (ISNULL(Column_Name2, 0) AS MONEY)) AS Column_Name2
    	, COUNT_BIG(CAST(ISNULL(Column_Name1, 0) AS BIGINT)) AS Column_Name1_CountBig
    	, COUNT_BIG(CAST(ISNULL(Column_Name2, 0) AS MONEY)) AS Column_Name2_CountBig
        , COUNT_BIG(*) AS CountBig
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.