473,394 Members | 1,841 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,394 software developers and data experts.

Combining multiple rows by creating new columns

Hi everyone, I have spent a few hours searching and cannot find an example that is similar enough for me to work with.

Is it possible to combine multiple rows by creating new columns? Searching the forum makes me believe it will take some VBA code.

What I have:
Number | person | email | phone | fax | Y/N | time
1 | bob | b.com | 123 | 123 | Y | 5:00
8 | bob | b.com | 888 | 888 | N |
4 | bob | b.com | 123 | 123 | Y | 5:00
1 | sue | s.com | 456 | 456 | N |
5 | sue | s.com | 456 | 456 | N |

What I'm looking for:
person | email | phone | fax | 1 | in_1 | 4 | in_4 | 5 | in_5 | 8 | in_8
bob | b.com | 888 | 888 | 5:00| Y | Y | 5:00 |NULL | N | N | Y
sue | s.com | 456 | 456 | N | Y |NULL | N | N | Y |NULL | N


It will take each different value in the number column and create a new column titles with the value.

It will update the phone and fax with the most recent value (The highest number in the number column will always be the most recent value.)

If the value is Yes, it will put the time instead of yes. If the value is no, it will put no.

If the person does not have a number that someone else has it will put no in the in_# column, else it will put yes.


This may take several queries to break the data apart better.
Any help would be greatly appreciated, I am new to access and have only created basic queries.
Aug 4 '15 #1
16 3096
Seth Schrock
2,965 Expert 2GB
You would actually want to do this in a related table as records and not as new fields. See Database Normalization and Table Structures. By doing it this way, you can add unlimited number of values without having to code anything or change the design of anything.
Aug 4 '15 #2
jforbes
1,107 Expert 1GB
What you are asking for could get pretty complicated and would most likely result in some VBA. The reason it could get complicated is that it isn't a standard practice in a Database and is akin to de-normalization. There is a slim chance what you are looking for could be accomplished by a Crosstab Query, but I wouldn't get your hopes up.

So, before heading down a development path, it would probably be best to ask what you are attempting to accomplish with this? There may be a simpler solution; a solution more inline with the tools available in Access.
Aug 4 '15 #3
I will look into what both of you have suggested, thank you.
For now there doesn't seem to be an easy way of doing this.

I was able to figure out how to replace the Yes with the time so instead of having a Yes/No column and a time column the new column is No/Time where if it is a yes it will show the time instead of the work yes.
Aug 4 '15 #4
Rabbit
12,516 Expert Mod 8TB
This is certainly doable. And we can help you do it. But before we can, you have to answer jforbe's question.
Aug 4 '15 #5
Thanks Rabbit, I read his question but completely skipped it in my response.
I have multiple rows per user, along with a number for each user. There can be multiple numbers per user. I am trying to combine multiple rows for each user into one row per user but create a column titled with that number and place a value whether or not the user has that number. Let me know if that makes sense, it sounds confusing trying to type it out.
Aug 4 '15 #6
Rabbit
12,516 Expert Mod 8TB
That's what you want to do. The question is why. We understand what it is you want to accomplish, but if you tell us the why, you may find out that you don't need to do it that way at all.
Aug 4 '15 #7
I need to be able to view a list of all the people in order to figure out who is a Yes or No for each Number. It is just for reporting purposes so there is one list of all of the users and what numbers they are a yes or no for. (Hopefully this counts as a why.)
Aug 4 '15 #8
Rabbit
12,516 Expert Mod 8TB
The next question is, does it have to be in that format? There's multiple ways to lay out that information and although I see the appeal of that layout, that's one of the more complicated methods to accomplish.
Aug 4 '15 #9
Yes it has to be in that format unfortunately.
I was able to create a crosstab query that does almost everything that I need.

person | email _| phone | fax | total __| total_Y __| 1 ___| 4 __| 5 | 8
bob ___| b.com | 888 __| 888 | 3 ____| ??_2_?? | 5:00 | 5:00 | 0 | N
sue ___| s.com | 456 __| 456 | 2 ____| ??_0_?? | N ___| 0 __| N | 0


A 0 means that the person does not have that number.
A N means they have that number but the value should be No
A time means they have the number and the value is a yes but I needed the time instead of yes.
I also have a total column that calculates the total numbers that each person has.

However, I need a column that calculates the total times each person has. I think this could use the Len() function to calculate the total of values that are longer than 1 character but I am having issues creating this column. And ideas?
Aug 5 '15 #10
jforbes
1,107 Expert 1GB
I would create a separate Query to calculate the totals, then create a third query to join the Crosstab Query you have with the New Totals Query on the Person. It will be easier to create and easier to maintain.

If you want to keep your query count down, create a Query based off the Crosstab Query, and add a column to select a Sum() from the base Table, kinda like:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   Crosstab.*
  3. , (SELECT Sum(Time) FROM BaseTable WHERE BaseTable.Person=Crosstab.Person) AS TotalTime
  4. FROM Crosstab
Aug 5 '15 #11
Rabbit
12,516 Expert Mod 8TB
In addition to jforbes suggestions, you can use the IIf() function to change your values that are displayed, you can also combine it with a sum to get your total Y count.
Aug 5 '15 #12
Rabbit, can you elaborate a little more on your idea. I have looked at jforbes response but am trying to get everything into one query if possible. I have used IIf, Nz and Len before and understand how they work, but not exactly sure what you are suggesting.

I can get the Total values in both the Total and Total_Y columns and I can also get the Total_Y values in both columns but I cannot get each in their own column at this point. If I use a WHERE Len(...)>2 I can get the desired results for the Total_Y column but the results also appear in the Total column since it filters those out completely. Can I use the WHERE filter only for one column or SELECT value?
Aug 5 '15 #13
Rabbit
12,516 Expert Mod 8TB
I have looked at jforbes response but am trying to get everything into one query if possible.
This (probably) won't be possible with everything you're trying to do.

I have used IIf, Nz and Len before and understand how they work, but not exactly sure what you are suggesting.
You said you want to display different values depending on the value of the field. You can do this using the IIf function.
Expand|Select|Wrap|Line Numbers
  1. IIf(field1 = 'some value', 'other value', field1)
Basically, what this does is, if field1 is equal to some value, return other value instead. Otherwise, for all other values of field1, return the original value.

Can I use the WHERE filter only for one column or SELECT value?
Nope.
Aug 5 '15 #14
Okay that is helping. I might have phrased one of my questions wrong.

If I have

# | 1 | 2 | 3 | 4 | 5
A | 0 | Y | N | Y | 0
B | N | N | 0 | 0 | 0

Can I create a both a column to sum the total Y and N in a row, and another column to sum just the Y in a row with only one query? Or will I have to go the route suggested above with multiple queries?
Aug 5 '15 #15
Rabbit
12,516 Expert Mod 8TB
You can, but it's convoluted. It's easier to go with a separate query as jforbes suggested. And then combine them into one result set.
Aug 5 '15 #16
Okay thank you, that is the approach I am working on now.
Aug 6 '15 #17

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

Similar topics

14
by: jackiefm | last post by:
I realize the thread I am responding to was posted in January but I am basically having the same issue. I am not familiar with VBA but use Access daily. I have written simple scripts but nothing to...
0
by: madhanmss | last post by:
Hi, How to freeze multiple rows and columns in a datagrid. It should be like an excel sheet, if you scroll horizontally, columns should be freezed and if u scroll vertically, rows should be...
6
by: Jeremy Goodman | last post by:
Access 2007; Merging records containing multivalue drop down lists. I have a database showing legislation information divided by State/territory. The database needs to be able to show the info...
1
by: samuel ray | last post by:
Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1
5
by: jamespfisher | last post by:
Hey all I'm writing a large extract report on clients of a pharmacy and the drugs they are prescribed (maximum of 6 drugs in total). Data structure of the drugs table looks like this: ...
7
by: almaroc | last post by:
i am using access 2007 and i am trying to combine multiple rows into one. i am using the code from this site but i continue to get this error: Compile error in query expression....
3
by: wyosorensen | last post by:
Hi, I work for an insurance company and what I'm doing is creating a database for writing policies where I can have one policy that covers multiple buildings. I have a form that they can fill out...
0
by: PreethiGowri | last post by:
I have a table something like name id date login logout arjun 1 2012-11-28 9-30 7-30 arjun 1 2012-11-29 9-30 8-00 arjun 1 2012-11-30 9-40 8-00 i want to...
7
by: Jrod2541 | last post by:
So I have a table named Table1. Inside Table1 are the fields: SKU, VendrItem, Qty and EachCost. SKU VendrItem Qty EachCost 123 ABC 2 $2 123 ABC 5 $2 ...
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
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?
0
marktang
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,...
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
Oralloy
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,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.