MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.

ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month!
Download and Activate to enter!

Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 30th, 2012, 02:20 PM
cjassi08 cjassi08 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 cjassi08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 16 m 38 sec
Reputation Power: 0
How to flatten a MYSQL table?

I'm new using MYSQL database and I would like some help to flatten a mysql table. Suppose I have the following table:

UID GO
Q9NQG7 GO:0005764
Q9NQG7 GO:0042470
P02GN1 GO:0005624
P02GN1 GO:0003461

Instead of having the UID repeated along side the GO terms, I want to have the two different UID grouped into one column and all the GO terms into a row. In this case the flattened file would be in the following.

UID 0005624 0005764 00042470 0003461
Q9NQG7 0 1 1 0
P02GN1 1 0 0 1

Where "1" is used to match otherwise "0". Can I please get some help? Thanks.

Reply With Quote
  #2  
Old January 30th, 2012, 09:40 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
are you looking to create a new table? or are you just interested in pulling out the data from your existing table?

also, why are you interested in "flattening" this data? what's wrong with the way it's stored now?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old January 30th, 2012, 10:42 PM
cjassi08 cjassi08 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 cjassi08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 16 m 38 sec
Reputation Power: 0
Quote:
Originally Posted by r937
are you looking to create a new table? or are you just interested in pulling out the data from your existing table?

also, why are you interested in "flattening" this data? what's wrong with the way it's stored now?


Yes, I'm looking to create a new table. Some of the data in the table have similar records. My goal is to eliminate the duplicate and create two rows.
A section of my thesis is to run a database using one of my machine learning algorithms and compare the performances, but in order to do so I have to flatten the data and I'm having some difficulties.

Reply With Quote
  #4  
Old January 31st, 2012, 11:19 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
here ya go...
Code:
SELECT UID
     , MAX(CASE WHEN GO = '0005624'
                THEN 1 ELSE 0 END) AS '0005624'
     , MAX(CASE WHEN GO = '0005764'
                THEN 1 ELSE 0 END) AS '0005764'
     , MAX(CASE WHEN GO = '00042470'
                THEN 1 ELSE 0 END) AS '00042470'
     , MAX(CASE WHEN GO = '0003461'
                THEN 1 ELSE 0 END) AS '0003461'
  FROM daTable
GROUP
    BY UID 
Comments on this post
cjassi08 agrees!

Reply With Quote
  #5  
Old February 6th, 2012, 02:06 PM
cjassi08 cjassi08 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 cjassi08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 16 m 38 sec
Reputation Power: 0
Quote:
Originally Posted by r937
here ya go...
Code:
SELECT UID
     , MAX(CASE WHEN GO = '0005624'
                THEN 1 ELSE 0 END) AS '0005624'
     , MAX(CASE WHEN GO = '0005764'
                THEN 1 ELSE 0 END) AS '0005764'
     , MAX(CASE WHEN GO = '00042470'
                THEN 1 ELSE 0 END) AS '00042470'
     , MAX(CASE WHEN GO = '0003461'
                THEN 1 ELSE 0 END) AS '0003461'
  FROM daTable
GROUP
    BY UID 


Thank you so much the query works great! But once I test the query with repeated GO terms it doesn't work correctly. It gives me only "0" output. For example once I test the following test case:

Q9NQG7 GO:0042470
Q9NQG7 GO:0042470
P02656 GO:0042627
P02656 GO:0034363
P02656 GO:0034363
P09651 GO:0005654
P09651 GO:0005654

The output is the following:
P02656 ,0,0,0
P09651 ,0,0,0
Q9NQG7 ,0,0,0

Is it something am I doing wrong? Thank you.

Reply With Quote
  #6  
Old February 6th, 2012, 02:51 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2006
Posts: 1,914 SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 11 h 34 sec
Reputation Power: 1297
If the values in your GO column actually are as you show (i.e., GO:0042470) then you'll need to change the CASE statements to look for that style of value:

Code:
SELECT UID
     , MAX(CASE WHEN GO = 'GO:0005624'
                THEN 1 ELSE 0 END) AS '0005624'
     , MAX(CASE WHEN GO = 'GO:0005764'
                THEN 1 ELSE 0 END) AS '0005764'
     , MAX(CASE WHEN GO = 'GO:0042470'
                THEN 1 ELSE 0 END) AS '0042470'
     , MAX(CASE WHEN GO = 'GO:0003461'
                THEN 1 ELSE 0 END) AS '0003461'
  FROM daTable
GROUP
    BY UID
Comments on this post
cjassi08 agrees!
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc

Reply With Quote
  #7  
Old February 6th, 2012, 03:24 PM
cjassi08 cjassi08 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 cjassi08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 16 m 38 sec
Reputation Power: 0
Quote:
Originally Posted by SimonJM
If the values in your GO column actually are as you show (i.e., GO:0042470) then you'll need to change the CASE statements to look for that style of value:

Code:
SELECT UID
     , MAX(CASE WHEN GO = 'GO:0005624'
                THEN 1 ELSE 0 END) AS '0005624'
     , MAX(CASE WHEN GO = 'GO:0005764'
                THEN 1 ELSE 0 END) AS '0005764'
     , MAX(CASE WHEN GO = 'GO:0042470'
                THEN 1 ELSE 0 END) AS '0042470'
     , MAX(CASE WHEN GO = 'GO:0003461'
                THEN 1 ELSE 0 END) AS '0003461'
  FROM daTable
