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.