I have 4-6 tables, but the main ones are LRTTable and TempTable. They are joined using INNER JOIN and result set is LEFT JOIN with other 6 tables.
LRTable and Temp Table contains Store ID ,PCC Id ,COF NO, Line of Business Name, Status, Address and few other columns. LRTable can have duplicate records for same Store ID with different COF No(0,7987987) and Line of Business Name(Food and Retail).
Temp Table has Store ID, PCC. Combination of these two are primary key in Access.
Query is correctly able to pull all the data from LRTable based on TempTable. For example, query pulls all the records (including duplicates)from LRtable when there is match in Temp Table and are sorted based on status code and line of business. By doing so I am ensuring the active and food records are available on the top.
What I need to achieve is, instead of having multiple records for each store in the query result set, I want only top record from set of records of each store from the result set. I am able to this with the first function, however its picking values fron LRTable rather than sub query result
Is there a way I can just pick from sub query ?
LRTable:
Store ID PCC ID Status Line of Business Name COF No
---------- -------- ---------- -----------------------
1 123 Active Food 999
1 123 InActive Food 89899
1 123 Active Retail 0
2 222 Active Retail 0
2 222 InActive food 76767
TempTable
Store ID : 1 and 2
PCC : 123 and 222
Result:
- 1,0
- 2,0
Expected output:
- 1,999
- 2,76767
Below is the query with first
SELECT StoreID,FIRST (COF No) from
(
select * from
(
SELECT distinct TempTable.[Avendra ID],
TempTable.[Marsha Code],
LRTable.[Concept Name],
LRTable.[Outlet (Store) Name],
LRTable.[Outlet Street Address Text ID],
LRTable.[Outlet City ID],
LRTable.[Outlet State ID],
LRTable.[Outlet Zip Code ID],
LRTable.[Status Code ID],
LRTable.[Outlet SAP ID ID],
LRTable.[Outlet PCC ID ID],
LRTable.[COF Number ID],
CLPSource.[Brand Mandated],
FROM (
(
(
(
(
(
LRTable INNER JOIN TempTable
ON LRTable.[Outlet PCC ID ID] = TempTable.[PCC ID]
AND LRTable.[Store Number ID] = TempTable.[Store Number ID]
) LEFT JOIN CLPSource
ON TempTable.[Avendra ID] = CLPSource.[Avendra Customer ID]
) LEFT JOIN OasisReportSource
ON TempTable.[Marsha Code] = OasisReportSource.[Marsha])
ORDER BY LRTable.[Status Code ID],
LRTable.[Line of Business Name]))
Group by LRTable.[StoreID]