SQL using same codes table for two different columns - sql

I have a table (Foo) that has two columns that store a code value from a codes table:
id - code1 - code2
1 - CC - DD
The Codes table:
Name - Code - Grouping
Call Center - CC - 22
County - DD - 54
I need a SQL that will pull 'Call Center' and 'County' based on the first table. It is assumed that I know Foo.code1 necessarily uses Codes.Grouping=22 and Foo.code2 uses Codes.Grouping=54.
I'm trying to write one SQL that will return both values.

Try this query:
select name from codes c inner join foo f on c.code = f.code1 or c.code = f.code2
Here is sqlfiddle

I am not really sure if this is the answer to your question, because I don't know exactly what you mean. I guess however, that you want to get both codes and groupings for an id value of your Foo table. For this I would
SELECT Foo.id,
C1.Name AS code1_name, C1.Code AS code1_code,
C1.Grouping AS code1_grouping,
C2.Name AS code2_name, C2.Code AS code2_code,
C2.Grouping AS code2_grouping
FROM Foo
INNER JOIN Codes AS C1 ON C1.Code = Foo.code1
INNER JOIN Codes AS C2 ON C2.Code = Foo.code2
WHERE Foo.id = 1;
expanding tuffkid sqlfiddlesqlfiddle

Related

SQL Join / Union

I have two statements that I want to merge into one output.
Statement One:
select name from auxiliary_variable_inquiry
where inquiry_idbr_code = '063'
Returns the following list of names:
Name
------------
Affiliates
NetBookValue
Parents
Worldbase
Statement Two:
select name, value from auxiliary_variable_value
where inquiry_idbr_code = '063'
and ru_ref = 20120000008
and period = 200912
Returns the following:
Name Value
-------------------
Affiliates 112
NetBookValue 225.700
I would like to have an output like this:
Name Value
-------------------
Affiliates 112
NetBookValue 225.700
Parents 0
Worldbase 0
So basically, if the second query only returns 2 names and values, I'd still like to display the complete set of names from the first query, with no values. If all four values were returned by both queries, then all four would be displayed.
Sorry I must add, im using Ingres SQL so im unable to use the ISNULL function.
You can do a left join. This ensures that all records from the first table will stay included. Where value is null, no child record was found, and we use coalesce to display 0 in these cases.
select i.name, COALESCE(v.Value,0) from auxiliary_variable_inquiry i
left join auxiliary_variable_value v
on v.inquiry_idbr_code = i.inquiry_idbr_code
and v.ru_ref = 20120000008
and v.period = 200912
where i.inquiry_idbr_code = '063'
I'd recommend a self-JOIN using the LEFT OUTER JOIN syntax. Include your 'extra' conditions from the second query in the JOIN condition, while the first conditions stay in the WHERE, like this:
select a.name, CASE WHEN b.Value IS NULL THEN 0 ELSE b.Value END AS Value
from
auxiliary_variable_inquiry a
LEFT JOIN
auxiliary_variable_inquiry b ON
a.name = b.name and -- replace this with your real ID-based JOIN
a.inquiry_idbr_code = b.inquiry_idbr_code AND
b.ru_ref = 20120000008 AND
b.period = 200912
where a.inquiry_idbr_code = '063'
if i got right, you should use something like:
SELECT i.NAME,
v.NAME,
v.value
FROM auxiliary_variable_inquiry i
LEFT JOIN auxiliary_variable_value v
ON i.inquiry_idbr_code = v.inquiry_idbr_code
WHERE v.ru_ref = 20120000008
AND v.period = 200912

SQL select for each record

i'm new to SQL and i'm getting mad trying to write a simple select to generate a table.
I have a table with the relation between different codes and i have to generate a table that correlate each code that is related from and to the same key, something like that:
TABLE CODES
CODE|KEY
-------------
A|B
C|B
D|B
E|F
G|F
GENERATED TABLE
--------------------
A|B
B|A
C|B
B|C
D|B
B|D
A|C
C|A
A|D
D|A
C|D
D|C
E|F
F|E
G|F
F|G
E|G
G|E
Hope i've been able to explain my problem.
Thanks
A self join that prevents the rows from joining to themselves
select c1.code,c2.code
from codes c1 join codes c2
on c1.key = c2.key and c1.code != c2.code
should return
A|C
A|D
C|A
C|D
D|A
D|C
E|G
G|E
work, however it would not return the B and F pairings you'd need rows B|B and F|F in your codes table
Alternatively you could union the results with your base table so:
select c1.code as code1,c2.code as code2
from codes c1 join codes c2
on c1.key = c2.key and c1.code != c2.code
union
select code as code1,key as code2
from codes
union
select key as code1,codes as code2
from codes
should return
Code1|Code2
A|C
C|A
A|D
D|A
C|D
D|C
E|G
G|E
A|B
C|B
D|B
E|F
G|F
B|A
B|C
B|D
F|E
F|G
I don't really understand your question?
To create a new table from a select statement you can use
SELECT *
INTO CORRELATED
FROM CODES
Is this what you mean ?

How to combine columns in sql based on another field

