Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to get only needed fields in the Group By Sql Query with Aggregate and with no aggregate function in Sql 2005? I tried following query with no aggregate function but can't group with needed fields only.
SQL
SELECT E.EmpId AS EMPID,E.CorporateId AS CORPID,

   E.FirstName+' '+E.LastName AS NAME, E.AccountNo,
  --[dbo].[fnMaskCardNo](E.CardNo) as CardNo,
    ES.Salary AS Amount,

    CASE WHEN  E.FollowMOLWorkFlow!=NULL THEN 'YES'
    ELSE 'NO' END  as WPS

  FROM EMPLOYEE E,CORPORATE C,EMPSALARY ES

        WHERE  (E.CorporateId='IBM') and
        ES.SALMONTH=3  and ES.SALYEAR=2014
          AND ES.Mode in('U')

        and C.IsDeleted=0

        GROUP BY E.CorporateId,E.EmpId,

 E.FirstName+' '+E.LastName,E.AccountNo, E.FollowMOLWorkFlow
Posted

1 solution

This won't work, for a number of reasons. First of all E.FollowMOLWorkFlow!=NULL will not work. You need to use is not null, as in E.FollowMOLWorkFlow is not null. Try writing a case statement that checks if null = null. It never does, so even if E.FollowMOLWorkFlow is null, it will not equal null.

Group by has no meaning in this query, as you're not using any methods that work on groups, such as count, sum or max. If you were to do that, you'd be able to do grouping with an over statement rather than have to use a group by, which then requires you to list all non aggregate fields.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900