• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 526
  • Last Modified:

MS Access -Multiple records to one record multiple fields

I have a table that lists multiple records for the same store, with varying values for Code1 and Code2.  

Store Nbr                 Count      Code1      Code2
15291            3      COB7      
15291            93      COB7      COB7
15293            1      CO172      
15293            3      CO172      CO172
15293            2      CO172      OA18
15293            2      COB7      
15293            1      COB7      CO97
15293            23      COB7      COB7
15294            13      COB7      COB7
15296            3      CO172      OA18
15307            1            COB7
15307            122      COB7      COB7
15307            1      COB7      OA18

I am wanting to take this information and make a separate table that lists each store only once, with each unique code as a field on that record and the count totalled.

How do I do that?
0
IDontMeanToWAG
Asked:
IDontMeanToWAG
  • 4
  • 2
1 Solution
 
Patrick MatthewsCommented:
Based on your sample above, what result would you expect?
0
 
IDontMeanToWAGAuthor Commented:
I am hoping for results like this.


Store      Total            Code1         Code2         Code3       Code4      Code5       Code6
15291      96               COB7
15293      32               CO172       OA18           COB7        CO97
15294      13               COB7
15296       3                COB7         OA18
15307      124             COB7         OA18

I would never expect one store to have more than 8 codes.
0
 
Rey Obrero (Capricorn1)Commented:
upload a db with the table.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Patrick MatthewsCommented:
Would having a single column for the codes concatenated together be OK?  I.e.:

Store      Total            Codes
15291      96               COB7
15293      32               CO172, OA18, COB7, CO97
15294      13               COB7
15296       3                COB7, OA18
15307      124             COB7, OA18
0
 
Patrick MatthewsCommented:
Ugly, but do-able:

TRANSFORM First(z.Code) AS FirstOfCode
SELECT z.StoreNbr, z.Total
FROM (
SELECT x.StoreNbr, x.Code, DSum("[Count]", "[SomeTable]", "[StoreNbr] = " & x.StoreNbr) AS Total, Count(y.Code) AS Ordinal
FROM
(SELECT StoreNbr, Code1 AS Code
FROM SomeTable
WHERE Code1 Is Not Null
UNION
SELECT StoreNbr, Code2 AS Code
FROM SomeTable
WHERE Code2 Is Not Null) AS x INNER JOIN
(SELECT StoreNbr, Code1 AS Code
FROM SomeTable
WHERE Code1 Is Not Null
UNION
SELECT StoreNbr, Code2 AS Code
FROM SomeTable
WHERE Code2 Is Not Null) AS y ON x.StoreNbr = y.StoreNbr And x.Code >= y.Code
GROUP BY x.StoreNbr, x.Code
) AS z
GROUP BY z.StoreNbr, z.Total
PIVOT "Code " & z.Ordinal;

Open in new window


Q-28144229.mdb
0
 
IDontMeanToWAGAuthor Commented:
Thank you so much!  You are a life saver!!
0
 
Patrick MatthewsCommented:
Glad to help :)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now