473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Consolidate data from multiple rows into 1 row??

I have attached an example of what I am wanting to do. Please let me know if you have any additional questions or need more information from me. Thanks.
Attached Files
File Type: zip ConsolidateRows (2).zip (112.8 KB, 120 views)
May 12 '10 #1

✓ answered by ADezii

I've produced your results exactly as requested, but unfortunately the code is rather cumbersome and complex, and I honestly do not have the time to refine it. It will also only work for a MAXIMUM of 3 Duplicates on the [UPN]. I will post what I have, and make the Test Database available to you as an Attachment, so that you may further develop it should you so desire.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstUniqueUPNs As DAO.Recordset          'Contains the Unique UPNs
  3. Dim rstOrig As DAO.Recordset                'Records in tblOriginal with Unique [UPN]s
  4. Dim rstFinal As DAO.Recordset               'Will write the Final Results
  5. Dim intNumOfUPNs As Integer
  6. Dim intCounter As Integer
  7. Dim varRes09 As Variant
  8. Dim varRes08 As Variant
  9. Dim varYrBlt As Variant
  10.  
  11. CurrentDb.Execute "DELETE * FROM tblConsolidated", dbFailOnError
  12.  
  13. Set MyDB = CurrentDb
  14. Set rstUniqueUPNs = MyDB.OpenRecordset("SELECT DISTINCT [UPN] from tblOriginal", dbOpenForwardOnly)
  15. Set rstFinal = MyDB.OpenRecordset("tblConsolidated", dbOpenDynaset)
  16.  
  17. With rstUniqueUPNs
  18.   Do While Not .EOF
  19.     intNumOfUPNs = DCount("*", "tblOriginal", "[UPN] = " & ![UPN])
  20.     Set rstOrig = MyDB.OpenRecordset("SELECT * FROM tblOriginal WHERE [UPN] = " & ![UPN], dbOpenSnapshot)
  21.       rstOrig.MoveLast: rstOrig.MoveFirst       'Need 'exact' Recordcount
  22.         Select Case rstOrig.RecordCount
  23.           Case 1
  24.             rstFinal.AddNew
  25.               rstFinal![UPN] = rstOrig![UPN]
  26.               rstFinal![Res09_1] = rstOrig![Res09]
  27.               rstFinal![Res08_1] = rstOrig![Res08]
  28.               rstFinal![YearBuilt_1] = rstOrig![YearBuilt]
  29.             rstFinal.Update
  30.           Case 2
  31.             For intCounter = 1 To intNumOfUPNs
  32.               varRes09 = varRes09 & rstOrig![Res09] & ";"
  33.               varRes08 = varRes08 & rstOrig![Res08] & ";"
  34.               varYrBlt = varYrBlt & rstOrig![YearBuilt] & ";"
  35.                 rstOrig.MoveNext
  36.             Next
  37.             rstFinal.AddNew
  38.               rstFinal![UPN] = ![UPN]
  39.               rstFinal![Res09_1] = Split(varRes09, ";")(0)
  40.               rstFinal![Res09_2] = Split(varRes09, ";")(1)
  41.               rstFinal![Res08_1] = Split(varRes08, ";")(0)
  42.               rstFinal![Res08_2] = Split(varRes08, ";")(1)
  43.               rstFinal![YearBuilt_1] = Split(varYrBlt, ";")(0)
  44.               rstFinal![YearBuilt_2] = Split(varYrBlt, ";")(1)
  45.             rstFinal.Update
  46.           Case 3
  47.             For intCounter = 1 To intNumOfUPNs
  48.               varRes09 = varRes09 & rstOrig![Res09] & ";"
  49.               varRes08 = varRes08 & rstOrig![Res08] & ";"
  50.               varYrBlt = varYrBlt & rstOrig![YearBuilt] & ";"
  51.                 rstOrig.MoveNext
  52.             Next
  53.             rstFinal.AddNew
  54.               rstFinal![UPN] = ![UPN]
  55.               rstFinal![Res09_1] = Split(varRes09, ";")(0)
  56.               rstFinal![Res09_2] = Split(varRes09, ";")(1)
  57.               rstFinal![Res09_3] = Split(varRes09, ";")(2)
  58.               rstFinal![Res08_1] = Split(varRes08, ";")(0)
  59.               rstFinal![Res08_2] = Split(varRes08, ";")(1)
  60.               rstFinal![Res08_3] = Split(varRes08, ";")(2)
  61.               rstFinal![YearBuilt_1] = Split(varYrBlt, ";")(0)
  62.               rstFinal![YearBuilt_2] = Split(varYrBlt, ";")(1)
  63.               rstFinal![YearBuilt_3] = Split(varYrBlt, ";")(2)
  64.             rstFinal.Update
  65.           Case Else
  66.         End Select
  67.           varRes09 = Null
  68.           varRes08 = Null
  69.           varYrBlt = Null
  70.       .MoveNext
  71.   Loop
  72. End With
  73.  
  74. rstFinal.Close
  75. rstOrig.Close
  76. rstUniqueUPNs.Close
  77. Set rstFinal = Nothing
  78. Set rstOrig = Nothing
  79. Set rstUniqueUPNs = Nothing
  80.  
  81. DoCmd.OpenTable "tblConsolidated", acViewNormal, acReadOnly
  82. DoCmd.Maximize
P.S. - Any questions, feel free to ask.

6 2358
ADezii
8,834 Expert 8TB
@stateemk
What is the Maximum Number of Duplicates that can exist in the Original Table for a given UPN?
May 13 '10 #2
There is no max.
May 13 '10 #3
ADezii
8,834 Expert 8TB
@stateemk
That significantly complicates matters but I'll see what I can do.
May 13 '10 #4
ADezii
8,834 Expert 8TB
I've produced your results exactly as requested, but unfortunately the code is rather cumbersome and complex, and I honestly do not have the time to refine it. It will also only work for a MAXIMUM of 3 Duplicates on the [UPN]. I will post what I have, and make the Test Database available to you as an Attachment, so that you may further develop it should you so desire.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstUniqueUPNs As DAO.Recordset          'Contains the Unique UPNs
  3. Dim rstOrig As DAO.Recordset                'Records in tblOriginal with Unique [UPN]s
  4. Dim rstFinal As DAO.Recordset               'Will write the Final Results
  5. Dim intNumOfUPNs As Integer
  6. Dim intCounter As Integer
  7. Dim varRes09 As Variant
  8. Dim varRes08 As Variant
  9. Dim varYrBlt As Variant
  10.  
  11. CurrentDb.Execute "DELETE * FROM tblConsolidated", dbFailOnError
  12.  
  13. Set MyDB = CurrentDb
  14. Set rstUniqueUPNs = MyDB.OpenRecordset("SELECT DISTINCT [UPN] from tblOriginal", dbOpenForwardOnly)
  15. Set rstFinal = MyDB.OpenRecordset("tblConsolidated", dbOpenDynaset)
  16.  
  17. With rstUniqueUPNs
  18.   Do While Not .EOF
  19.     intNumOfUPNs = DCount("*", "tblOriginal", "[UPN] = " & ![UPN])
  20.     Set rstOrig = MyDB.OpenRecordset("SELECT * FROM tblOriginal WHERE [UPN] = " & ![UPN], dbOpenSnapshot)
  21.       rstOrig.MoveLast: rstOrig.MoveFirst       'Need 'exact' Recordcount
  22.         Select Case rstOrig.RecordCount
  23.           Case 1
  24.             rstFinal.AddNew
  25.               rstFinal![UPN] = rstOrig![UPN]
  26.               rstFinal![Res09_1] = rstOrig![Res09]
  27.               rstFinal![Res08_1] = rstOrig![Res08]
  28.               rstFinal![YearBuilt_1] = rstOrig![YearBuilt]
  29.             rstFinal.Update
  30.           Case 2
  31.             For intCounter = 1 To intNumOfUPNs
  32.               varRes09 = varRes09 & rstOrig![Res09] & ";"
  33.               varRes08 = varRes08 & rstOrig![Res08] & ";"
  34.               varYrBlt = varYrBlt & rstOrig![YearBuilt] & ";"
  35.                 rstOrig.MoveNext
  36.             Next
  37.             rstFinal.AddNew
  38.               rstFinal![UPN] = ![UPN]
  39.               rstFinal![Res09_1] = Split(varRes09, ";")(0)
  40.               rstFinal![Res09_2] = Split(varRes09, ";")(1)
  41.               rstFinal![Res08_1] = Split(varRes08, ";")(0)
  42.               rstFinal![Res08_2] = Split(varRes08, ";")(1)
  43.               rstFinal![YearBuilt_1] = Split(varYrBlt, ";")(0)
  44.               rstFinal![YearBuilt_2] = Split(varYrBlt, ";")(1)
  45.             rstFinal.Update
  46.           Case 3
  47.             For intCounter = 1 To intNumOfUPNs
  48.               varRes09 = varRes09 & rstOrig![Res09] & ";"
  49.               varRes08 = varRes08 & rstOrig![Res08] & ";"
  50.               varYrBlt = varYrBlt & rstOrig![YearBuilt] & ";"
  51.                 rstOrig.MoveNext
  52.             Next
  53.             rstFinal.AddNew
  54.               rstFinal![UPN] = ![UPN]
  55.               rstFinal![Res09_1] = Split(varRes09, ";")(0)
  56.               rstFinal![Res09_2] = Split(varRes09, ";")(1)
  57.               rstFinal![Res09_3] = Split(varRes09, ";")(2)
  58.               rstFinal![Res08_1] = Split(varRes08, ";")(0)
  59.               rstFinal![Res08_2] = Split(varRes08, ";")(1)
  60.               rstFinal![Res08_3] = Split(varRes08, ";")(2)
  61.               rstFinal![YearBuilt_1] = Split(varYrBlt, ";")(0)
  62.               rstFinal![YearBuilt_2] = Split(varYrBlt, ";")(1)
  63.               rstFinal![YearBuilt_3] = Split(varYrBlt, ";")(2)
  64.             rstFinal.Update
  65.           Case Else
  66.         End Select
  67.           varRes09 = Null
  68.           varRes08 = Null
  69.           varYrBlt = Null
  70.       .MoveNext
  71.   Loop
  72. End With
  73.  
  74. rstFinal.Close
  75. rstOrig.Close
  76. rstUniqueUPNs.Close
  77. Set rstFinal = Nothing
  78. Set rstOrig = Nothing
  79. Set rstUniqueUPNs = Nothing
  80.  
  81. DoCmd.OpenTable "tblConsolidated", acViewNormal, acReadOnly
  82. DoCmd.Maximize
