Unix Technical Forum

query - select common data from one column and display in severalcolumns

This is a discussion on query - select common data from one column and display in severalcolumns within the MySQL forums, part of the Database Server Software category; --> Hello, I have a (big) table which is not normalized, but for i need at the moment i think ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 01:59 PM
rui dias
 
Posts: n/a
Default query - select common data from one column and display in severalcolumns

Hello,

I have a (big) table which is not normalized, but for i need at the
moment i think
that's no problem. Indeed, what i would like to do is to select the
name field and
the note. The problem is that i want to display the note in 2
different columns.
the first columns will show the number (count) of time that a certain
note (e.g note=4)
appears for a certain name and in the other column the same thing but
for a different note value.

so each column noteX will display the number of time that the note
with the value X appears for each name


for example, to the following table:


Name | note | job | city | id |
----------------------------------------
john | 4 | jb1 | hamb | 1 |
john | 5 | jb2 | hamb | 2 |
john | 5 | jb3 | hamb | 3 |
john | 5 | jb4 | hamb | 4 |
Mark | 4 | jb1 | mun | 5 |
Mark | 4 | jb2 | mun | 6 |
Mark | 4 | jb5 | mun | 7 |
Mark | 5 | jb1 | mun | 8 |
peter | 5 | jb3 | berl | 9 |
peter | 5 | jb5 | berl | 10 |
frank | 4 | jb6 | v.form | 11 |
frank | 5 | jb3 | v.form | 12 |
frank | 5 | jb2 | v.form | 13 |

the result should be:

Name | note5 | note4 |
-------------------------
john | 3 | 1 |
Mark | 1 | 3 |
peter | 2 | 0 |
frank | 2 | 1


How should be the right SQL command to show the data i
want? :chomp:

Thanks a lot.

Rui Dias
(E-Mail address blocked: See forum rules)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 01:59 PM
Rik Wasmus
 
Posts: n/a
Default Re: query - select common data from one column and display in several columns

On Thu, 28 Feb 2008 11:39:06 +0100, rui dias <rldias@gmail.com> wrote:
> I have a (big) table which is not normalized, but for i need at the
> moment i think
> that's no problem. Indeed, what i would like to do is to select the
> name field and
> the note. The problem is that i want to display the note in 2
> different columns.
> the first columns will show the number (count) of time that a certain
> note (e.g note=4)
> appears for a certain name and in the other column the same thing but
> for a different note value.
>
> so each column noteX will display the number of time that the note
> with the value X appears for each name
>
>
> for example, to the following table:
>
>
> Name | note | job | city | id |
> ----------------------------------------
> john | 4 | jb1 | hamb | 1 |
> john | 5 | jb2 | hamb | 2 |
> john | 5 | jb3 | hamb | 3 |
> john | 5 | jb4 | hamb | 4 |
> Mark | 4 | jb1 | mun | 5 |
> Mark | 4 | jb2 | mun | 6 |
> Mark | 4 | jb5 | mun | 7 |
> Mark | 5 | jb1 | mun | 8 |
> peter | 5 | jb3 | berl | 9 |
> peter | 5 | jb5 | berl | 10 |
> frank | 4 | jb6 | v.form | 11 |
> frank | 5 | jb3 | v.form | 12 |
> frank | 5 | jb2 | v.form | 13 |
>
> the result should be:
>
> Name | note5 | note4 |
> -------------------------
> john | 3 | 1 |
> Mark | 1 | 3 |
> peter | 2 | 0 |
> frank | 2 | 1
>
>
> How should be the right SQL command to show the data i
> want? :chomp:


Not straight forward at all, here's some reading:
http://rpbouman.blogspot.com/2005/10...-in-mysql.html

> (E-Mail address blocked: See forum rules)


What forum? This is usenet.
--
Rik Wasmus
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 01:59 PM
Rik Wasmus
 
Posts: n/a
Default Re: query - select common data from one column and display in several columns

On Thu, 28 Feb 2008 11:46:33 +0100, Rik Wasmus
<luiheidsgoeroe@hotmail.com> wrote:
<snip crosstab>

>> How should be the right SQL command to show the data i
>> want? :chomp:

>
> Not straight forward at all, here's some reading:
> http://rpbouman.blogspot.com/2005/10...-in-mysql.html


This one might be better:
http://dev.mysql.com/tech-resources/...ard/index.html
--
Rik Wasmus
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 01:59 PM
Pavel Lepin
 
Posts: n/a
Default Re: query - select common data from one column and display in several columns


rui dias <rldias@gmail.com> wrote in
<85ad7903-ebcc-462b-8d34-104ffecd2557@s8g2000prg.googlegroups.com>:
> I have a (big) table which is not normalized, but for i
> need at the moment i think
> that's no problem. Indeed, what i would like to do is to
> select the name field and
> the note. The problem is that i want to display the note
> in 2 different columns.
> the first columns will show the number (count) of time
> that a certain note (e.g note=4)
> appears for a certain name and in the other column the
> same thing but for a different note value.
>
> Name | note5 | note4 |
> -------------------------
> john | 3 | 1 |
> Mark | 1 | 3 |
> peter | 2 | 0 |
> frank | 2 | 1


Unless I'm missing something, this is trivial:

mysql> SELECT Name,SUM(IF(note=5,1,0)) AS
note5,SUM(IF(note=4,1,0)) AS note4 FROM johnmark GROUP BY
Name;
+-------+-------+-------+
| Name | note5 | note4 |
+-------+-------+-------+
| frank | 2 | 1 |
| john | 3 | 1 |
| Mark | 1 | 3 |
| peter | 2 | 0 |
+-------+-------+-------+
4 rows in set (0.01 sec)

You could also use self-joins instead of IF()s for clarity.

> How should be the right SQL command to show the data i
> want? :chomp:


":chomp:"?

> (E-Mail address blocked: See forum rules)


"Forum rules"? This is a Usenet newsgroup.

--
When all you have is a transformation engine, everything
looks like a tree.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:00 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com