Overlapping Records in t_package, t_object, t_connector - primary-key

A package is also a model element. Therefore, there are Package records in t_object.
What is the primary key field that relates t_package and t_object?
Select * from t_package
inner join t_object
on t_package.? = t_object.?

They both have the same ea_guid, but the t_object.pdata1 also corresponds with the t_Package.Package_ID.

Related

Delete rows from table inner join

I'm having a problem, I explain:
I have a table called Tipo_Base that contains Id, nombre_tipo_base
I have a table called Tipo_Base_Lista that contains Id, id_tipo_base, and lista_id
I have a table called Modelo_unidad that contains Id, nombre_modelo and id_tipo_base
I have a table called Modelo_Lista that contains Id, id_modelo, id_lista
Each id_lista of the table modelo_lista MUST be present in the table tipo_base_lista, then, when I delete a id_lista from the table tipo_base_lista, it must also be deleted from the table modelo_lista.
Try the following:
DELETE Tbl_modelo_lista
FROM
Tbl_modelo_lista
INNER JOIN Tbl_modelo_unidad as MU ON MU.id_modelo = Tbl_modelo_lista.id_modelo
INNER JOIN Tbl_tipo_base_lista as TBL ON TBL.id_tipo_base = MU.id_tipo_base
WHERE
TBL.id_lista <> Tbl_modelo_lista.id_lista
I think the logic you want is more like this:
DELETE ml
FROM Tbl_modelo_lista ml INNER JOIN
Tbl_modelo_unidad mu
ON mu.id_modelo = ml.id_modelo LEFT JOIN
Tbl_tipo_base_lista tbl
ON tbl.id_tipo_base = mu.id_tipo_base AND
tbl.id_lista = ml.id_lista
WHERE tbl.id_lista IS NULL;
Normally, the way to implement this logic is with a cascading delete constraint. In your case, I'm not sure this would work. Cascading triggers are useful when you need to propagate changes from the reference table outwards. They don't keep track of incoming references and delete a record when there are no references.

Join two tables in SQL Report Builder with mutiple conditions

I am using SQL Report Builder 2016.
I have 2 tables, named assets and DepreciationInfo,
following is the structure of these tables.
Table Assets:
ID|Name|Cost|Prior Dep|Prior Dep Period|Use Prior|
values would be like
123|Name|10000|4000|06/03/2014|True|
Table DepreciationInfo:
ID|EndDate|CurrentDepreciation|AccumulatedDepreciation|CarryingValue|Monthly|
values would be like
123|2020-04-30 00:00:00.000|2000|5000|5000|0/1|
I want to achieve following;
I want to select id from table assets, and will show all fields mentioned above from table assets along with fileds from table dep info based on "ID" , Column "ID" is same in both tables.
I am successful in getting all values when Id is common in both table using below mentioned query.
SELECT
Assets.ID
,Assets.Name
,Assets.Cost
,Assets.Prior Dep
,Assets.Prior Dep Period
,Assets.Use Prior
,DepreciationInfo.EndDate
,DepreciationInfo.CurrentDepreciation
,DepreciationInfo.AccumulatedDepreciation
,DepreciationInfo.CarryingValue
,DepreciationInfo.DepID
,DepreciationInfo.Monthly
FROM
Assets
INNER JOIN DepreciationInfo
ON Assets.AssetID = DepreciationInfo.AssetID
where DepreciationInfo.EndDate=#EndDate and DepreciationInfo.Monthly=0
What i want is that i want to show all results from table asset whether or not such id existed in table DepreciationInfo.
I tried all outer joins and result is same, it is showing number of records with Inner and Outer join.
Any help would be appreciated.
Change your inner join to a left join and you will see all results from table asset whether or not a corresponding assetid value exists in table DepreciationInfo. The only issue here is that the query will still be limited by your two filters in the where clause. I suggest you change them to filter off of fields in the asset table if possible:
SELECT
Assets.ID
,Assets.Name
,Assets.Cost
,Assets.Prior Dep
,Assets.Prior Dep Period
,Assets.Use Prior
,DepreciationInfo.EndDate
,DepreciationInfo.CurrentDepreciation
,DepreciationInfo.AccumulatedDepreciation
,DepreciationInfo.CarryingValue
,DepreciationInfo.DepID
,DepreciationInfo.Monthly
FROM
Assets
Left JOIN DepreciationInfo
ON Assets.AssetID = DepreciationInfo.AssetID
where Asset.EndDateorOtherCorrespondingDateValue=#EndDate and Asset.MonthlyorOtherCorrespondingValue=0
Move your where into the join and change to left join
SELECT
Assets.ID
,Assets.Name
,Assets.Cost
,Assets.Prior Dep
,Assets.Prior Dep Period
,Assets.Use Prior
,DepreciationInfo.EndDate
,DepreciationInfo.CurrentDepreciation
,DepreciationInfo.AccumulatedDepreciation
,DepreciationInfo.CarryingValue
,DepreciationInfo.DepID
,DepreciationInfo.Monthly
FROM
Assets
left JOIN DepreciationInfo
ON Assets.AssetID = DepreciationInfo.AssetID
and DepreciationInfo.EndDate=#EndDate
and DepreciationInfo.Monthly=0
Returns all assets and the matches to depreciation based on you ON conditions.
You cannot have left joined objects in your where clause or you convert that left join into an inner join (because the nulls are eliminated).

