简体   繁体   中英

How to use 'Count', based on a particular column in SQL

I have a query and which give the count greater than 1, but what I expect is I need the result to be based on particular column(Rollno.) How to achieve it.

Table Studies

   NAME            RollNo        DeptType      InternalStaff_1     InternalStaff_2
-----------     -----------     -----------    ---------------     --------------- 
   Anu              5            CompSci        Eve                  Antony
   Joy             13            Architecture   Elizabeth            George
   Adam             2            Mech           Grady                Lisa 
   Adam             2            Mech           Grady                Kim
   Anu              5            CompSci        Eve                  Antony

The below query gives me Count but not as expected

SELECT DISTINCT S.Name
, S.RollNo
, COUNT(S.RollNo) AS [Count]
, S.DeptType
, S.InternalStaff_1
, S.InternalStaff_2
FROM DataMining.dbo.Studies S
WHERE StartDate >= '20210325'--@StartDate
AND StartDate <= '20210407'--@EndDate
GROUP BY S.Name, S.RollNo, S.DeptType, S.InternalStaff_1, S.InternalStaff_2
HAVING COUNT(S.RollNo) > 1
ORDER BY RollNo

The query gave me the below result

  NAME            RollNo          Count         DeptType      InternalStaff_1     InternalStaff_2
-----------     -----------     -----------    -----------    ---------------     --------------- 
   Anu              5              2            CompSci        Eve                  Antony

But the expected result is

  NAME            RollNo          Count         DeptType      InternalStaff_1     InternalStaff_2
-----------     -----------     -----------    -----------    ---------------     --------------- 
   Anu              5              2            CompSci        Eve                  Antony
   Adam             2              2            Mech           Grady                NULL

As you can see the expected result is having a different InternalStaff_2 name for Adam which is not considered on the present result.

May I know how to over come this?

Note: I need the results to be displayed based on Rollno but I also need the InternalStaff_2 to be included in the result.

like this? I do not confirm your needs, but the internalstaff_2 column can refer to STRING_AGG() to replace the nested subquery in the following script.

SELECT DISTINCT S.Name
       , S.RollNo
       , COUNT(S.RollNo) AS [Count]
       , S.DeptType
       , S.InternalStaff_1
       , (SELECT CASE WHEN COUNT(DISTINCT InternalStaff_2) = 1 THEN MIN(InternalStaff_2) ELSE null END
          FROM #temp AS i
          WHERE i.NAME = S.NAME and i.RollNo = S.RollNo and i.DeptType = S.DeptType and i.InternalStaff_1 = S.InternalStaff_1) as InternalStaff_2
FROM #temp S
GROUP BY S.[Name], S.RollNo, S.DeptType, S.InternalStaff_1
HAVING COUNT(S.RollNo) > 1
ORDER BY RollNo

Hmmm. . . If I understand correctly, you want NULL if the internal staff columns do not match. That would be:

SELECT S.Name, S.RollNo, COUNT(*) AS [Count], S.DeptType,
       (CASE WHEN MIN(S.InternalStaff_1) = MAX(S.InternalStaff_1) THEN MIN(S.InternalStaff_1) END) as InternalStaff_1,
       (CASE WHEN MIN(S.InternalStaff_2) = MAX(S.InternalStaff_2) THEN MIN(S.InternalStaff_2) END) as InternalStaff_2
FROM DataMining.dbo.Studies S
WHERE StartDate >= '20210325' AND --@StartDate
      StartDate <= '20210407'     --@EndDate
GROUP BY S.Name, S.RollNo, S.DeptType
HAVING COUNT(*) > 1
ORDER BY RollNo;

Here is a db<>fiddle that shows that this basically works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM