SQL select for each record - sql

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 ?

Related

EXCEPT it exits in 2 other tables

I need to get all values from table product , EXCEPT they exits in 2 other table products. Is this query acceptable using 2 EXCEPTS ? Should this be done differently .
CREATE table missing_values
AS
select post
from product
EXCEPT
select post
from product_table_a
EXCEPT
select post
from product_table_b
;
As MatBailie say, you should try your query and see if that give you the result you want.
But in case that doesnt work you can solve it with a double not exists
This mean p1 isn't found neither in p2 or p3.
SELECT post
FROM product p1
WHERE not exists (SELECT p2.post
FROM product_table_a p2
WHERE p1.post = p2.post)
AND not exists (SELECT p3.post
FROM product_table_b p3
WHERE p1.post = p3.post)
Also this is a probably a more eficient way
SELECT post
FROM product p1
left join product_table_a p2
on p1.post = p2.post
left join product_table_b p3
on p1.post = p3.post
WHERE p2.post is null
and p3.post is null
A - B - C is the same as A - (B + c):
SELECT post
FROM product
EXCEPT
(
SELECT post
FROM product_table_a
UNION ALL
SELECT post
FROM product_table_b
) AS sum;

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.

SQL using same codes table for two different columns

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

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'

SQL: How compare cells from different tables?

I have two tables - band and band2. Band:
and band2:
The columns are equals. I'm using Access 2010. I want to SELECT rows WHERE band.Kampas<>band2.Kampas, but there isn't primary key so I can't use JOIN operation.
Maybe someone has an idea?
The answer:
Only in these rows band.Kampas<>band2.Kampas.
Thanks in advance.
SELECT b2.*
FROM band2 b2
WHERE b2.kampas NOT IN (SELECT b1.kampas
FROM band b1
WHERE b1.kampas IS NOT NULL)
AND b2.kampas IS NOT NULL
If I understand you correctly this is what you want:
Select * from band where kampas not in (select kampas from band2)
union
Select * from band2 where kampas not in (select kampas from band)
EDIT. Ok, might be that not in doesn't work in Access. It looks like this could work, though:
Select * from band2 where not exists (select * from band where band.kampas = band2.kampas)
This find a selection in the inner select where kampas's match and we want to pick those band2 lines that returns an empty selection in the inner select.
If you want to do this two-way (i.e. also find from band) just use union like I did in the first attempt.
How about this:
select *
from Band as B1
inner join Band2 as B2
on B1.Stotis = B2.Stotis
where B1.Kampas <> B2.Kampas