I want to insert data from different tables in observation table - sql-server

OBSERVATION table-
O_ID, NAME ,OL_ID ,BP_ID ,OC_ID and OL_ID ,BP_ID ,OC_ID these three column contain FK.
OBSERVATION_CATEGORY table- OC_ID NAME
BUSINESS_PROCESS table- BP_ID NAME
OPERATING_LOCATION_1 table- ID NAME
I have 4 other tables in which ID column is there which has to inserted in observation table. In short I have to fetch data from 4 tables into one table.

FIRST CREATE TABLE AND CREATE SQL QUERY LIKE below:-
INSERT INTO table5
SELECT O_ID,OC_ID, oc.NAME,BP_ID bp.NAME,ol.ID,ol.NAME
FROM table1 ob INNER JOIN table2 oc ON ob.O_ID=oc.OC_ID
INNER JOIN table3 bp ON ob.O_ID=bp.BP_ID
INNER JOIN table4 ol ON ob.O_ID=ol.ID;
How ever you have NOT mention your DATABASE nor your exact problem.

Related

Multiple foreign key relationship to one table

How do you make a relationship between multiple foreign key values from one table to another table
First Table:
Item1a
Item1b
Item1c
Second Table:
ItemID
Description
Whereas Item1a, Item1b and Item1c gets their source from the second table: ItemID. Connecting them all in the relationship diagram doesn't change anything, how do I build their connection?
Thanks
You need three different JOIN for the second table.
SELECT <columns>
FROM FirstTable f
<LEFT/INNER> JOIN SecondTable s1 ON s.ItemID = Item1a
<LEFT/INNER> JOIN SecondTable s2 ON s.ItemID = Item1b
<LEFT/INNER> JOIN SecondTable s3 ON s.ItemID = Item1c
Hope this help.

Join Query Combining Two Tables

I need to display 3 columns from table A and one column from table B. I used the following join query to combine them but not getting the value in the expected columns.
example1 -this is working.(If I display one column from table A and one column from table B it works)
SELECT
tableA.col1(fkid),
tableB.col1
FROM tableA
INNER JOIN tableB
ON tableA.fkid = tableB.pkid;
I need to display 3 columns from table A and one column from table B the below query is not working.
SELECT
tableA.col1,
tableA.col2,
tableA.col3,
tableB.col1
FROM tableA
FULL JOIN tableB
ON tableA.fkid = tableB.pkid;
Original query:
select device.name,device.description, device.fkphonetemplate,phonetemplate.name from device inner join phonetemplate ON device.fkphonetemplate=phonetemplate.pkid;
Result:
description fkphonetemplate name
Nikhil (nkalantr) 10ce46f6-615d-4605-9f42-454225df5647 ARRAY(0xc7153b0)
Expected result should be:
description fkphonetemplate name
Nikhil (nkalantr) 10ce46f6-615d-4605-9f42-454225df5647 Standard 7960 SCCP
I don't get name from device table in the result and the name from phonetemplate table shows something as Array0X... but I need to get the name of phonetemplate like Standard 7960 as shown in expected result.
Can you refine my query or suggest what's wrong with the 2nd query?
Any reason for you to using full join instead of inner join ?
If no,try using inner join.
SELECT
a.col1,
a.col2,
a.col3,
b.col1
FROM tableA a
INNER JOIN tableB b
ON a.fkid = b.pkid;
Can you provide your database sample data , so we can clearly know what your need.

Merging Two Tables in SQL Server

I have two tables, each with some columns that are the same. However, each table also contains data that is unique. (Similar data includes a row name).
What I need to do is tack on the data from table two to it's matching row in table one (matching the name column).
Is there any way to do this?
I need stuff from table two to go into table 1 where the names match:
The following query should return all matching rows with columns from both tables. Note that any unique rows (that only exist in table one or two) will be excluded.
SELECT
one.matchingColum,
one.oddColum,
two.evenColumn
FROM one
JOIN two on one.matchingColumn = two.matchingColumn
If the data types are the same, then you can do a union
SELECT *
FROM table1
UNION
SELECT *
FROM table2
If the datatypes are not the same and you have a field that you can JOIN on, then you can do a JOIN
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id

SQL Server many-to-many query

I have two tables, let's call them table1 and table2. They both have a column called ID1 and ID2 which are respective PK for each of two tables.
I have also another table, called table3 which contains ID1 and ID2, establishing a many to many relation between these two tables.
What I need is to get all the records from table2 that are not related with records in table1.
Ex.
Table 1 contains a record with ID = 1
Table 2 contains two records with ID 1, 2
Table 3 contains one record with values 1 - 1
I need a query that will give me as result 2.
Can anyone suggest me a way to proceed?
Thanks
SELECT t2.ID2
FROM table2 t2
WHERE NOT EXISTS(SELECT NULL
FROM table3 t3
WHERE t3.ID2 = t2.ID2);
You could also use a LEFT JOIN:
SELECT t2.ID2
FROM table2 t2
LEFT JOIN table3 t3
ON t2.ID2 = t3.ID2
WHERE t3.ID2 IS NULL;

T-SQL Join Query

I have been trying to write a query for a view but I can't seem to get it... I have two tables that I need to join... but what I need is that for every ID in table 1 I get all records on table 2. For Example:
Table 1 Table 2
Jhon Car
Bill House
Jet
I would like to get:
Table 3
Jhon Car
Jhon House
Jhon Jet
Bill Car
Bill House
Bill Jet
P.S. Data on both tables can vary.
P.S.S. Table 1 is actually a Left Outer Join between two other tables where the first table contains the indexes and the second contains the field used to create relation to Table 2.
You need a CROSS JOIN for this (AKA Cartesian Product).
SELECT t1.col, t2.col
FROM Table1 t1 cross join Table2 t2
Try this
select * from table1, table2
or use a CROSS JOIN if database supports it
SELECT *
FROM table1
CROSS JOIN
table2
select columns you want to get
from Table1 Cross Join Table2

Resources