Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL conditional join

Posted on 2014-09-30
3
Medium Priority
?
265 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
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

772 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