简体   繁体   English

使用SQL查询计算总数

[英]calculate a total using SQL Query

I have a table with 3 attributes: 我有一个具有3个属性的表:

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).. 我正在尝试创建一个查询,该查询显示TotalNumberOfPublishedAuthors的总和和TotalNumberOfNonPublishedAuthors的总和(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. 如果您在表[Submission]中确实有名为[TotalNumberOfPublishedAuthors]和[TotalNumberOfAuthors]的列,则无需联接任何其他表。 Just perform the sums as you already have them in the query but without further tables. 只需执行总和即可,因为它们已经存在于查询中,但没有其他表。

SQL Fiddle SQL小提琴

MS SQL Server 2014 Schema Setup : MS SQL Server 2014架构设置

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 : 查询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 |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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