I have a table in SQL Server 2008 R2 like this:
Acc_id Bench-1 Bench-2
-------------------------------
1 xx
1 vv
2 pp
2 ii
3 kk
4 ll
Now, I want to combine this table on the basis of Acc_id column and get something like:
Acc_id Bench-1 Bench-2
---------------------------------
1 xx vv
2 pp ii
3 kk
4 ll
So, could someone please help me out.
SELECT ISNULL(b1.Acc_id,b2.Acc_id) as Acc_id,
b1.data,
b2.data
FROM Bench-1 AS b1 FULL OUTER JOIN
Bench-2 AS b2 ON b2.Acc_id = b1.Acc_id
Check Below query
SELECT DISTINCT a.acc_id,
b.bench_1,
c.bench_2
FROM table1 a
LEFT OUTER JOIN (SELECT acc_id,
bench_1
FROM table1
WHERE Isnull(bench_1, '') <> '') b
ON a.acc_id = b.acc_id
LEFT OUTER JOIN (SELECT acc_id,
bench_2
FROM table1
WHERE Isnull(bench_2, '') <> '') c
ON a.acc_id = c.acc_id
I would do this like:
select
Acc_id,
max([Bench-1)] as [Bench-1],
max(([Bench-2]) as [Bench-2]
from
myTable
group by
Acc_id
This assumes Acc_id won't have multiple rows with data in the same columns
If that is the case then your knowledge of the use of the results will come into play. I often will perform this more completely like
select
Acc_id,
min([Bench-1)] as [Bench-1Min],
max([Bench-1)] as [Bench-1Max],
Count([Bench-1)] as [Bench-1Count],
min([Bench-2)] as [Bench-2Min],
max([Bench-2)] as [Bench-2Max],
Count([Bench-2)] as [Bench-2Count],
from
myTable
group by
Acc_id
All this depends on the actual complexity of the real data and what you want to do with the results. If it IS actually as simple as you example the multi-join solution may work for you, but I often find that in more complex summarizations the group by solution give me results and performance I need.

Update a table by using a join [duplicate]

This question already has answers here:
Update a table using JOIN in SQL Server?
(13 answers)
Closed 8 years ago.
I have two tables ta, tb. ta columns - cId, c1, c2. c1 and c2 contain nulls and need to be filled with data. tb columns - cId, c3, c4. The data for c1 and c2 will come from c3 and c4 respectively.
So, I tried to do a simple inner join first. Both tables were aliased as al_ta and al_tb respectively. Then, I put an update statement -
UPDATE ta SET
al_ta.c1 = al_tb.c3,
al_ta.c2 = al_tb.c4
FROM ta AS al_ta
INNER JOIN tb AS al_tb
ON al_tb.cId = al_tb.cId
This does not work and I get an error - The multi-part identifier al_ta.c1 could not be bound. How do I make this work ?
Sample tables -
ta
cId c1 c2
1 NULL NULL
2 NULL NULL
3 NULL NULL
tb
cId c3 c4
1 11 111
2 22 222
3 33 333
4 44 444
When referencing the columns, you need to use the alias, not the base table name, if you've abstracted the table names away in the JOIN. Guessing at what your join might look like, you probably meant to write it this way:
UPDATE ta SET
ta.c1 = tb.c3,
ta.c2 = tb.c4
FROM dbo.some_long_table_name_a AS ta
INNER JOIN dbo.some_long_table_name_b AS tb
ON ta.cId = tb.cId
WHERE ta.c1 IS NULL OR ta.c2 IS NULL;
I don't understand the purposes of saying:
FROM ta AS al_ta
Why would you bother using an alias here that is actually harder to write than the original table name?
Please Try it
update ta set
ta.c1 = b.c3,
ta.c2 = b.c4
from ta a join tb b on a.cid = b.cid

How to do a subquery using the result obtained in the original query?

Here is the situation I have..I have to fetch all the associated cases for a given quoteId and this requires a join of 3 tables and I am able to come up with a query for that. Below is the sample : for brevity I have omitted some table name and used only Alias name.
SELECT distinct caseTable.CASEID, quoteHdrTable.Case_UID FROM
caseTable INNER JOIN quoteHdrTable ON
quoteHdrTable.Case_UID = caseTable.Case_UID WHERE quoteHdrTable.QUOTE_ID = '12345'.
Now for each CASE_UID that returns back, I also need to display its status from a different table. That has structure below.
STATUS_TABLE
CASE_UID STATUS
------------ -----------
123 Good
234 Bad.
345 {null}
In the end I want a result like
result
case_ID case_UID status
001 123 Good
Can we use subquery to do a 2nd SQL using the result(case_UID) from first..please provide pointers or a sample SQL statement.
FYI..using DB2 database
Thanks
Sandeep
SELECT distinct c.CASEID, q.Case_UID, s.status
FROM caseTable c
INNER JOIN quoteHdrTable q ON q.Case_UID = c.Case_UID
LEFT JOIN StatusTable s ON s.CASE_UID = q.CASE_UID
WHERE quoteHdrTable.QUOTE_ID = '12345'
Why not just add another JOIN?
SELECT distinct
caseTable.CASEID,
quoteHdrTable.Case_UID,
status.STATUS
FROM caseTable
INNER JOIN quoteHdrTable
ON quoteHdrTable.Case_UID = caseTable.Case_UID
INNER JOIN STATUS_TABLE status
ON quoteHdrTable.Case_UID = status.Case_UID
WHERE quoteHdrTable.QUOTE_ID = '12345'