Solved

MS Access -Multiple records to one record multiple fields

Posted on 2013-05-31
7
514 Views
Last Modified: 2013-05-31
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
Comment
Question by:IDontMeanToWAG
  • 4
  • 2
7 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39211241
Based on your sample above, what result would you expect?
0
 

Author Comment

by:IDontMeanToWAG
ID: 39211420
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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39211515
upload a db with the table.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39211541
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
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 39211558
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
 

Author Closing Comment

by:IDontMeanToWAG
ID: 39211853
Thank you so much!  You are a life saver!!
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39211906
Glad to help :)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question