473,692 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combine multiple records into one row with multiple columns [SOLVED]

63 New Member
I need to combine multiple records into one row with multiple columns for those records. Very similar to a crosstab query, but one that shows all the records, not a summary of them.

For example, if the table is like this:

Expand|Select|Wrap|Line Numbers
  1. Name     Month     Score
  2. John     Jan       92
  3. John     Jan       90
  4. John     Feb       97
  5. John     Feb       91
I want a query that gives these results like this:

Expand|Select|Wrap|Line Numbers
  1. Month     Name     Score1     Score2
  2. Jan       John     92         90
  3. Feb       John     97         91
Crosstab queries are set for summarizing data, but I don't want that--I just want ALL the scores laid out in a row for each person for each month.

I'm using Access 2007 and in this case, VBA is not an option.

Any help would be much appreciated.

EDIT: See complete solution down below.
Feb 18 '15 #1
27 16182
Seth Schrock
2,965 Recognized Expert Specialist
Can there be more than two scores per person per month? There is a SQL solution for that, but the more scores, the harder it is to code.
Feb 18 '15 #2
63 New Member
It would be a maximum of 4 scores.
Feb 18 '15 #3
Seth Schrock
2,965 Recognized Expert Specialist
Just an FYI, it is not a good idea to use Name or Month as field names as those are reserved words in Access. Access does permit you to use them, but it can sometimes cause problems.

Anyway, we first need to have an id field in your table. An autonumber field would be perfect for this. Then we need to create a querydef that puts a number beside each record to tell us if it is score 1, 2, 3 or 4. I'll assume a table name of tblScores.
Expand|Select|Wrap|Line Numbers
  2. , [Name]
  3. , [Month]
  4. , Score
  5. , DCount("*"
  6.     , "tblScores"
  7.     , "[Name] = '" & [Name] & 
  8.         "' And [Month] = '" & [Month] & 
  9.         "' And ID <= " & ID) 
  10.     As ScoreNum
  11. FROM tblScores
This should produce the results
Expand|Select|Wrap|Line Numbers
  1. ID    Name    Month    Score    ScoreNum
  2. 1     John    Jan      92       1
  3. 2     John    Jan      90       2
  4. 3     John    Feb      97       1
  5. 4     John    Feb      91       2
Save this query as qryScores. Now the query that gets the results that you want will have several subqueries to get the score numbers based on the ScoreNum field.
Expand|Select|Wrap|Line Numbers
  2. , MQ.[Name]
  3. , MQ.[Month]
  4. , S1.Score As Score1
  5. , S2.Score As Score2
  6. , S3.Score As Score3
  7. , S4.Score As Score4
  8. FROM qryScores As MQ 
  9. LEFT JOIN (SELECT ID, Score FROM qryScores WHERE ScoreNum = 1) As S1 ON MQ.ID = S1.ID
  10. LEFT JOIN (SELECT ID, Score FROM qryScores WHERE ScoreNum = 2) As S2 ON MQ.ID = S2.ID
  11. LEFT JOIN (SELECT ID, Score FROM qryScores WHERE ScoreNum = 3) As S3 ON MQ.ID = S3.ID
  12. LEFT JOIN (SELECT ID, Score FROM qryScores WHERE ScoreNum = 4) As S4 ON MQ.ID = S4.ID
Feb 18 '15 #4
1,107 Recognized Expert Top Contributor
Nice Work! That is probably preferable to a Crosstab as you will have a predefined amount of columns.

But if you wanted to, since you already did the hard work of creating a RowNumber type column, you could use a Crosstab ;)
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM First(qryScores.[Month]) AS Scores
  2. SELECT qryScores.[Name], First(qryScores.[Month]) AS ReportMonth
  3. FROM qryScores
  4. GROUP BY qryScores.[Name], qryScores.[Month]
  5. PIVOT qryScores.[ScoreNum]
Feb 18 '15 #5
63 New Member
Thanks, Seth.

