我可以将两列合并为一列,然后像一列一样计算它们的值吗?
我当前正在使用此 SQL 语句返回完成每条记录的计数 (RFI):
SELECT Completed_By, COUNT(DISTINCT Unique_ID) AS RFICount FROM RFI_
WHERE Date_Submitted BETWEEN '20110101' AND '20110630'
GROUP BY Completed_By
这将返回如下所示的数据集:
Completed_By RFICount
------------ ---------
SMITH, Bob 1
DOE, John 15
WARNER, Jack 11
这就是我希望它的外观。但是,某人可以是完成或协助完成记录 (RFI) 的主要或次要人员。我想在一栏中显示这一切,无论他们是完成记录 (Completed_By) 还是协助记录 (Assisting_Analyst)。
当我使用以下 SQL 语句时,当它们需要聚合时,它将显示为单独的:
SELECT Completed_By, Assisting_Analyst, COUNT(DISTINCT Unique_ID) AS RFICount
FROM RFI_ WHERE Date_Submitted BETWEEN '20110101' AND '20110630'
GROUP BY Completed_By, Assisting_Analyst
这将返回以下类型的输出:
Completed_By Assisting_Analyst RFICount
------------ ----------------- --------
SMITH, Bob NULL 1
DOE, John NULL 15
WARNER, Jack NULL 7
WARNER, Jack SMITH, Bob 4
由于 Bob Smith 协助 Jack Warner 处理了四个记录 (RFI),因此创建了一个单独的行。但我只是希望每个人都获得个人信用并将其显示在一列中,如下所示:
Completed_By RFICount
------------ ---------
SMITH, Bob 5
DOE, John 15
WARNER, Jack 11
在上面的示例中,他们共同处理的四条记录都归功于他们两人。当然,这意味着某些记录会被计算两次,但这正是我想要的应用程序。
我已经尝试过 UNIONS 和一堆其他东西,但它要么没有给我我想要的东西,因为我想要它显示,要么它给我一个没有数据的错误。我知道 DISTINCT 必须消失,但不知道具体如何做到这一点。这是在 SQL Server 2008 R2 中完成的。帮助?
I am currently using this SQL statement to return a count by who completed each record (RFI):
SELECT Completed_By, COUNT(DISTINCT Unique_ID) AS RFICount FROM RFI_
WHERE Date_Submitted BETWEEN '20110101' AND '20110630'
GROUP BY Completed_By
This returns a data set like the following:
Completed_By RFICount
------------ ---------
SMITH, Bob 1
DOE, John 15
WARNER, Jack 11
This is how I want it to look. However, someone can be the primary or secondary person who completed or assisted in the completion of a record (RFI). I want to show this all in one column, regardless of whether they completed the record (Completed_By) or assisted with it (Assisting_Analyst).
When I use the following SQL statement, it shows these as separate when they need to be aggregated:
SELECT Completed_By, Assisting_Analyst, COUNT(DISTINCT Unique_ID) AS RFICount
FROM RFI_ WHERE Date_Submitted BETWEEN '20110101' AND '20110630'
GROUP BY Completed_By, Assisting_Analyst
This returns the following type of output:
Completed_By Assisting_Analyst RFICount
------------ ----------------- --------
SMITH, Bob NULL 1
DOE, John NULL 15
WARNER, Jack NULL 7
WARNER, Jack SMITH, Bob 4
Since Bob Smith assisted Jack Warner with four records (RFIs), there is a separate row created. But I simply want each to get individual credit and have it show up in one column, like so:
Completed_By RFICount
------------ ---------
SMITH, Bob 5
DOE, John 15
WARNER, Jack 11
In the above example, the four records they worked on jointly are credited to both of them. Granted, this means some records are being counted twice, but that is what I want for this application.
I have tried UNIONS and a bunch of other stuff but it either doesn't give me what I want as I want it displayed or it gives me an error with no data. I know DISTINCT has got to go, but not sure how to do this exactly. This is being done in SQL Server 2008 R2. Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
做到这一点的最佳方法可能是使用汇总,它避免了子查询,并且可能在大型数据集上表现更好。
ROLLUP 意味着总值应包含在 Completed_By 行中,因此排除了 where 子句中的 Assisting 行。
The best way of doing this is probably with a rollup, which avoids the sub-query and would probably perform better on a large datasets.
The ROLLUP means that the total value should be included in the Completed_By line, hence the exclusion of the Assisting line in the where clause.
另一种尝试:
Another try: