T
Tokyo Alex
Dear all,
I have two tables, POs and Orders. POs represent purchase orders from our
client, and Orders represent internal orders. One PO can be related to many
orders.
Each order has a Sales Rep (data in a different table, ID in Orders as FK).
The PO number is not a unique ID in the client order data (as one PO can
cover many separate client orders).
Graphically, the structure looks something like this (* indicates a unique
ID):
POs Orders Reps
Client Order ID* ORDER ID*
PO Code <---------> PO NUM
SALES REP ID <------- SALES REP ID*
In theory, each PO should relate to one and only one SR. However, it seems
that the client may be reusing old PO numbers with different SRs.
I created the following query to identify whether this is actually happening:
SELECT PONUM, REPNUM
FROM
(SELECT [POs].[PO Code] AS PONUM, [Orders].[SALES REP ID] AS REPNUM
FROM [POs] INNER JOIN [Orders] ON [POs].[PO Code] = [Orders].[PO NUM]
GROUP BY [POs].[PO Code], [Orders].[SALES REP ID]) AS Q
GROUP BY PONUM, REPNUM
HAVING count(PONUM) > 1;
My logic:
The subquery generates unique combinations of PO Number and SR, and the
outer query identifies any duplicates of PO Number. (i.e. PONUM:REPNUM =/=
1:1)
My questions:
1) Does this code actually do what I think it does?
2) Am I reinventing the wheel here?
Any advice or suggestions will be much appreciated.
Thanks,
Alex.
I have two tables, POs and Orders. POs represent purchase orders from our
client, and Orders represent internal orders. One PO can be related to many
orders.
Each order has a Sales Rep (data in a different table, ID in Orders as FK).
The PO number is not a unique ID in the client order data (as one PO can
cover many separate client orders).
Graphically, the structure looks something like this (* indicates a unique
ID):
POs Orders Reps
Client Order ID* ORDER ID*
PO Code <---------> PO NUM
SALES REP ID <------- SALES REP ID*
In theory, each PO should relate to one and only one SR. However, it seems
that the client may be reusing old PO numbers with different SRs.
I created the following query to identify whether this is actually happening:
SELECT PONUM, REPNUM
FROM
(SELECT [POs].[PO Code] AS PONUM, [Orders].[SALES REP ID] AS REPNUM
FROM [POs] INNER JOIN [Orders] ON [POs].[PO Code] = [Orders].[PO NUM]
GROUP BY [POs].[PO Code], [Orders].[SALES REP ID]) AS Q
GROUP BY PONUM, REPNUM
HAVING count(PONUM) > 1;
My logic:
The subquery generates unique combinations of PO Number and SR, and the
outer query identifies any duplicates of PO Number. (i.e. PONUM:REPNUM =/=
1:1)
My questions:
1) Does this code actually do what I think it does?
2) Am I reinventing the wheel here?
Any advice or suggestions will be much appreciated.
Thanks,
Alex.