I'm struggling with a SQL join query, maybe you could help me.
I have to join two tables. In one table, the primary key is a composite key from two columns to uniquely define a row.
In the other table, I just have one of these two rows.
Table looks like this:
Table 1
Type (PK)
Code (PK)
Description
f.e.
Type
Code
Description
0
A
potential A
0
A+
potential A+
1
1
Premium
1
2
Mainstream
2
EFI
EFI
2
LK
Local Key
3
100
exclusive
3
105
inclusive
In the second table, I have the code as the foreign key, but multiple times:
Table 2
Number (PK)
Name
Address
Code 1 (FK)
Code 2 (FK)
Code 3 (FK)
I want a query, where it shows me, f.e the customer name, code 1, code 2 and code 3 and the corresponding description.
If I have a query like this:
SELECT a.Number, a.Name, a.Code1, b.description, a.Code2, b.description
FROM Table1 a
INNER JOIN Table2 b ON a.Code1 = b.Code
it always shows me the first description. So, if Code 1 = A -> Description: potential A, if Code 2 = 1 -> Description = potential A - even if the correct description is "Premium".
I hope it's clear what I'm trying to do.
Thanks for your help!
Table 1 sample data:
Number (PK)
Name
Address
Code1
Code2
Code 3
Code 4
1587452
BMW
Downstreet5
A
2
LK
105
1887525
Audi
Abbyroad 7
A+
1
EFI
100
Table 2 sample data see above
Expected output:
New Query / View with
Name
Code1
Description Code 1
Code2
Description Code 2
BMW
A
potential A
2
Mainstream
What I get:
SELECT a.Number, a.Name, a.Code1, b.description, a.Code2, b.description
FROM Table1 a
INNER JOIN Table2 b ON a.Code1 = b.Code
| BMW | A | potential A | 2 | potential A | LK | potential A | 105 | potential A |
Solution if someone else does have the same problem:
I solved it with a subquery, like:
SELECT a.Number, a.Name, a.Code1,Description =
(SELECT b.description
FROM Table2 b
WHERE a.Code1 = b.Code),
a.Code2, Description =
(SELECT b.description
FROM Table2 b
WHERE a.Code2 = b.Code),
FROM Table1 a
Thanks for your help!
Related
I have two SQL tables
TABLE A
id | user | embedding
-----------------------
1 Ram [.12,.56]
2 Shyam [.23,.24]
3 Ghanshyam [.23,.39]
4 Balram [.34,.39]
TABLE B
--------------------
id | users
--------------------
1 [Ram,Shyam]
2 [Ram,Ghanshyam]
3 [Ram, Balram]
And I want to have a query that will return essentially table B but with the users replaced by their embeddings.
Desired output
-----------------------------
id | users
-----------------------------
1 [[.12,.56],[.23.,.24]]
2 [[.12,.56],[.23,.39]]
3 [[.12,.56], [.34,.39]]
How can I do this?
how about using unnest and array_agg:
select b.id , array_agg(embedding)
from TableB b
cross join unnest(b.users) c(user)
join TableA a
on c.users = a.user
group by b.Id
I have a table like below:
ID Role CompanyID
1 T 123
2 S 1234
3 B 12345
T's company id has relationship with S's and B's company ids.The value of their relationship is in another table like below:
Rel CompanyId1 CompanyId
Private 1234 123
Public 12345 123
Of course first I have to join the two tables and then I have to get the relationship of T(123) with S(1234) and B(12345) from the table 2 and on the basis of that I have to create another column which will be if the relationship of T with S and B is private then 1 and if public then 0
It should be like this
Id Role Companyid BoolCol
1 T 123 for T there is no need to fill this col can be null
2 S 1234 1 becoz rel b/w T and S is private
3 B 12345 0 becoz rel b/w T and B is public
The relationship is only of T with another companies not the other way around
Any help will be appreciated.......
Thanks
You can join the table using Left join on CompanyId in first table and CompanyId1 in second table and use case when statement to get the BoolCol column.
select a.*,case when b.rel = 'Private' then 1
when b.rel = 'Public' then 0
else NULL
end as BoolCol
from table1 a
left join
table2 b
on a.companyid = b.companyid1;
Let me know in case you of any queries.
you can try this code:
select id, role,a.companyid,rel,case when rel='Private' then 1 when rel='Public' then 0 else null end as boolcol
from first_table a left join relation_table b
on a.companid=b.companyid1
I am struggeling with a left join on a Raima database. I'd like to add an additional join condition, but in this case the join behaves like an inner join, thus I am losing some of the expected results.
Example:
TABLE_A
ID
-------
1
2
3
4
.
TABLE_B
A_ID | B
--------
1 | 1
2 | 1
2 | 2
3 | 2
Query
select * from TABLE_A left join TABLE_B
on TABLE_A.ID = TABLE_B.A_ID
and TABLE_B.B = 1
I am expecting the following result:
1 1 1
2 2 1
3 null null
4 null null
E.g. on an Oracle 11g I get the expected result, but on the Raima it shows me only the first two results. What is the problem here and how to fix it?
You need this
select * from A left join
(select * from B where B=1) bd
on A.ID = bd.A_ID
The query you gave will not give you expected result in oracle also. This will.
PS: Please use different names for table and column
Hello I need some help regarding SQL Query.
Table A
ID Name Type
---------------------
1 abc BC
---------------------
2 def SD
---------------------
3 ghi BC
----------------------
Table B (BC_ID and SD_ID are Foreign keys of Table 'B' referencing the Primary key 'ID' of Table 'A')
ID BC_ID SD_ID
---------------------
1 1 2
---------------------
2 3 2
---------------------
I am using VB.Net datagrid view to print the Table B details. I need to print 2 more columns in datagridview which specifies the Name of the corresponding BC_ID and SD_ID.
How do I write a SQL query which can get the 'Name' of the Corresponding ID's from the database and print in datagridview...
Expected Output in datagridview:
ID BC_Name SD_Name
---------------------
1 abc def
---------------------
2 ghi def
---------------------
I dont want to have separate BC_Name and SD_Name columns in the table B..I just need to join the two tables and get the name in a single query..Is it possible? Thanks in advance for your help
You need to join TableA twice with different alias names.
Try this:
SELECT B.ID,ISNULL(A1.NAME,'') as BC_NAME,ISNULL(A2.NAME,'') as SD_NAME
FROM TableB B LEFT JOIN
TableA A1 ON A1.ID=B.BC_ID LEFT JOIN
TableA A2 ON A2.ID=B.SD_ID
Result:
ID BC_NAME SD_NAME
1 abc def
2 ghi def
See result in SQL Fiddle.
I would like to know what's the logic for multiple joins (for example below)
SELECT * FROM B returns 100 rows
SELECT B.* FROM B LEFT JOIN C ON B.ID = C.ID returns 120 rows
As I know using left join will returns any matching data from the left table which is B if data are found for both table. But how come when using left join, it returns more data than table B itself?
What am I do wrong or misunderstood here? Any guidance are very appreciated. Thanks in advance.
Let be table B:
id
----
1
2
3
Let be table C
id name
------------
1 John
2 Mary
2 Anne
3 Stef
Any id from b is matched with ids from c, then id=2 will be matched twice. So a left join on id will return 4 rows even if base table B has 3 rows.
Now look at a more evil example:
Table B
id
----
1
2
2
3
4
table C
id name
------------
1 John
2 Mary
2 Anne
3 Stef
Every id from b is matched with ids from c, then first id=2 will be matched twice and second id=2 will be matched twice so the result of
select b.id, c.name
from b left join c on (b.id = c.id)
will be
id name
------------
1 John
2 Mary
2 Mary
2 Anne
2 Anne
3 Stef
4 (null)
The id=4 is not matched but appears in the result because is a left join.
Look at the following example :
B = {1,2}
C = {(1,a),(1,b),(1,c),(1,d),(1,e)}
The result of B left join C will be :
1 | a
1 | b
1 | c
1 | d
1 | e
2 | null
The number of rows in the result is definitely larger than rows in B (2).
In general the number of rows in result of B left join C is bounded by B.size + C.size and not only by B.size as you think...
As per your query it do the join to B Table with C and B table is Left Table so it will display all the records of Left table in our case it is B and related from other Table in our Case it is C.