Insert using selecting column from other table - sql

I have three tables as below
I want to insert the column dep_typ in table 2 as selecting the column from table 1.
But the dep_typ in table 2 has all 4 values as 'U' and whereas in table 1 it is three times 'U' and 1 time 'F'. I want the result to be as same as table 1.

The following will join the dep_type values from t1 with only the records from t2. Your data sample does not have any matching ex_line values common to t1 and t2. Is this true of the entire dataset? It if isn't true for the entire dataset, then #OldProgrammer is right - the join will not return the exact number of records in t1.
SELECT
t1.position,
t1.ss_id,
t2.ex_line,
t1.dep_Typ,
t1.num
FROM
t1
INNER JOIN
t2
ON t1.position = t2.position
AND t1.ss_id = t2.ss_id
INNER JOIN
t3
ON t1.ss_id = t3.ss_id
AND t1.value = t3.value
AND t2.vech_num = t3.vech_num
;

Related

SQL Query to Join Two Tables where data in one table is transposed

I'm trying to join two tables but not getting the desired results. Im new to SQL and will need help to joining these two tables.
The data in Table 2 is transposed and the table will only be populated only when the "value" field is populated with random values.
Details of Table 1 and Table 2 and expected output
You could use CTEs to split up the impact from outcome. Also, your screenshot had an error for task_id 6.
with t_impact as (
select task_id, value
from table2
where name = 'task_impact'
),
t_outcome as (
select task_id, value
from table2
where name = 'task_outcome'
)
select distinct t1.id,
t1.title,
i.value as task_impact,
o.value as task_outcome
from table1 t1
left join t_impact i
on t1.id = i.task_id
left join t_outcome o
on t1.id = o.task_id
order by t1.id
DB-fiddle found here.

Join table in oracle database

I have 2 tables that showing data Item master and BOM. I would like to join the tables between Item master as T1 and BOM as T2 and the additional table for table BOM as T3. Item master table containing ITM_CD, ITM_TYP (1,2,3,4) where each ITM_TYP represents a code for the first digit on ITM_CD. The thing that I want is like the picture below
CHILD_CD2 value replace to CHILD_CD1 value. So the data should be like this. What query should I fix ? I am very new using oracle query.
Here is mycode;
SELECT DISTINCT
T1.ITM_CD,
T2.C_ITM_CD AS CHILD_CD1,
T3.C_ITM_CD AS CHILD_CD2
FROM CM_HINMO_ALL T1
INNER JOIN (SELECT P_ITM_CD, C_ITM_CD, BOM_PTN FROM SM_BOM_ALL) T2
ON T1.ITM_CD = T2.P_ITM_CD
LEFT JOIN (SELECT P_ITM_CD, C_ITM_CD, BOM_PTN FROM SM_BOM_ALL) T3
ON T2.C_ITM_CD = t3.P_ITM_CD
WHERE 0=0
AND T2.BOM_PTN IN (1)
AND T1.ITM_TYP IN (1,2)
AND T1.ITM_CD = '110100370'
ORDER BY 2
Just use Case expression to replace the values.
SELECT ITM_CD, CASE WHEN CHILD_CD2 IS NULL THEN CHILD_CD2 ELSE CHILD_CD1 END AS CHILD_CD1
FROM TABLE1
If I understood, you want child_cd2 value should taken precedence over child_cd1 if available. If this assumption is right then we can use coalesce which returns the fist non null expression to achieve the same.
SELECT DISTINCT
T1.ITM_CD,
COALESCE(T3.C_ITM_CD,T2.C_ITM_CD) AS CHILD_CD1
FROM CM_HINMO_ALL T1
INNER JOIN SM_BOM_ALL T2
ON T1.ITM_CD = T2.P_ITM_CD
LEFT JOIN SM_BOM_ALL T3
ON T2.C_ITM_CD = t3.P_ITM_CD
WHERE T2.BOM_PTN IN (1)
AND T1.ITM_TYP IN (1,2)
AND T1.ITM_CD = '110100370'
ORDER BY 2

Return data when the OR operator conditions are true in sql server

