We help IT Professionals succeed at work.

mySQL, Self Join Table - more than a single Left Join?

0tacon
0tacon asked
on
737 Views
Last Modified: 2008-02-01
See:

https://www.experts-exchange.com/Database/MySQL/Q_23008349.html

For the basis of this question.


What I'm after now is to ascertain if, and how, it's possible to extend the query:

select
   a.asofdate as date,
   a.instrticker as hasA,
   a.updvalue as aValue,
   b.instrticker as hasB,
   b.updvalue as bValue
from
   transact.nav as a
left join
   transact.nav as b
on
   (a.asofdate = b.asofdate) and (b.instrticker = 'b')
where
   (a.instrticker = 'a');



To include a third variable ('c') which results in two additional columns.

This would thus take data from:

Table: transact.nav

asofdate   |  instrticker |  updvalue
2001          |    a            |   1
2001          |    b            |   2
2001          |    c            |   3
2003          |    a            |   4
2003          |    c            |   5
2008          |    a            |   6
2009          |    a            |   7
2010          |    a            |   8
2010          |    b            |   9
2011          |    b            |   10


Desired output:

asofdate    |  instrticker |  updvalue|  instrticker |  updvalue|  instrticker |  updvalue
2001          |    a             |   1            |    b            |    2            |    c            |    3
2003          |    a             |   4            |  null           |  null          |    c            |    5
2008          |    a             |   6            |  null           |  null          |    null         |    null
2009          |    a             |   7            |  null           |  null          |    null         |    null
2010          |    a             |   8            |   b             |    9            |    null         |    null


Any help, as ever, is greatly appreciated.
Comment
Watch Question

Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Brilliant, spot on! Many thanks
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.