473,408 Members | 2,734 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,408 software developers and data experts.

Combining Rows - Sort Order

I've implemented this solution and found that it only works if the records with the same "CompanyName" are beside each other in the table. More specifically, the VBA code processes each record based on the order of primary key in the table. Is there a way to accomplish the goal of the original poster if records aren't adjacent in the table?

Thank you for your help!
May 5 '10 #1
11 2692
NeoPa
32,556 Expert Mod 16PB
The solution can be modified easily to include an ORDER BY clause in the SQL. In all my testing I never found it was necessary as the GROUP BY clause implied the same. If you are finding this is a problem though, that is certainly the thing to do.
May 5 '10 #2
Thank you for responding; I wasn't sure if anyone would be monitoring this thread for new posts. You're really quite knowledgeable after reading this thread and the one linked to in the solution at the top.

I'm running SQL along these lines:
Expand|Select|Wrap|Line Numbers
  1. SELECT [GroupBy1],
  2.        [GroupBy2],
  3.        ...,
  4.        [GroupByN],
  5.        Max(Concat([GroupBy1] &
  6.                   [GroupBy2] &
  7.                   ... &
  8.                   [GroupByN], Item)) AS Items
  9. FROM [YourTable]
  10. GROUP BY [GroupBy1],
  11.          [GroupBy2],
  12.          ...,
  13.          [GroupByN]
  14. ORDER BY [GroupBy2]
I've set the VBA function to output its variables right before the "Concat = strItems" line at the end of the function. Even with the Order By, the function is processing records in the order they were put into the database table, either based on the primary key or the default ordering of the table. I've even tried creating an Advanced Sort on the table (a sort that persists even if the database is closed) so that the right records are adjacent, and the records are still processed by the function in the primary key / default order.

I must be missing something if this is working for others who have tables that weren't populated by inputting all of the records in order by "CompanyName".

e.g. my table would look like:
Expand|Select|Wrap|Line Numbers
  1. CompanyName        Grower    Category    Product
  2. B Orchard            -1    Fruits        Apples
  3. B Orchard            -1    Vegetables    Beans
  4. B Orchard            -1    Vegetables    Other
  5. B Star Acres        -1    Vegetables    Beets
  6. B Farms            -1    Fruits        Blackberries
  7. B Orchard & Roadside Market -1    Fruits        Apples
  8. B Orchard            -1    Fruits        Blackberries
  9. B Orchard & Roadside Market -1    Fruits        Blackberries
  10. B Orchard & Roadside Market -1    Vegetables    Beans
  11. B Orchard & Roadside Market -1    Vegetables    Beets
  12. B Star Acres        -1    Vegetables    Asparagus
  13. B Farms            -1    Vegetables    Broccoli
  14. B Farms            -1    Vegetables    Cabbage
May 6 '10 #3
NeoPa
32,556 Expert Mod 16PB
This thread was a post added to Combining Multiple Rows of one Field into One Result and refers to that thread.
May 6 '10 #4
NeoPa
32,556 Expert Mod 16PB
I'd like to help if I can, but your actual SQL & VBA code would be more helpful to understand the issue than some general references to amendments made to the originals. I don't have enough to even understand the issue here I'm afraid. Something is unusual about your setup, but as I don't have that available. I depend on your sharing the information with me.

I can say that the ORDER BY clause should match the GROUP BY clause exactly. If your post is accurate (very important in situations such as these) then it could not be expected to work.
May 6 '10 #5
I'm sorry. I will include that information here:

Here is my SQL, with the ORDER BY now matching the GROUP BY:
Expand|Select|Wrap|Line Numbers
  1. SELECT [qry_row_aggregates-prework].bigNumber, [qry_row_aggregates-prework].Building, [qry_row_aggregates-prework].WTU, [qry_row_aggregates-numdays].NumDays, Max(Concat([qry_row_aggregates-prework].bigNumber & [qry_row_aggregates-prework].Building & [qry_row_aggregates-prework].WTU & [qry_row_aggregates-numdays].NumDays,[BusinessType])) AS Business_type
  2. FROM [qry_row_aggregates-prework], [qry_row_aggregates-numdays]
  3. WHERE ((([qry_row_aggregates-prework].bigNumber)=[qry_row_aggregates-numdays].bigNumber))
  4. GROUP BY [qry_row_aggregates-prework].bigNumber, [qry_row_aggregates-prework].Building, [qry_row_aggregates-prework].WTU, [qry_row_aggregates-numdays].NumDays
  5. ORDER BY [qry_row_aggregates-prework].bigNumber, [qry_row_aggregates-prework].Building, [qry_row_aggregates-prework].WTU, [qry_row_aggregates-numdays].NumDays;
In this SQL, I have this Concat VBA function called:
Expand|Select|Wrap|Line Numbers
  1. Public Function Concat(strGroup As String, _
  2.                        strItem As String) As String
  3.     Static strLastGroup As String
  4.     Static strItems As String
  5.  
  6.     If strGroup = strLastGroup Then
  7.         strItems = strItems & ", " & strItem
  8.     Else
  9.         strLastGroup = strGroup
  10.         strItems = strItem
  11.     End If
  12.     MsgBox "strGroup=" & strGroup & " strLastGroup=" & strLastGroup & " strItem=" & strItem & " strItems=" & strItems
  13.     Concat = strItems
  14. End Function
Herein lies the issue: when MsgBox is invoked, it prints out the variables, which indicates that the records are being processed not by the ORDER BY clause order but in the original order of primary key / default order from the table. Does that make sense? In other words, based on the data in the variables, I'm able to verify the order that the Concat function is processing records.

Note that the table isn't referenced in the SQL above, because there are a few queries between this query and the table. These intermediary queries also have ORDER BY clauses that put the records in the right order. Alas, it seems that the VBA code is not paying attention to that order.
May 6 '10 #6
NeoPa
32,556 Expert Mod 16PB
May I ask why you are working with a Cartesian Product of the two queries in the FROM clause?

Put another way, why is there no JOIN in your FROM clause?
May 6 '10 #7
NeoPa
32,556 Expert Mod 16PB
Note that the table isn't referenced in the SQL above, because there are a few queries between this query and the table. These intermediary queries also have ORDER BY clauses that put the records in the right order. Alas, it seems that the VBA code is not paying attention to that order.
By the way, the VBA code is invoked in the order that the SQL processes the data. The ordering is only relevant in the SQL, though your VBA trick of displaying the data does give us a reliable window on how (in which order) the data is being processed.
May 6 '10 #8
@NeoPa
Thanks for your suggestion. I've updated the SQL to include the JOIN:
Expand|Select|Wrap|Line Numbers
  1. SELECT [qry_row_aggregates-prework].bigNumber, [qry_row_aggregates-prework].Building, [qry_row_aggregates-prework].WTU, [qry_row_aggregates-numdays].NumDays, Max(Concat([qry_row_aggregates-prework].bigNumber & [qry_row_aggregates-prework].Building & [qry_row_aggregates-prework].WTU & [qry_row_aggregates-numdays].NumDays,[BusinessType])) AS Business_type
  2. FROM [qry_row_aggregates-prework]
  3. INNER JOIN [qry_row_aggregates-numdays]
  4. ON [qry_row_aggregates-prework].bigNumber=[qry_row_aggregates-numdays].bigNumber
  5. GROUP BY [qry_row_aggregates-prework].bigNumber, [qry_row_aggregates-prework].Building, [qry_row_aggregates-prework].WTU, [qry_row_aggregates-numdays].NumDays
  6. ORDER BY [qry_row_aggregates-prework].bigNumber, [qry_row_aggregates-prework].Building, [qry_row_aggregates-prework].WTU, [qry_row_aggregates-numdays].NumDays;
As far as I can tell, the behavior is the same. What else should I try?
May 6 '10 #9
NeoPa
32,556 Expert Mod 16PB
That's interesting :S

I cannot see why you would get the results you describe. It might be interesting to look at the database itself if that is ok. I've copied below a general set of instructions for attaching databases. If you want to I suggest you attach yours for a closer look.
When attaching your work please follow the following steps first :
  1. Remove anything not relevant to the problem. This is not necessary in all circumstances but some databases can be very bulky and some things do not effect the actual problem at all.
  2. Likewise, not entirely necessary in all cases, but consider saving your database in a version not later than 2003 as many of our experts don't use Access 2007. Largely they don't want to, but some also don't have access to it. Personally I will wait until I'm forced to before using it.
  3. If the process depends on any linked tables then make local copies in your database to replace the linked tables.
  4. If you've done anything in steps 1 to 3 then make sure that the problem you're experiencing is still evident in the updated version.
  5. Compile the database (From the Visual Basic Editor select Debug / Compile {Project Name}).
  6. Compact the database.
  7. Compress the database into a ZIP file.
  8. When posting, scroll down the page and select Manage Attachments (Pressing on that leads you to a page where you can add or remove your attachments. It also lists the maximum file sizes for each of the allowed file types.) and add this new ZIP file.
It's also a good idea to include some instructions that enable us to find the issue you'd like help with. Maybe some instructions of what to select, click on, enter etc that ensures we'll see what you see and have the same problems.
May 7 '10 #10
@NeoPa
I've been doing some debugging and found the following. Please follow this logic:

The Max(Concat()) function is in field X of Query B. Query B queries Query A. Query B does not receive concatenated data in field X in all instances where it should.

If the results of Query A are in a table, Table A (via make-table query), Query B queries Table A and receives concatenated data in field X in all instances where it should.

Since Query A and Table A contain the same data, why do Query B's results differ?
May 12 '10 #11
NeoPa
32,556 Expert Mod 16PB
Access (& most DBMSs) see the sort order of a query as something important for displaying, but may override it for subqueries if it determines that performance is enhanced by different sorting. Tables have a built-in sort order that a query will use by default.

I suspect if this is noticed that you have some sort of problem along these lines. Without seeing the database though, I'm just using my licked finger.

BTW. It's a good thing that you're doing the debugging first. That sort of approach will certainly help you.
May 12 '10 #12

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

Similar topics

2
by: One's Too Many | last post by:
Ran into a strange problem today: 8.1.7 on AIX 4.3.3 Database and applications had been working fine for two years and all of a sudden a couple of regularly-run queries are now no longer...
5
by: Greg Brady | last post by:
Is there a way to return a random sort order from a query?
3
by: Bob Dankert | last post by:
Is there any way to maintain the sort order of a sort on a 2D array? For example: I have the array: 1,a 2,a 3,f 4,a 5,s 6,a 7,z 8,b and sort it by the second column, and I...
7
by: Steve Crawford | last post by:
I am suffering some sort order confusion. Given a database, "foo", with a single character(4) column of data left padded with spaces I get: select * from foo order by somechars; somechars...
0
by: joseph speigle | last post by:
hi, To see the query results in native language see http://database.sarang.net/?inc=read&aid=5368&criteria=pgsql&subcrit=qna&id=&limit=20&keyword=&page=1 the simpler url is ...
2
by: Emma Burrows | last post by:
I have created a typed dataset in .Net 2.0 based on an Access database, and set up various methods to retrieve specific data from the tables, etc (great fun). However, I need to implement a custom...
12
by: Cindy Lee | last post by:
When I do a sorta on 1 table, then the other table goes back to the original order. What can I set so, it keeps the order of the other current gridview's order. I set all the gridview values...
1
dotneto
by: dotneto | last post by:
Hi, I'm working on a gridview with an object datasource. I send the sort expression and sort order to the selecting method through a parameter. It works fine in the ascending order, but in the...
1
by: TimSki | last post by:
Hi, I have a large table (2M rows) with an id column. I've put a primary key on the id column which in turn automatically creates a clustered index on this column in ASC order. All of my queries...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
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...

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.