3

I am new to SQL. I am trying to get my query to display in the format below:

FacultyName          Book     Book chapter   Journal Article     Conference
CLM                   5            11              5                 1
Health Sciences       1            0               0                 0
Humanities            1            0               0                 0
Science               0            0               0                 3

At the moment, this is how it is displayed:

FacultyName              Type            CountOfSubmissionID
CLM                      Book                     5
CLM                      Book chapter             11
CLM                      Conference               1
CLM                      Journal article          5
Health Sciences          Book                     1
Humanities               Book                     1
Science                  Conference               3

This the query that got me this:

SELECT Faculty.FacultyName,
       Submission.Type,
       Count(Submission.SubmissionID) AS CountOfSubmissionID
FROM Submission
  INNER JOIN ((Faculty INNER JOIN School ON Faculty.FacultyID = School.[Faculty ID])
    INNER JOIN (Researcher INNER JOIN ResearcherSubmission
                  ON Researcher.ResearcherID = ResearcherSubmission.ResearcherID)
        ON School.SchoolID = Researcher.SchoolID)
      ON Submission.SubmissionID = ResearcherSubmission.SubmissionID
GROUP BY Faculty.FacultyName, Submission.Type

Here are my tables:

Table Structure

Please advice on how I can get my query to display in the format of the first table I have drawn up.

5
  • 2
    First of all, don't tag products not involved. Are you really using both MySQL and MS SQL Server? Commented Sep 24, 2015 at 11:47
  • 1
    Post the table structure here in SO, many people doesn't have dropbox access from offsite-onsite locations. I guess you should try PIVOT for your case. Commented Sep 24, 2015 at 11:47
  • i hope this will help you stackoverflow.com/questions/32334486/… Commented Sep 24, 2015 at 11:53
  • which sqlserver version? is it Sqlserver or oracle or else? Commented Sep 24, 2015 at 12:04
  • I am using Sqlserver Commented Sep 24, 2015 at 12:08

1 Answer 1

4

You can use CASE. Try this sentence in your code:

 SELECT Faculty.FacultyName, 
        SUM( CASE WHEN Submission.Type='Book' THEN 1 ELSE 0 END) Book,
        SUM( CASE WHEN Submission.Type='Book Chapter' THEN 1 ELSE 0 END) BookChapter,
        SUM( CASE WHEN Submission.Type='Conference' THEN 1 ELSE 0 END) Conference
 FROM Submission INNER JOIN ((Faculty INNER JOIN School ON Faculty.FacultyID = School.[Faculty ID]) INNER JOIN (Researcher INNER JOIN ResearcherSubmission ON Researcher.ResearcherID = ResearcherSubmission.ResearcherID) ON School.SchoolID = Researcher.SchoolID) ON Submission.SubmissionID = ResearcherSubmission.SubmissionID
 GROUP BY Faculty.FacultyName, Submission.Type;

Good Luck!

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, this works for me. however it doesn't group the faculty. eg: in the case of CLM, it displays it in 4 different rows, (each row showing data for Book, Book Chapter, Journal and Conference, independently). I appreciate your assistance though.
Sorry kivan!, remove submission.Type from group by. I am on my phone now

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.