Two times froeign key from the same table

I have a problem to query such design
I need select this information (student_beginner, student_finalYear, project). The problem I have, in the "student_project" table are just integers from the primary keys from "student" and "project" tables. In the Result must be the actual values from "student" and "project" tables.
I donĀ“t know exactly what do you mean. I will try to guess:
Do you mean this:
Select student_beginner, student_finalYear, project from student_project
This will select the fields you want.
On the other hand, this query returns the project's name intead of project's ID:
Select sp.student_beginner, sp.student_finalYear, p.project_name
FROM student_project sp
JOIN project p ON sp.project = p.id
Or you can this in order to JOIN the three tables. Note that in this case I'm also returnin the student's ID.
Select s.name, sp.student_beginner, sp.student_finalYear, p.project_name
FROM student_project sp
JOIN project p ON sp.project = p.id
JOIN student s ON sp.id = s.id

When performing an INNER JOIN on two tables, which table should I reference for duplicate column names?

I have two tables that I am joining, the EventRequest table and the Customer table. The Customer.CustNo is a primary key and the EventRequest.CustNo is a foreign key:
SELECT EventNo, DateHeld, Customer.CustNo, CustName
FROM EventRequest
INNER JOIN Customer ON EventRequest.CustNo = Customer.CustNo
My question is, is there any difference between using Customer.CustNo instead of EventRequst.CustNo in the SELECT statement when doing an INNER JOIN? Is one preferred over the other and why?
There is no difference, but I prefer use the field of the main table.
Or the field from the table that you are retrieving more fields.
It does not matter which table's column you reference to make an inner join as long as 2 tables have the column name same. I would prefer using the primary key column.

Assistance with simple JOIN

I have got a problem in selecting the employees name from employees table(empID) which is primary key and from consignation table based on two foreign key 1- consignee and 2- hand over by ( these two fields have relation with empID) so is it possible to select consignee and handoverby not by ID but based on Employees name(emp.name). Please write the query, thanks in adv.
Basically, you can use INNER JOIN if both column from consignation table are non-nullable. But if one is nullable, you need to use LEFT JOIN so records from consignation will still be shown on the list.
SELECT con.EmpName AS consigneeName,
ho.EmpName AS handOverName
FROM consignation a
INNER JOIN employee con
ON a.ConSignee = con.empID
INNER JOIN employee ho
ON a.handoverby = ho.empID
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins