0

I have a table with 3 attributes:

Title          TotalNumberOfAuthors              TotalNumberOfPublishedAuthors
A                    3                                         1
B                    2                                         2
C                    4                                         2
D                    2                                         1

I am trying to create a query that Displays the Sum of TotalNumberOfPublishedAuthors and the sum of TotalNumberOfNonPublishedAuthors which is (TotalNumberofAuthors - TotalNumberOfPublishedAuthors)..

This is the query that I have generated, but doesn't display the results as expected:

SELECT SUM(Submission.TotalNumberOfPublishedAuthors),        
       (SUM(Submission.TotalNumberOfAuthors) - SUM(Submission.TotalNumberOfPublishedAuthors)) AS Number_of_Non_Published_Authors
FROM Submission INNER JOIN ((Faculty INNER JOIN School 
ON Faculty.FacultyID = School.[FacultyID]) INNER JOIN (Researcher INNER JOIN ResearcherSubmission 
ON Researcher.ResearcherID = ResearcherSubmission.ResearcherID) 
ON School.SchoolID = Researcher.SchoolID) 
ON Submission.SubmissionID = ResearcherSubmission.SubmissionID;

This is the result I am trying to get:

TotalNumberofPublishedAuthors                     TotalNumberofPublishedAuthors
         6                                                     5
4
  • Your question is confusing. You say you have a table, then you show a query with multiple tables. Can you set up a SQL Fiddle with some sample data? Commented Sep 28, 2015 at 10:25
  • Also it would help to show the value that is being returned, or is the SQL just not working? Commented Sep 28, 2015 at 10:27
  • 1
    Looking at the query you have used this should give you the result as you expect. SUM(Submission.TotalNumberOfPublishedAuthors), (SUM(Submission.TotalNumberOfAuthors) - SUM(Submission.TotalNumberOfPublishedAuthors)) But as per you the result is different, what are you getting? Commented Sep 28, 2015 at 10:36
  • it looks like some problem with your join operation. check whether you are getting all row or not..... if you are getting all rows with your join, then definitely will get you correct result as you expected. Commented Sep 28, 2015 at 13:19

2 Answers 2

1
SELECT <Column Names>
FROM <Table Name>
UNION ALL
SELECT NULL,SUM(<Column name>),null,sum(<RunningTotal or AnyTotal>)
FROM <Table Name>
Sign up to request clarification or add additional context in comments.

Comments

1

If you really have columns in the table [Submission] called [TotalNumberOfPublishedAuthors] and [TotalNumberOfAuthors] as shown then there is no need to join any other tables. Just perform the sums as you already have them in the query but without further tables.

SQL Fiddle

MS SQL Server 2014 Schema Setup:

CREATE TABLE Submission
    ([Title] varchar(1), [TotalNumberOfAuthors] int, [TotalNumberOfPublishedAuthors] int)
;

INSERT INTO Submission
    ([Title], [TotalNumberOfAuthors], [TotalNumberOfPublishedAuthors])
VALUES
    ('A', 3, 1),
    ('B', 2, 2),
    ('C', 4, 2),
    ('D', 2, 1)
;

Query 1:

SELECT
      SUM(Submission.TotalNumberOfPublishedAuthors) AS TotalNumberOfPublishedAuthors
    , SUM(Submission.TotalNumberOfAuthors) 
    - SUM(Submission.TotalNumberOfPublishedAuthors) AS Number_of_Non_Published_Authors
FROM Submission

Results:

| TotalNumberOfPublishedAuthors | Number_of_Non_Published_Authors |
|-------------------------------|---------------------------------|
|                             6 |                               5 |

Comments

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.