Case statement and group by clause in function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sam hank
    New Member
    • Feb 2012
    • 2

    Case statement and group by clause in function

    Hi,
    in this function, only using @c to decide whether to sum over the [ddd] column. i am doing it in this way but i am getting error.
    Code:
    CREATE FUNCTION abc 
    (	
    @Date_a datetime = '01/01/2010',
    @Date_b datetime = '01/01/2012,
    @c bit   = 0
    )
    RETURNS 
    @ret TABLE 
    (
    [aaa]	[VARCHAR](100),
    [bbb]   [DATETIME],
    [ccc]   [DATETIME]
    [ddd]   [VARCHAR](80),
    [ID]	[INT] IDENTITY(1,1)
    PRIMARY KEY CLUSTERED (
    	[bbb],
    	[ID]
    )
    )
    AS BEGIN
    INSERT into @ret (
    		[aaa],
    		[bbb],
    		[ccc],
    		[ddd]
    	)
    SELECT
    	'abc',
    	[bbb],
    	[ccc],
    	[ddd] = CASE @c WHEN 1 THEN [ddd] ELSE '' END
    		FROM xyz(@Date_a, @Date_b, null, null)
    		GROUP BY [aaa], [bbb],[ccc]
    		CASE @c WHEN 1 THEN [ddd] ELSE '' END
    RETURN	
    
    END
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Remove the [ddd] = part, that's only used to set variables and you're not trying to set a variable. You're also missing a comma in your group by clause after your [ccc] field.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      Your SQL is nicely laid out (readable) Sam, but you didn't include the error message. Try Rabbit's suggestions, and if they don't work, consider coming back with a post that includes the error message.

      Comment

      • mohit2k8
        New Member
        • Feb 2012
        • 5

        #4
        I think you have made some syntactical errors, those i have removed in given below code.
        Code:
        Create FUNCTION abc 
        (    
        @Date_a datetime = '01/01/2010',
        @Date_b datetime = '01/01/2012',
        @c bit = 0
        )
        RETURNS 
        @ret TABLE 
        (
        [aaa]   [VARCHAR](100),
        [bbb]   [DATETIME],
        [ccc]   [DATETIME],
        [ddd]   [VARCHAR](80),
        [ID]    [INT] IDENTITY(1,1)
        PRIMARY KEY CLUSTERED (
            [bbb],
            [ID]
        )
        )
        AS BEGIN
        INSERT into @ret (
                [aaa],
                [bbb],
                [ccc],
                [ddd]
            )
        SELECT
            'abc',
            [bbb],
            [ccc],
            [ddd] = CASE @c WHEN 1 THEN [ddd] ELSE '' END
                FROM xyz(@Date_a, @Date_b, null, null)
                GROUP BY [aaa], [bbb],[ccc],
                CASE @c WHEN 1 THEN [ddd] ELSE '' END
        RETURN    
         
        END
        Last edited by NeoPa; Feb 22 '12, 01:56 PM. Reason: Added mandatory [CODE] tags for you

        Comment

        • sam hank
          New Member
          • Feb 2012
          • 2

          #5
          i have removed the [ddd]= part from the select, still it is showing error.
          -----
          Error Message: Msg 8120, Level 16, State 1, Procedure abc, Line 69
          Column 'abc.ddd' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
          ------
          also the case in group by clause, will it consider the '' after the "[ccc]," if the value is other than 1 ?
          Code:
           GROUP BY [aaa], [bbb],[ccc], 
           CASE @c WHEN 1 THEN [ddd] ELSE '' END

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32633

            #6
            That error message doesn't match the SQL we've seen, with or without the changes you've described.

            Try this :
            Code:
            SELECT   'abc'
                   , [bbb]
                   , [ccc]
                   , CASE @c WHEN 1 THEN [ddd] ELSE '' END AS [New_ddd]
            FROM     xyz(@Date_a, @Date_b, null, null)
            GROUP BY [aaa]
                   , [bbb]
                   , [ccc]
                   , CASE @c WHEN 1 THEN [ddd] ELSE '' END
            You may even get away with a last line of the following as it's T-SQL :
            Code:
                   , [New_ddd]
            Last edited by NeoPa; Feb 23 '12, 06:04 PM. Reason: Fixed error in SQL.

            Comment

            • mohit2k8
              New Member
              • Feb 2012
              • 5

              #7
              In the above code, it may be a case after [ccc] it will get '' when value of @c is something else rather than 1.

              Comment

              Working...