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
SELECT <Column Names>
FROM <Table Name>
UNION ALL
SELECT NULL,SUM(<Column name>),null,sum(<RunningTotal or AnyTotal>)
FROM <Table Name>
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.
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 |
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.