I have the following query. Currently it checks on two conditions:
if one condition is true it will return the results for the first statement (t1.ticket=t2.ticket and ( t1.type=t2.type)
if this result is false then it will return results for the next condition ( t1.code=t2.code).
It does this is because sometimes this condition columns (t1.ticket=t2.ticket and ( t1.type=t2.type)) are equal to null and some times this condition colums ( t1.code=t2.code) are null thats why it switches between both.
But now what i noticed is that sometimes both the conditions return true and because of the OR statement its ignoring one of the conditions.
How do i return results for both of those conditions if they both there conditions are met? If its not met then they must return the one condition that matches.
select t1.name
,t1.ID
,t1.type
,t2.TicketID
,t2.Account
,t1.code
from table 1 t1
inner join table 2 t2
on (t1.ticketID=t2.ticketID and t1.type=t2.type)
or ( t1.code=t2.code)
left join table 3 t3
on t2.Res=t3.res
left join table 4 t4
on t3.IdDetail=t4.idDetail
you can try by using left join
select t1.name
,t1.ID
,COALESCE(t2.Ticket,t22.Ticket) as Ticket
,COALESCE(t2.Account,t22.Account) as Account
,COALESCE(t2.code,t22.code) as code
,t22.type
,t3.res
,t1.type
from table1 t1 left join table2 t2 on (t1.ticket=t2.ticket and t1.code=t2.code)
left join t22 on ( t1.type=t22.type)
left join table3 t3 on t2.Res=t3.res
left join table4 t4 on t3.IdDetail=t4.idDetail
If you want both result of table2 then no need use COALESCE function
My suggestion is to add a calculated column as hash of ticket, on both table1 and table2 code and type on all the tables, then using it as a new 'surrogate' key, a trigger to generate it each time a row is updated.
ALTER TABLE T1 ADD
NEW_KEY AS (hashbytes('SHA1',CONCAT(TICKET,CODE,TYPE)))
ALTER TABLE T2 ADD
NEW_KEY AS (hashbytes('SHA1',CONCAT(TICKET,CODE,TYPE)))
UPDATE TABLE T2 SET NEW_KEY = hashbytes('SHA1',CONCAT(TICKET,CODE,TYPE)) WHERE NEW_KEY IS NULL
UPDATE TABLE T1 SET NEW_KEY = hashbytes('SHA1',CONCAT(TICKET,CODE,TYPE)) WHERE NEW_KEY IS NULL

How to update Two Columns at the same time with different queries

I have this after insert trigger which updates two different columns based on a join. Basically it turns an Id into a value. This works fine except when one of the Ids does not match (ie, it's zero for the default) Then neither is updated. If the join fails, it should just insert null.
CREATE TRIGGER [AfterHistoryInsert]
ON [Jet].[HistoryEntity]
FOR INSERT
AS
BEGIN
Update t1 Set t1.OldValue = t2.Value, t1.NewValue = t3.Value
From Jet.HistoryEntity t1
join Jet.LookupListItemEntity t2 on Cast(t1.OldValue as int) = t2.Id
join Jet.LookupListItemEntity t3 on Cast(t1.NewValue as int) = t3.Id
inner join inserted i on i.Id = t1.Id
where t1.FieldName like '%Id'
END
Greg
Try left outer join instead join
If you are updating into a bigger table, one of the suggestion will be:
- Selecting all the columns that you want to update(In your case Value) into temp table
- Update Statement by using left outer join to temp table.

How do I Write a SQL Query With a Condition Involving a Second Table?

Table1
...
LogEntryID *PrimaryKey*
Value
ThresholdID - - - Link to the appropriate threshold being applied to this log entry.
...
Table2
...
ThresholdID *PrimaryKey*
Threshold
...
All fields are integers.
The "..." thingies are there to show that these tables hold a lot more imformation than just this. They are set up this way for a reason, and I can't change it at this point.
I need write a SQL statement to select every record from Table1 where the Value field in that particular log record is less than the Threshold field in the linked record of Table2.
I'm newish to SQL, so I know this is a basic question.
If anyone can show me how this SQL statement would be structured, it would be greatly appreciated.
SELECT T1.*
FROM Table1 T1
JOIN Table2 T2 ON T2.ThresholdID = T1.ThresholdID
WHERE T2.Threshold > T1.Value
SELECT t1.*
FROM dbo.Table1 t1 INNER JOIN dbo.Table2 t2 ON t1.ThresholdID = t2.ThresholdID
WHERE t2.Threshold > t1.Value
SELECT * from table1 t1 join table2 t2 on (t1.thresholdId = t2.thresholdId)
where t1.value < t2.threshold;
SELECT t1.LogEntryID, t1.Value, t1.ThresholdID
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.ThresholdID = t2.ThresholdID
WHERE t1.Value < t2.threshold
SELECT * FROM Table1
JOIN Table2
ON table1.ThresholdID = table2.ThresholdID --(assuming table 2 holds the same value to link them together)
WHERE
value < thresholdvalue
A 'JOIN' connects 2 tables based on the 'ON' clause (which can be multipart, using 'AND' and 'OR')
If you have 3 entries in table 2 which share table1's primary key (a one-to-many association) you will receive 3 rows in your result set.
for the tables below, for example:
Table 1:
Key Value
1 Hi
2 Bye
Table 2:
Table1Key 2nd_word
1 You
1 fellow
1 friend
2 now
this query:
SELECT * FROM Table1
JOIN Table2
on table1.key = table2.table1key
gets this result set:
Key Value Table1Key 2nd_word
1 Hi 1 You
1 Hi 1 fellow
1 Hi 1 friend
2 Bye 2 now
Note that JOIN will only return results when there is a match in the 2nd table, it will not return a result if there is no match. You can LEFT JOIN for that (all fields from the second table will be NULL).
JOINs can also be strung together, the result from the previous JOIN is used in place of the original table.