计算多个加入MS SQL 2016的不同值
我试图从连接3个表的查询中计算出不同值的输出。
这是我到目前为止正在运行的查询(MS SQL 2016 STD):
SELECT
f.factor_desc,
pf.value,
p.measure_date
FROM
gagestation.dbo.part p
INNER JOIN gagestation.dbo.part_factor pf ON
p.part_id = pf.part_id
INNER JOIN gagestation.dbo.qcc_file qf ON
p.qcc_file_id = qf.qcc_file_id
INNER JOIN gagestation.dbo.factor f ON
pf.factor_id = f.factor_id
WHERE
factor_desc = 'Work Order #'
Order BY pf.value
此查询输出记录如下:
Work Order # / Date last updated
Work Order # 26724/1 2021-06-22 10:57:35
Work Order # 26724/1 2021-06-23 04:51:47
Work Order # 26724/1 2021-06-23 13:19:42
Work Order # 26724/1 2021-06-24 04:36:43
Work Order # 26724/1 2021-06-24 07:49:51
Work Order # 26724/1 2021-06-24 09:49:48
Work Order # 26724/1 2021-06-24 12:48:58
Work Order # 26724/1 2021-06-25 05:30:01
希望使其看起来像:
Workorder# / TotalCount
26724/1 / 11
34553/5 / 2
31556/2 / 5
它具有2列,其中一个与工作列表#,另一个具有行总数对于该工作器,通过计算pf.value列中其数字的发生。
我试图在各种配置中添加计数/扩展,最终都在这种变化中失败:
包含在组中
在选择列表中无效,因为它不包含在汇总函数中,或者由子句
我希望获取此信息并通过链接的服务器将其推到另一个数据库中,我知道如何确实计算出不同的值,但是我尝试过的一切都以错误结束。
I seek to count the output of DISTINCT values from a query that joins 3 tables.
Here is the query (MS SQL 2016 STD) I am running with so far:
SELECT
f.factor_desc,
pf.value,
p.measure_date
FROM
gagestation.dbo.part p
INNER JOIN gagestation.dbo.part_factor pf ON
p.part_id = pf.part_id
INNER JOIN gagestation.dbo.qcc_file qf ON
p.qcc_file_id = qf.qcc_file_id
INNER JOIN gagestation.dbo.factor f ON
pf.factor_id = f.factor_id
WHERE
factor_desc = 'Work Order #'
Order BY pf.value
This query outputs records like this:
Work Order # / Date last updated
Work Order # 26724/1 2021-06-22 10:57:35
Work Order # 26724/1 2021-06-23 04:51:47
Work Order # 26724/1 2021-06-23 13:19:42
Work Order # 26724/1 2021-06-24 04:36:43
Work Order # 26724/1 2021-06-24 07:49:51
Work Order # 26724/1 2021-06-24 09:49:48
Work Order # 26724/1 2021-06-24 12:48:58
Work Order # 26724/1 2021-06-25 05:30:01
Looking to get this to look like:
Workorder# / TotalCount
26724/1 / 11
34553/5 / 2
31556/2 / 5
It has 2 columns one with the workorder# and the other with the total count of rows for that workorder by counting the occurances of its number in the pf.value column.
I have tried to add Count/Distict in various configurations and all end up failing in variations of this:
is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
I am looking to take this info and push it to another database via a linked server and I know how to do counts for distinct values but everything I have tried has ended in an error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要介绍
count()
函数的使用,您还需要使用子句的组,以便为
f中的每个唯一组合执行计数.factor_desc
和pf.value
。在计数函数内部,您可以使用不同的
来限定计数的执行方式。NB:您可以从
选择
和组中删除列
f.factor_desc
,如果您不希望该列在最终的最终结果。对于此特定查询,可能并不需要使用所有4个表 - 但是没有数据进行测试只是可能性。
To introduce use of the
COUNT()
function you need to also use aGROUP BY
clause, so that the count is performed for each unique combination of values withinf.factor_desc
andpf.value
. Inside the count function you can then useDISTINCT
to qualify how the count is performed.nb: You can remove the column
f.factor_desc
from both theselect
andgroup by
clauses if you don't want that column in the final result.For this specific query it might not be necessary to use all 4 tables - but without data to test with this is merely a possibility.