0

Good day again to all SQL guru here :)

I have a problem querying my table using COUNT.

This is the data inside of the table (tblstudentoffense)

enter image description here

And this is the RESULT that I want (Let's say that there is a new column after the offenseType that will count the Stealing and Gaming Offense

enter image description here

Is this possible? If possible kindly add an explanation with your provided code. Thanks :)

6
  • Please read postgresql-performance then edit your question and provide the missing information. Commented Feb 23, 2017 at 7:05
  • 1
    What sort of data is hiding behind the blacked out squares? Commented Feb 23, 2017 at 7:06
  • @a_horse_with_no_name i'm using MySQL by phpMyAdmin Commented Feb 23, 2017 at 7:08
  • @TimBiegeleisen I just hide the name of the owner just a respect... you can see the Student ID all data are the same but difference offenseType. Commented Feb 23, 2017 at 7:09
  • 2
    Your expected output does not make sense. Which value of offenseFN should be displayed for a given combination of studentID and offenseType? Commented Feb 23, 2017 at 7:10

4 Answers 4

1

Using subquery to get count and concatenate count with main select statement

SELECT DISTINCT OffenseType + ' ' + CAST(B.Cnt AS VARCHAR) ,  
Othercolumn1,Othercolumn2
FROM Your_tableName A
JOIN 
(
  SELECT COUNT(*) Cnt , StudentId
  FROM Your_tableName
  GROUP BY StudentId 
) B ON B.StudentId = A.StudentId
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't match the expected output.
I'll try this one later. Thanks for the reply.
0

Try using CONCAT_WS().The CONCAT_WS() function is used to join two or more strings with a separator.

Note: offensetypeCount is an new column having expected output.

SELECT *,CONCAT_WS(' ',offensetype,count(offenseType) AS offensetypeCount FROM tblstudentoffense GROUP BY offensetype;

4 Comments

Is * in select possible when you have only offensetype field in groupby?
I don't think the fields which are not in group by cannot be fetched using select clause
@Mark did you try this.
I'll try this one too later. Thanks for the reply. (I'm busy at the moment)
0

Is this what you are looking for:

 WITH SRC AS  
    (SELECT 'STEALING' OFFENSETYPE,'1' STUDENTID FROM DUAL UNION ALL 
     SELECT 'STEALING' OFFENSETYPE,'1' STUDENTID FROM DUAL UNION ALL
     SELECT 'STEALING' OFFENSETYPE,'1' STUDENTID FROM DUAL UNION ALL
     SELECT 'STEALING' OFFENSETYPE,'1' STUDENTID FROM DUAL UNION ALL
     SELECT 'GAMING' OFFENSETYPE,'1' STUDENTID FROM DUAL UNION ALL)  
     SELECT OFFENSETYPE,STUDENTID,COUNT(*) FROM SRC GROUP BY OFFENSETYPE,STUDENTID;

1 Comment

I'll try this too later. Thanks for the reply.
0

You could try

SELECT CONCAT(offensetype, ' ', COUNT(offensetype)) AS offenseType, 
       studentid, 
       offenseln, 
       offensefn, 
       offensemn, 
       offensesuffixname 
FROM   tblstudentoffense
GROUP  BY offensetype, 
          studentid, 
          offenseln, 
          offensefn, 
          offensemn, 
          offensesuffixname; 

I've created a sample at this link http://rextester.com/WZGW41761.

2 Comments

I'll try this one too later. Thanks for the reply.
based on your example. Your code is fit on result that I want :-) to all who gave their answers I will test them all but for now I will use Trung Duong code since I understand a little of his code :-) Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.