I am having a bit of a grey-matter obstacle on what seems, at least to me, to be a fairly easy BAQ task.
I’d like to get all of our distinct parts, by ascending part number and perform a simple count of [1] how many times they appear in upcoming scheduled jobs and [2] how many times they have been placed on sales orders with a future request date. The date range would be parameterized so that the person running the BAQ can select how far out they wish to look.
On the surface, this seems pretty easy to configure, but for some reason, I’m struggling with how to approach this.
I had originaly started with the Erp.Part table; then, believing that the counts for jobs and sales orders would not calculate unless Erp.JobHead and Erp.OrderDtl were in separate subqueries, I linked those tables back to Erp.Part in their own subquery. This may, or may not be sensible and someone may want to provide me with some guidance there.
As far as the counts are concerned, I attempted to partition the counts over the Erp.Part.PartNum field because these were to be distinct counts for each part:
COUNT(1) OVER(PARTITION BY Part.PartNum)
… but I’m not exactly sure that this is the best way.
As far as returned values (or output), all I’m really looking for is the distinct part number list along with the separate counts as outlined above. Perhaps, like so:
(Let's assume this is for a 02/01/2019 to 05/01/2019 date range):
PartNum TotalJobs TotalOrders
------- --------- -----------
A-1 9 2
A-2 0 1
B-1 13 0
C-1 41 1
C-2 6 30
D-1 2 8
I’m having some trouble knowing if this approach is sound/sensible - and - actually getting the counts in the same query. It seems every time I try to separately link the Erp.JobHead and the Erp.OrderDtl subqueries back to Erp.Part, the output becomes “wonky” and doesn’t validate against SQL query validation.
What advice/guidance might there be for obtaining counts of part numbers that are expected to exist in two different tables?