How to concatenate two tables with matched columns into a single view - sql

I have two tables in a Microsoft Access 2007 database.
One of them has columns "name 1 & name 2" with 2000 records. The other table has more information: it has two columns named "code1 & code2" with 3000 records. Each name has a code means that every name has a code stored in the second table in the column code 1.
I want to make one table or query that shows me name 1 & name 2 with their specific code1 & code 2 from the second table .
And it must have only 2000 records
example :
tabel 1 :
name-1 name-2
-----------------------------
Abacavir Digoxin
Amprenavir Aspirin
tabel 2 :
code-1 drug1
----------------
xy1 Abacavir
xy2 Digoxin
yxr1 Amprenavir
uyv2 Aspirin
sample output :
name-1 code-1 name-2 code-2
-----------------------------------------
Abacavir xy1 Digoxin xy2

This table structure is poorly designed but I'll assume you are stuck with it. You have to create a query that joins Table2 twice... once for each of the relationships you are linking by. In my test I created an access db and named my tables simple Table1 and Table2 so you may need to alter this query slightly to match your table/column names.
SELECT Table1.name1, Table2a.code1, Table1.name2, Table2b.code1
FROM (Table1 LEFT JOIN Table2 AS Table2b ON Table1.name2 = Table2b.drug1) LEFT JOIN Table2 AS Table2a ON Table1.name1 = Table2a.drug1;

The fact you have 2K in the first and 3K in the second is a clue. Try adding the 'Distinct' key in your SQL statement:
SELECT DISTINCT Table1.name1, Table2a.code1, Table1.name2, Table2b.code1
FROM (Table1 LEFT JOIN Table2 AS Table2b ON Table1.name2 = Table2b.drug1) LEFT JOIN Table2 AS Table2a ON Table1.name1 = Table2a.drug1;
Otherwise I think Frank nailed it.

Related

How do I find unmatched records with a table that contains comma separated values

I am trying to check if the values from Table1 exist in Table2.
The thing is that the values are comma separated in Table1
Table 1
ID
TXT
1
129(a),P24
2
P112
3
P24,XX
4
135(a),135(b)
Table 2
ID
P24
P112
P129(a)
135(a)
135(b)
The following only works if the complete cell value exists in both tables:
SELECT Table1.ID, Table1.TXT
FROM Table1 LEFT JOIN Table2 ON Table1.[TXT] = Table2.[ID]
WHERE (((Table2.ID) Is Null));
MY QUESTION IS:
Is there a way to check each comma separated value and return those that do not exists in Table 2.
In above example the value XX should end up in the result.
Not sure why you store your data in that way (which is bad practice as sos mentioned above), but you need to mimic the temp table like in SQL server.
Select from table1 and create different txt rows per id.
Insert the results from section 1 into the table3.
Select from table3 and join it to table2.
Delete table 3.
Table3 the temp table
ID
TXT
1
129(a)
1
P24
2
P112
3
P24
3
XX
4
135(a)
4
135(b)
Here is some explanation MS Access database (2010) how to create temporary table/procedure/view from Query Designer

Finding non-matching data in two tables as per columns and arranging column data as row by row

i have a requirement like below
i have two tables in same data base, both table have same structure and column count.but the columns not present in the same position.
ex:
table 1
id name age
1 dhileep 22
2 uday 33
table 2
id age name
1 20 udayga
2 22 uday
i have id column is same for all tables, if i change the table also i have id same, but may columns name and column count and data count will change.
my final output is:
column_name id table1 table 2
name 1 dhileep udayga
note: i gave above as example, the count of columns is more than 500 and data exist approximately 50000+
use Sql JOIN .to join the 2 tables
use the following answer .i think it is useful for u.
SELECT t1.id,t1.name,t2.name FROM table1 AS t1 JOIN table2 AS t2 ON t1.id = t2.id

Join on two dis-similar column

I need to join one column of a table to a column of another table.
Now these two column consists the geographic region data. But the issue is the column dont have exactly same strings od data.
For ex. Latin America in one column and LATM in another.
The data is table if had been same string would be the simplest joins but these two mean the same but then are different strings . What do I use to accomplish my task.
I need to do is
Select * from Table1 Inner Join Table2 on table1.region = table2.region
You would need to create a mapping table which maps every possible region in Table1.region to every possible region in Table2
for example your Mapping table be like.
MappingTable
--------------------------
Region1 | Region 2
--------------------------
Latin America | LATM
Europe | EUR
.....
The you can create a join like
Select *
from
Table1
inner join
MappingTable
on
Table1.region = MappingTable.Region1
inner join
Table2
on
MappingTable.Region2 = Table2.region
You need to make another table which contains information of TWO table Joining like 'Latin America' = 'LATM' and then have to use this table in join.

Inner join returns same columns from two tables access sql

I have 2 tables and i'm inner join them using EID
CSCcode Description BNO BNO-CSCcode E_ID
05078 blah1 5430 5430-05078 1098
05026 blah2 5431 5431-05026 1077
05026 blah3 5431 5431-05026 3011
04020 blah4 8580 8580-04020 3000
07620 blah5 7560 7560-07620 7890
07620 blah6 7560 7560-07620 8560
05020 blah1 5560 5560-04020 1056
Second table
y/n EID
y 1056
n 1098
y 1077
n 3011
y 3000
n 7890
n 8560
I'm selecting all fields from table one and y/n field from table 2. but it retrieve all from table 2 including EID. I don't want to retrieve EID from table2 because result table will have two EID fields.
My query
SELECT *, table2 .EID
FROM table1 INNER JOIN table2 ON table1 .E_ID = table2 .EID;
"I'm selecting all fields from table one"
No, you are selecting all fields from all tables. You need to specify the table if you only want all fields from one table:
SELECT table1.*, table2.EID
However, using * is not good practive. It's better to specify the fields that you want, so that any field that you add to the table isn't automatically included, as that might break your queries.
Your query is selecting everything from both tables as well as specifically the EID column from table 2. Change it to:
SELECT table1.*, table2.y/n
...
Although, as stated in other answers, avoid using * and instead list the column names table.columnName, etc.
Specify the table from which you want to select all fields modify your query to
SELECT
table1.*, table2.EID
FROM
table1
INNER JOIN table2
ON table1.E_ID = table2.EID;
If you are using the query designer, Access sometimes sets the property Output All Fields of the query to Yes. Make sure it is set to No and select the fields you want manually. (Right click in the designer and select Properties... in the context menu in order to see the properties.)
The reason you sometimes get stuck with a surprise * is because of a property in the query designer. Change this to No in the designer. Alternatively, like the other answers say, just remove the * from your query.
You can't do things like this 'SELECT *, table2 .EID' - you should include all your fields from table 1. However, it is not a good practice even if you are selecting from one table.
SELECT
table1.CSCcode,
table1.Descriptio,
table1.BNO,
table1.BNO-CSCcode,
table1.E_ID,
table2 .EID
FROM
table1 INNER JOIN table2 ON table1 .E_ID = table2 .EID

Update a single table based on data from multiple tables SQL Server 2005,2008

I need to update table one using data from table two. Table one and two are not related by any common column(s). Table three is related to table two.
Ex : table one(reg_det table)
reg_det_id | reg_id | results
101 | 11 | 344
table two :(temp table)
venue | results
Anheim convention center | 355
Table three (regmaster-tbl)
reg_id| venue
11 | Anaheim convention center
I need to update results column in table one using data from table two. But table one and two are not related. Table two and three and table one and three are related as you can see above. Can anyone please suggest any ideas! I need the results value to be 355 in table one and this data is coming from table 2, but these two are unrelated, and they can be related using table three. Sorry if it is confusing!
Fairly straight forward:
UPDATE T1
SET result = t2.results
FROM [table one] T1
INNER JOIN [table three] t3
on t1.reg_id = t3.reg_id
INNER JOIN [table two] T2
on t2.venue = t3.venue
Almost a question instead of an answer. :)
Couldn't you use an implied inner join?
UPDATE rd
SET rd.results = tt.results
FROM reg_det rd, regmaster rm, temptable tt
WHERE rm.reg_id = rd.reg_id
AND rm.venue = tt.venue;
I find it easier to read, and this syntax works in a SELECT statement, with the same meaning as an explicit inner join.
Try this:
UPDATE rd
SET rd.results = t.results
FROM reg_det rd
JOIN regmaster rm ON rm.reg_id = rd.reg_id
JOIN temptable t ON t.venue = rm.venue
WHERE t.results = 355
I added a WHERE clause because otherwise it will update all reg_det records that have matches in regmaster and temptable.