P.S. - Any questions, feel free to ask.
Attached Files
File Type: zip Consolidate Rows.zip (18.8 KB, 97 views)
May 13 '10 #5
Thanks so much for all your help.
May 18 '10 #6
dsatino
393 256MB
What's the necessity of making the UPN field a primary key? The original table has a much easier structure to work with than your intended result table.
May 18 '10 #7

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

Similar topics

1
by: merdaad | last post by:
I am trying to read multiple rows from an SP into a datalist. I can easily read and display multiple rows if I use a select statement but when I call an SP to send me a few rows, I only get back...
5
by: SSP | last post by:
Dear ASP.NETers, How would I insert multiple rows of data from a web form? Are there any tute's and stuff around. Couldn't find any myself. Thanks in advance. SSP
5
by: Arsen V. | last post by:
Hello, What is the optimal way to insert multiple rows (around 1000) from a web application into a table? The user enters multiple lines into a text box (up to 10,000). The ASP.NET...
3
by: manjub | last post by:
Hi, I am trying to add ability to delete multiple rows from a DataGridView in my application. When user selects multiple rows and presses delete button, I want to show a message asking to user...
1
by: patrick.waldo | last post by:
Hi all, I was just curious if there was a built-in or a more efficient way to do take multiple rows of information and write them into excel using pyExcelerator. This is how I resolved the...
3
by: =?Utf-8?B?S2F5xLFoYW4=?= | last post by:
In my project,i added datagridview to my form , i transfered my table to datagridview and added multiple rows and when i called dataadapther.update ,,result is ok. But when i tried it for the...
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
3
by: Vinda | last post by:
Hi Bytes, Using a previous question as a base Access 2000 Inserting multiple rows based on a date range. I also wanted to insert multiple rows into a table according to a date range supplied by a...
2
by: emceemic | last post by:
Is there a way to show multiple rows per row if required? For example, say the table I currently have show the following data: Impact to Business, Business Owner Name, ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...

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.