I have tow tables, one collar table with drillhole unique numbers with their x,y,z values and second table with drillhole numbers with geology information and one drillhole can have many geology information based on drillhole depth. I want to join two table. Would you please how can I join these two table in ms access. I want all the data from drillhole collar table and only matching data from geology table. I am trying but could not succeed. Please advise.enter image description here
Two Tables
If you want all data from a master table (drillhole collar table) plus matching data from a child table (geology table), this is achieved by using a LEFT JOIN query. Adapt this example by replacing the table and column names with ones that you need from your database:
SELECT d.*, g.*
FROM Drillhole d
LEFT JOIN Geology g ON d.Hole_ID = g.Hole_ID
Related
I have 2 tables - A Spend Table and a Sales Table
Spend Table Schema:
Spend_ID,
Spend_amount
Sales Table Schema:
Sales_ID_A,
Sales_ID_B,
Sales_amount
I want to do a left join of the spend table to the sales table. The join key from the spend table is the Spend_ID and i want to join when it matches the value in either Sales_ID_A or Sales_ID_B i.e. my match key on the sales table is in 2 columns. So in a way its like applying an 'or' condition to the join on the sales table Sales_ID_A or Sales_ID_B. If a match is found in Sales_ID_A, then no need to check Sales_ID_B. Only check Sales_ID_B if no match is found in Sales_ID_A. How do i achieve this with SQL?
Sample Data Illustration:
See screenshot
The columns called sales_id_a and sales_id_b are actually spend IDs and should better be called spend_id_a and spend_id_b.
You want to join on the first ID, but if that is null, you want to join on the second ID. Use COALESCE for this:
select *
from sales
left join spend on spend.spend_id = coalesce(sales.sales_id_a, sales.sales_id_b)
I have a table A, which has the columns:
cat
rat
dog
pig
I need to paste/replicate this columns in table B and table C.
But I don't want to create two INSERT INTO queries.
I saw using INNER JOIN to join two tables in one, but not the other way (that I want).
Is there any way to do it?
i have a query which is used to generate reports. There are multiple fields to be displayed. One requirement is such that i need to join one table to different tables with different aliases for data. e.g., table 1 employee id with employee table for knowing the full name. similarly table 2 employee id with employee table for table 2 employee id full name. PFB the query:
select * from office o
left join employee e
on e.id=o.id
left join master m
on m.id=o.id
left join student s1
on e.id=s1.id
left join student s2
on m.id=s2.id
Can we optimize this query to use only one join statement of student table instead of multiple table join statement? I need to reduce the number of tables used in the query since i'm getting the error as too many tables in the query maximum allowed is 50. Please help. Appreciate.
Can we optimize this query to use only one join statement of student
table instead of multiple table join statement?
No, I would not call it query optimization. However, for reporting purposes you reduce the joins by creating views.
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc00801.1510/html/iqrefso/X315714.htm
I have two tables in a database. The tables are called bi_employee and hr_employee. These two tables have few similar columns and then other extra columns specific to each table.
I need to reconcile the data between these two tables based on some user defined columns.
Lets say the common columns are id(pk), emp_code, region, country, title, division etc.
Now when I reconcile these two tables, I would want to see the rows which are there in both the tables but only differ in some columns.
e.g. emp_code 1000 is prsent in both the tables but in hr_employee his title is jr. developer but in bi_employee his title is sr.developer.
I do not want the records which are in one table but not in another table.
I only need to reconcile the rows which are present in both the tables but on columnwise which will be selected by the user.
User may chose to reconcile based on title or region or country or all of them.
Please help.
EDIT 1:
This is what I have done so far, with the following query I could get all the records which are there in both the tables. Now I just need to compare their columns to see if there are any mismatches.
SELECT emp_code FROM bi_employee INTERSECT SELECT emp_code FROM hr_employee
From what I understand, there is only one column that relates the recodes in each table; emp_code. Then you want to show records where the emp_code is the same in each talbe, but other field(s) are different.
You can do that with a simple join and filter in a WHERE clause...
SELECT
*
FROM
bi_employee
INNER JOIN
hr_employee
ON bi_employee.emp_code = hr_employee.emp_code
WHERE
(bi_employee.title <> hr_employee.title)
OR (bi_employee.region <> hr_employee.region)
etc, etc
(If any fields are nullable, you'll need to account for that with something like ISNULL(bi.x, '') <> ISNULL(hr.x, '')).
You might try this.
select hr.<list of columns to reconcile from hr table>
from bi_employee bi join hr_employee hr on hr.emp_code = bi.empcode
minus
select bi.<list of columns to reconcile from bi table>
from bi_employee bi join hr_employee hr on hr.emp_code = bi.empcode
I have a database with two tables
One with games
and one with participants
A game is able to have more participants and these are in a different table.
Is there a way to combine these two into one query?
Thanks
You can combine them using the JOIN operator.
Something like
SELECT *
FROM games g
INNER JOIN participants p ON p.gameid = g.gameid
Explanation on JOIN operators
INNER JOIN - Match rows between the two tables specified in the INNER
JOIN statement based on one or more
columns having matching data.
Preferably the join is based on
referential integrity enforcing the
relationship between the tables to
ensure data integrity.
o Just to add a little commentary to the basic definitions
above, in general the INNER JOIN
option is considered to be the most
common join needed in applications
and/or queries. Although that is the
case in some environments, it is
really dependent on the database
design, referential integrity and data
needed for the application. As such,
please take the time to understand the
data being requested then select the
proper join option.
o Although most join logic is based on matching values between
the two columns specified, it is
possible to also include logic using
greater than, less than, not equals,
etc.
LEFT OUTER JOIN - Based on the two tables specified in the join
clause, all data is returned from the
left table. On the right table, the
matching data is returned in addition
to NULL values where a record exists
in the left table, but not in the
right table.
o Another item to keep in mind is that the LEFT and RIGHT OUTER
JOIN logic is opposite of one another.
So you can change either the order of
the tables in the specific join
statement or change the JOIN from left
to right or vice versa and get the
same results.
RIGHT OUTER JOIN - Based on the two tables specified in the join
clause, all data is returned from the
right table. On the left table, the
matching data is returned in addition
to NULL values where a record exists
in the right table but not in the left
table.
Self -Join - In this circumstance, the same table is
specified twice with two different
aliases in order to match the data
within the same table.
CROSS JOIN - Based on the two tables specified in the join clause, a
Cartesian product is created if a
WHERE clause does filter the rows.
The size of the Cartesian product is
based on multiplying the number of
rows from the left table by the number
of rows in the right table. Please
heed caution when using a CROSS JOIN.
FULL JOIN - Based on the two tables specified in the join clause,
all data is returned from both tables
regardless of matching data.
example
table Game has columns (gameName, gameID)
table Participant has columns (participantID, participantName, gameID)
the GameID column is the "link" between the 2 tables. you need a common column you can join between 2 tables.
SELECT gameName, participantName
FROM Game g
JOIN Participat p ON g.gameID = p.gameID
This will return a data set of all games and the participants for those games.
The list of games will be redundant unless you structure it some other way due to multiple participants to that game.
sample data
WOW Bob
WOW Jake
StarCraft2 Neal
Warcraft3 James
Warcraft3 Rich
Diablo Chris