I know about reserved names and ID fields. It was just a simple example to illustrate what I was after.

I actually tried this method with the DCount function before, but it ran extremely slow due to the aggregate function. But I see a few differences in your function's criteria that I'll try out.

I'll test and post results.

Feb 18 '15 #6
Seth Schrock
2,965 Recognized Expert Specialist
The only way that you can speed up the query is to reduce the number of records that it has to run the aggregate function on.
Feb 18 '15 #7
32,569 Recognized Expert Moderator MVP
Actually Seth, you could link in the data as a separate data source rather than use a Domain Aggregate function call. The latter are notoriously inefficient and should be avoided where possible within SQL.

As J Forbes illustrates this can still be done using a cross-tab. I would expect that to work more efficiently as you're letting the SQL engine to the hard work - which is what it's designed for.
Feb 18 '15 #8
63 New Member
NeoPa, could you please clarify or give an example of linking in the data as a separate data source?

Feb 19 '15 #9
63 New Member
Seth, the performance is much better on the DCount function using the method in your example, thank you.

But I keep getting a "syntax error (missing operator)" on the second query example you gave with the subqueries. The error is in the section with the LEFT JOINS.

Any ideas?
Feb 19 '15 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

by: glenn | last post by:
I'm really scratching my head over this one. I'm working with CSV data exported from Excel, which explains why it's a mess to begin with. Within a table (or via any other means someone might be able to recommend) I need to combine multiple records which share two like fields. (If that's not clear, the real-world explanation is below.) +----------+--------------+----+----+----+----+----+ | ID | ADDRESS | P1 | P2 | P3 | P4 |...
by: Omega | last post by:
Hi How do I make my datagrid to display multiple records on a row ? With: <Columns> <asp:TemplateColumn> <ItemTemplate> <td>Blablabla</td>
by: jhutchings | last post by:
Hello everyone, I have a database where I collect shipment data from various tables. However, I have a problem. Whenever I want to see shipping data for orders that were set to ship on or before a certain date (in this case January 30th) the database will return 2 rows for an order as you can see below. Order ID: Line: Due Date: Qty: Ship Qty: Part #: Shipped: 141285 1 1/30/2006 31 10 S15F-55
by: musicloverlch | last post by:
I have a table with multiple records in it. I am being forced to combine multiple records into one record in order to be uploaded to a website, and I only get one record per client. How can I systematically combine multiple records into 1? The table looks like this Client ID Service Status 1 Doc Pending 1 Test Pending
by: NumberCruncher | last post by:
Hi All, I am struggling with setting up my first system of tables, forms,and reports, and could use your help! I am setting up a database to keep track of the production of a produced item. The item is a panel, with a specific texture. There are standard panels that are then produced in a limited number of specific textures. The number of panels/project varies, so I keep track of the actual number of panels per project with the count:...
by: werks | last post by:
Hello experts could someone tell me how can i combine multiple records into one value. Sample in MS Access: (Lastname & "" & "," & " " & Firstname) AS Name How can i do this in MySQL?..tnx in advance -- Kenneth "Better Than Yesterday"
by: bluereign | last post by:
Thank you for your assistance. I am a novice looking to JOIN and append or combine records from 2 current Tables into 2 new Tables named below. I have been able to JOIN Tables with the script below. My problem is if I don’t use the WHERE clause in my script below, the script can query up to 3 records with the same “LoanNo” due to the fact that there can be up to 3 “TypeCodes” in each Table (Borrower, Co-Borrower 1 and Co-Borrower 2...
by: mattd | last post by:
I have a data file that contains multiple records for each employee. I'm trying to build a view of the table that would show me a combined date range for per location. The example below are the fields from the file that i'm trying to combine. Table Exp: Employee # Begin Date End Date Location 1 1/1/2005 10/1/2005 BRD 1 10/2/2005 5/31/2007 BRD 1 6/1/2007 3/31/2009 RMC 1 4/1/2009 ...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.