Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL conditional join

Posted on 2014-09-30
3
Medium Priority
?
263 Views
Last Modified: 2014-10-01
Hi All,

I have a list of codes and options. A code can either have the option a, b, or c (for example):

A00001    a
A00001    b
A00002    a
A00002    c
A00003    b


In another table, I have these codes and options, mapped to prices

A00001    a    £1
A00001    b    £2
A00002    a    £3
A00002    c    £4
A00003    b   £5

However, in the second list, if all prices for all options are the same, then the code will have just one line, with the option of 'd' indicating that all options attract the same price.

A00004    d  £6

So my question is how do a write a join so that I return the correct price next to every code and option in the first list?

Thanks in advance.
0
Comment
Question by:James Elliott
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 800 total points
ID: 40352419
Not necessarily a Join, methinks.

select MyRef, MyLetter, MyPrice
from T2
where MyLetter<>'d'
UNION
select MyRef, 'a', MyPrice
from T2
where MyLetter='d'
UNION
select MyRef, 'b', MyPrice
from T2
where MyLetter='d'
UNION
select MyRef, 'c', MyPrice
from T2
where MyLetter='d'

Open in new window

0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1200 total points
ID: 40352481
SELECT t1.code, t1.option, t2.price
FROM table1 t1
LEFT OUTER JOIN table2 t2 ON
    t2.code = t1.code AND
    (t1.option = t2.option OR t2.option = 'D')
0
 
LVL 12

Author Closing Comment

by:James Elliott
ID: 40354002
Thanks. I went with the second solution but have awarded points to the first as it also taught me an approach with Union that I hadn't previously considered.

Rgds
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question