GROUP
    BY UID


Thank you! This is absolutely awesome! I'm going to test a larger data and see how it goes.

Reply With Quote
  #8  
Old March 10th, 2012, 11:34 AM
cjassi08 cjassi08 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 cjassi08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 16 m 38 sec
Reputation Power: 0
Quote:
Originally Posted by cjassi08
Thank you! This is absolutely awesome! I'm going to test a larger data and see how it goes.


Hi, thank you so much for your help! The SQL command works great for small databases, but I can't use it on larger data because I have to manually entered every single lines manually. I'm using a pretty large data for my thesis and I need help setting up a program to help somehow automatically give the same outcome without having to write thousand of lines manually. Thank you!

Reply With Quote
  #9  
Old March 10th, 2012, 11:41 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,046 r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level)r937 User rank is General 42nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 2 Days 22 h 44 sec
Reputation Power: 3829
Quote:
Originally Posted by cjassi08
...without having to write thousand of lines manually. Thank you!
thousands?

you prolly won't be able to do that

writing thousands of CASE expressions implies you want a row that has thousands of columns

do a search in the mysql manual for this:
Quote:
There is a hard limit of 4096 columns per table, but the effective maximum may be less for a given table. The exact limit depends on several interacting factors, listed in the following discussion.
and then read the following discussion

you're probably going to want to leave your table narrow (two columns) and do whatever analysis you need to do in some specialized software

i believe the apl language is good for that

Reply With Quote
  #10  
Old March 10th, 2012, 11:58 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2006
Posts: 1,914 SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level)SimonJM User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 11 h 34 sec
Reputation Power: 1297
APL, from what I have seen, is a stupendous language for stats and analysis. Also from what I have seen it's a seriously weird language (some of our Nokia keyboards for mainframe terminals had special APL characters)!

Reply With Quote
  #11  
Old March 10th, 2012, 08:42 PM
cjassi08 cjassi08 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 cjassi08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 16 m 38 sec
Reputation Power: 0
Quote:
Originally Posted by SimonJM
APL, from what I have seen, is a stupendous language for stats and analysis. Also from what I have seen it's a seriously weird language (some of our Nokia keyboards for mainframe terminals had special APL characters)!


Thank you, but not only I'm not familiar with the APL, I can't seem to find a way to find some programing tutorials or examples. It probably wont' work for me.

Reply With Quote
  #12  
Old March 24th, 2012, 11:51 AM
cjassi08 cjassi08 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 cjassi08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 16 m 38 sec
Reputation Power: 0
Quote:
Originally Posted by cjassi08
Thank you! This is absolutely awesome! I'm going to test a larger data and see how it goes.



I would like to output 'True' and 'False' instead of '1' and '0' because my program reject numerical values. The MYSQL is as follow:

SELECT UID
, MAX(CASE WHEN GO = 'GO:0005624'
THEN 1 ELSE 0 END) AS '0005624'
, MAX(CASE WHEN GO = 'GO:0005764'
THEN 1 ELSE 0 END) AS '0005764'
, MAX(CASE WHEN GO = 'GO:0042470'
THEN 1 ELSE 0 END) AS '0042470'
, MAX(CASE WHEN GO = 'GO:0003461'
THEN 1 ELSE 0 END) AS '0003461'
FROM daTable
GROUP
BY UID

I have tried the following without any luck:

SELECT UID IF((SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END),'true','false');
, MAX(CASE WHEN GO = 'GO:0005624'
THEN 1 ELSE 0 END) AS '0005624'
, MAX(CASE WHEN GO = 'GO:0005764'
THEN 1 ELSE 0 END) AS '0005764'
, MAX(CASE WHEN GO = 'GO:0042470'
THEN 1 ELSE 0 END) AS '0042470'
, MAX(CASE WHEN GO = 'GO:0003461'
THEN 1 ELSE 0 END) AS '0003461'
FROM daTable
GROUP
BY UID

Thanks in advance!

Reply With Quote
  #13  
Old March 24th, 2012, 01:08 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,279 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 15 sec
Reputation Power: 388
Code:
select UID,
       max(case when GO = 'GO:0005624' then 'TRUE' else 'FALSE' end) as "0005624",
       max(case when GO = 'GO:0005764' then 'TRUE' else 'FALSE' end) as "0005764",
       max(case when GO = 'GO:0042470' then 'TRUE' else 'FALSE' end) as "0042470",
       max(case when GO = 'GO:0003461' then 'TRUE' else 'FALSE' end) as "0003461"
  from daTable
 group
    by UID
Comments on this post
cjassi08 agrees!

Reply With Quote
  #14  
Old March 24th, 2012, 10:49 PM
cjassi08 cjassi08 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 13 cjassi08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 16 m 38 sec
Reputation Power: 0
Quote:
Originally Posted by swampBoogie
Code:
select UID,
       max(case when GO = 'GO:0005624' then 'TRUE' else 'FALSE' end) as "0005624",
       max(case when GO = 'GO:0005764' then 'TRUE' else 'FALSE' end) as "0005764",
       max(case when GO = 'GO:0042470' then 'TRUE' else 'FALSE' end) as "0042470",
       max(case when GO = 'GO:0003461' then 'TRUE' else 'FALSE' end) as "0003461"
  from daTable
 group
    by UID


Thank you!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > How to flatten a MYSQL table?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap