Unix Technical Forum

if in a query

This is a discussion on if in a query within the MySQL forums, part of the Database Server Software category; --> Hello there. I've a query with returns 2 values: a code and a value. What I want is to ...


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, 09:27 AM
Bob Bedford
 
Posts: n/a
Default if in a query

Hello there.

I've a query with returns 2 values: a code and a value.

What I want is to have 3 columns: col1 is the code, col2 is the value if the
code is=1 and col3 is the value if the code is=2.

Also I'd like to have the total of col2 and col3.

How to do so ?

Thanks for helping.

Bob



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 09:27 AM
Captain Paralytic
 
Posts: n/a
Default Re: if in a query

On 21 Mar, 17:01, "Bob Bedford" <b...@bedford.com> wrote:
> Hello there.
>
> I've a query with returns 2 values: a code and a value.
>
> What I want is to have 3 columns: col1 is the code, col2 is the value if the
> code is=1 and col3 is the value if the code is=2.
>
> Also I'd like to have the total of col2 and col3.
>
> How to do so ?
>
> Thanks for helping.
>
> Bob


What if the code is neither 1 nor 3?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 09:27 AM
strawberry
 
Posts: n/a
Default Re: if in a query

On Mar 21, 5:01 pm, "Bob Bedford" <b...@bedford.com> wrote:
> Hello there.
>
> I've a query with returns 2 values: a code and a value.
>
> What I want is to have 3 columns: col1 is the code, col2 is the value if the
> code is=1 and col3 is the value if the code is=2.
>
> Also I'd like to have the total of col2 and col3.
>
> How to do so ?
>
> Thanks for helping.
>
> Bob


Well here's one way:

SELECT a1.code,a2.value,a3.value
FROM mytable a1
LEFT JOIN mytable a2 ON a1.code = a2.code AND a2.code =1
LEFT JOIN mytable a3 ON a1.code = a3.code AND a3.code =2;

Is 'total' the sum or the count?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 09:27 AM
Captain Paralytic
 
Posts: n/a
Default Re: if in a query

On 21 Mar, 17:28, "strawberry" <zac.ca...@gmail.com> wrote:
> On Mar 21, 5:01 pm, "Bob Bedford" <b...@bedford.com> wrote:
>
> > Hello there.

>
> > I've a query with returns 2 values: a code and a value.

>
> > What I want is to have 3 columns: col1 is the code, col2 is the value if the
> > code is=1 and col3 is the value if the code is=2.

>
> > Also I'd like to have the total of col2 and col3.

>
> > How to do so ?

>
> > Thanks for helping.

>
> > Bob

>
> Well here's one way:
>
> SELECT a1.code,a2.value,a3.value
> FROM mytable a1
> LEFT JOIN mytable a2 ON a1.code = a2.code AND a2.code =1
> LEFT JOIN mytable a3 ON a1.code = a3.code AND a3.code =2;
>
> Is 'total' the sum or the count?


You should use COALESCE on that to get just 2 values out.

Or there is the IF function or even possibly in this case the ELT one.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 09:27 AM
Bob Bedford
 
Posts: n/a
Default Re: if in a query

I tried this:

select code,part,
case code
when 0 then (part as col2)
when 1 then (part as col3)
end
from list

but it doesn't work.

Is what you have understood I want to do ?

Bob



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 09:27 AM
Captain Paralytic
 
Posts: n/a
Default Re: if in a query

On 21 Mar, 17:35, "Bob Bedford" <b...@bedford.com> wrote:
> I tried this:
>
> select code,part,
> case code
> when 0 then (part as col2)
> when 1 then (part as col3)
> end
> from list
>
> but it doesn't work.
>
> Is what you have understood I want to do ?
>
> Bob


I thought code was 1 or 2 not 0 or 1??

SELECT
`code`
CASE `code`
WHEN 1 THEN `col2`
WHEN 2 THEN `col3`
END `part`

FROM `list`

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 09:27 AM
Bob Bedford
 
Posts: n/a
Default Re: if in a query

> SELECT
> `code`
> CASE `code`
> WHEN 1 THEN `col2`
> WHEN 2 THEN `col3`
> END `part`
>
> FROM `list`


well it doesn't work but I'll explain again as I don't know if it's clear
enough:

My query returns a code and an amount.

Here are some result rows:
code part
1 50
1 80
2 70
1 20
2 10

the result I'd like is:
code col2 col3
1 50
1 80
2 70
1 20
2 10

So when the code is 1 then the part value must go in col2
when the code is 2 then the part value must go in col3.

You query says that col2 is unknown. They are aliases (new created fields)
only for this query, they are no part of any table.

Bob



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 09:27 AM
strawberry
 
Posts: n/a
Default Re: if in a query

Bob Bedford wrote:
> > SELECT
> > `code`
> > CASE `code`
> > WHEN 1 THEN `col2`
> > WHEN 2 THEN `col3`
> > END `part`
> >
> > FROM `list`

>
> well it doesn't work but I'll explain again as I don't know if it's clear
> enough:
>
> My query returns a code and an amount.
>
> Here are some result rows:
> code part
> 1 50
> 1 80
> 2 70
> 1 20
> 2 10
>
> the result I'd like is:
> code col2 col3
> 1 50
> 1 80
> 2 70
> 1 20
> 2 10
>
> So when the code is 1 then the part value must go in col2
> when the code is 2 then the part value must go in col3.
>
> You query says that col2 is unknown. They are aliases (new created fields)
> only for this query, they are no part of any table.
>
> Bob


So what's your Primary Key?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-28-2008, 09:27 AM
Bob Bedford
 
Posts: n/a
Default Re: if in a query

> So what's your Primary Key?
I've simplified the query. I've an other field for primary key, called
idrecord.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-28-2008, 09:27 AM
strawberry
 
Posts: n/a
Default Re: if in a query

Bob Bedford wrote:
> > So what's your Primary Key?

> I've simplified the query. I've an other field for primary key, called
> idrecord.


Well why didn't you say so? So,does this work - except for the
counting bit obviously...

SELECT a1.idrecord,a1.code,a2.value col2,a3.value col3
FROM mytable a1
LEFT JOIN mytable a2 ON a1.idrecord = a2.idrecord AND a2.code =1
LEFT JOIN mytable a3 ON a1.idrecord = a3.idrecord AND a3.code =2;

You could always add a 'WHERE !isnull' type statement for neatness

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 05:52 AM.


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