PC Review


Reply
Thread Tools Rate Thread

Combine data from multiple rows onto one row in separate columns

 
 
Balbina
Guest
Posts: n/a
 
      6th Jan 2010
My table looks like this:

Sample_# data1 data2 data3 data4 etc..
0000001 0.1 0.2
0000001 2.5
0000001 0.5
0000002 0.2 0.2
0000002 3.1
0000003 0.1 0.2

I receive data at different points in time and it thus I end up with
multiple sample_#'s and various data columns filled in. I would like to
combine all the data for each sample into one row.

I would like it to look like this:

Sample_# data1 data2 data3 data4 etc..
0000001 0.1 2.5 0.2 0.5
0000002 0.2 3.1 0.2
0000003 0.1 0.2

Any suggestions for automating amalgamating this data into the format I
would like?

Please help.
Thank you.
 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      6th Jan 2010
On Tue, 5 Jan 2010 18:18:01 -0800, Balbina <(E-Mail Removed)>
wrote:

>My table looks like this:
>
>Sample_# data1 data2 data3 data4 etc..
>0000001 0.1 0.2
>0000001 2.5
>0000001 0.5
>0000002 0.2 0.2
>0000002 3.1
>0000003 0.1 0.2
>
>I receive data at different points in time and it thus I end up with
>multiple sample_#'s and various data columns filled in. I would like to
>combine all the data for each sample into one row.
>
>I would like it to look like this:
>
>Sample_# data1 data2 data3 data4 etc..
>0000001 0.1 2.5 0.2 0.5
>0000002 0.2 3.1 0.2
>0000003 0.1 0.2
>
>Any suggestions for automating amalgamating this data into the format I
>would like?
>
>Please help.
>Thank you.


Your data input process is clearly at fault here: it shouldn't be adding
multiple records per sample, it should be updating existing sample records if
they are there and only adding a new one if there isn't!

Will you ever have two different numbers for a given field for a given sample,
e.g.

Sample_# data1 data2 data3 data4 etc..
0000001 0.1 0.2
0000001 2.5
0000001 0.5
0000001 0.5

If so, what do you want to happen? Store 0.1, 0.5, 0.6? Generate an error
message?

If you will NEVER have this situation - each field will have only one non-null
value for all instances - you could create a second identically structured
table and fill it using an Append query:

INSERT INTO newtable
SELECT table.[Sample_#], Max([data1]) AS Data1, Max([data2]) AS Data2,
Max([data3]) AS data3, Max([data4]) AS Data4
FROM table
GROUP BY table.[Sample_#];

--

John W. Vinson [MVP]
 
Reply With Quote
 
Balbina
Guest
Posts: n/a
 
      6th Jan 2010


"John W. Vinson" wrote:

> On Tue, 5 Jan 2010 18:18:01 -0800, Balbina <(E-Mail Removed)>
> wrote:
>
> >My table looks like this:
> >
> >Sample_# data1 data2 data3 data4 etc..
> >0000001 0.1 0.2
> >0000001 2.5
> >0000001 0.5
> >0000002 0.2 0.2
> >0000002 3.1
> >0000003 0.1 0.2
> >
> >I receive data at different points in time and it thus I end up with
> >multiple sample_#'s and various data columns filled in. I would like to
> >combine all the data for each sample into one row.
> >
> >I would like it to look like this:
> >
> >Sample_# data1 data2 data3 data4 etc..
> >0000001 0.1 2.5 0.2 0.5
> >0000002 0.2 3.1 0.2
> >0000003 0.1 0.2
> >
> >Any suggestions for automating amalgamating this data into the format I
> >would like?
> >
> >Please help.
> >Thank you.

>
> Your data input process is clearly at fault here: it shouldn't be adding
> multiple records per sample, it should be updating existing sample records if
> they are there and only adding a new one if there isn't!
>
> Will you ever have two different numbers for a given field for a given sample,
> e.g.
>
> Sample_# data1 data2 data3 data4 etc..
> 0000001 0.1 0.2
> 0000001 2.5
> 0000001 0.5
> 0000001 0.5
>
> If so, what do you want to happen? Store 0.1, 0.5, 0.6? Generate an error
> message?
>
> If you will NEVER have this situation - each field will have only one non-null
> value for all instances - you could create a second identically structured
> table and fill it using an Append query:
>
> INSERT INTO newtable
> SELECT table.[Sample_#], Max([data1]) AS Data1, Max([data2]) AS Data2,
> Max([data3]) AS data3, Max([data4]) AS Data4
> FROM table
> GROUP BY table.[Sample_#];
>
> --
>
> John W. Vinson [MVP]
> .


Thank you. That works really well. It might happen that I would have more
than one value for a given field, but that would be an error. Any suggestions
for trapping that error?
 
Reply With Quote
 
KARL DEWEY
Guest
Posts: n/a
 
      6th Jan 2010
Try this --
INSERT INTO newtable
SELECT table.[Sample_#], IIF(Max([data1]) = Min([data1]), Max([data1]),
"Error")AS Data1, IIF(Max([data2]) = Min([data2]), Max([data2]), "Error") AS
Data2, IIF(Max([data3]) = Min([data3]), Max([data3]), "Error") AS Data3,
IIF(Max([data4]) = Min([data4]), Max([data4]), "Error")AS Data4
FROM table
GROUP BY table.[Sample_#];

--
Build a little, test a little.


"Balbina" wrote:

>
>
> "John W. Vinson" wrote:
>
> > On Tue, 5 Jan 2010 18:18:01 -0800, Balbina <(E-Mail Removed)>
> > wrote:
> >
> > >My table looks like this:
> > >
> > >Sample_# data1 data2 data3 data4 etc..
> > >0000001 0.1 0.2
> > >0000001 2.5
> > >0000001 0.5
> > >0000002 0.2 0.2
> > >0000002 3.1
> > >0000003 0.1 0.2
> > >
> > >I receive data at different points in time and it thus I end up with
> > >multiple sample_#'s and various data columns filled in. I would like to
> > >combine all the data for each sample into one row.
> > >
> > >I would like it to look like this:
> > >
> > >Sample_# data1 data2 data3 data4 etc..
> > >0000001 0.1 2.5 0.2 0.5
> > >0000002 0.2 3.1 0.2
> > >0000003 0.1 0.2
> > >
> > >Any suggestions for automating amalgamating this data into the format I
> > >would like?
> > >
> > >Please help.
> > >Thank you.

> >
> > Your data input process is clearly at fault here: it shouldn't be adding
> > multiple records per sample, it should be updating existing sample records if
> > they are there and only adding a new one if there isn't!
> >
> > Will you ever have two different numbers for a given field for a given sample,
> > e.g.
> >
> > Sample_# data1 data2 data3 data4 etc..
> > 0000001 0.1 0.2
> > 0000001 2.5
> > 0000001 0.5
> > 0000001 0.5
> >
> > If so, what do you want to happen? Store 0.1, 0.5, 0.6? Generate an error
> > message?
> >
> > If you will NEVER have this situation - each field will have only one non-null
> > value for all instances - you could create a second identically structured
> > table and fill it using an Append query:
> >
> > INSERT INTO newtable
> > SELECT table.[Sample_#], Max([data1]) AS Data1, Max([data2]) AS Data2,
> > Max([data3]) AS data3, Max([data4]) AS Data4
> > FROM table
> > GROUP BY table.[Sample_#];
> >
> > --
> >
> > John W. Vinson [MVP]
> > .

>
> Thank you. That works really well. It might happen that I would have more
> than one value for a given field, but that would be an error. Any suggestions
> for trapping that error?

 
Reply With Quote
 
Balbina
Guest
Posts: n/a
 
      6th Jan 2010
Thank you!
 
Reply With Quote
 
John W. Vinson
Guest
Posts: n/a
 
      6th Jan 2010
On Wed, 6 Jan 2010 14:13:03 -0800, KARL DEWEY
<(E-Mail Removed)> wrote:

>Try this --
>INSERT INTO newtable
>SELECT table.[Sample_#], IIF(Max([data1]) = Min([data1]), Max([data1]),
>"Error")AS Data1, IIF(Max([data2]) = Min([data2]), Max([data2]), "Error") AS
>Data2, IIF(Max([data3]) = Min([data3]), Max([data3]), "Error") AS Data3,
>IIF(Max([data4]) = Min([data4]), Max([data4]), "Error")AS Data4
>FROM table
>GROUP BY table.[Sample_#];


Very nice Karl!!! Thanks.
--

John W. Vinson [MVP]
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combine multiple columns and rows for one record into one row. Bowtie63 Microsoft Excel Misc 2 16th Feb 2008 04:20 AM
How do I combine data from separate Excel rows =?Utf-8?B?UGV0ZQ==?= Microsoft Excel New Users 2 19th Oct 2006 07:03 PM
Combine multiple columns into two long columns, Repeating rows in first column anasab@gmail.com Microsoft Excel Misc 2 31st Jul 2006 09:45 PM
Combine multiple columns into two long columns, Repeating rows in first column anasab@gmail.com Microsoft Excel Misc 0 31st Jul 2006 05:07 PM
SQL to combine columns of multiple rows into one row =?Utf-8?B?cmxn?= Microsoft Access Queries 9 13th Jun 2006 06:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:25 PM.