Update statement with Join in Informix - informix

I need to select an employee_id from a table
select emp_id
from employee emp, temp_data td where lower(td.supervisor_name) like lower(emp.last_name||emp.firstname)
and insert it into a field in the same table
update emp set emp.supervisor_id = **The value returned from the previous query**
from employee as emp
Inner join temp_table as td
on td.emp_id = emp.emp_id
Is there any way that I can achieve it? The problem is that I am referring to another field of the same table while setting it in the update statement.. Please let me know if there is any other route I could go... I am doing it in Informix.

I think you need to check your data
done following check
table : temp_data table has following data
supervisor_name
------------------------------
kaushal
abc
solanki
def
table : employee table has following values
firstname last_name
------------------------------
ajay jadeja
sachin solanki
kaushal kumar
manish galav
1.
select supervisor_name from temp_data
select firstname from employee
Note the result
1 record named kaushal which is match . So you need to update the the ID of kaushal
2.
select supervisor_name from temp_data
select last_name from employee
1 record named solanki matches. So you need to update the the ID of solanki
see the result
Note how many rows you need to update (here 2)
First try your select query put into update query
then you said it gaves error ..
then Try this :
Modified your select query as below and put it into your update query.
select emp_id
from employee emp, temp_data td
where
( lower(td.supervisor_name) like lower(emp.last_name )
or
lower(td.supervisor_name) like (emp.firstname) )
)
see the result 2 rows update or not ...????????

Try this also it successfully update your data.
MERGE INTO employee as emp
USING temp_data as td
ON emp.emp_id = td.emp_id
and ( lower(td.supervisor_name) like lower(emp.last_name)
or
lower(td.supervisor_name) like lower(emp.firstname) )
WHEN MATCHED THEN UPDATE set emp.supervisor_id = emp.emp_id

Related

Can anyone help me in this dbms query?

The Question-
Write a query to display the name(s) of the students who have secured the maximum marks in each subject, ordered by subject name in ascending order. If there are multiple toppers, display their names in alphabetical order.
Display it as subject_name and student_name.
O/P: first column - subject_name
second column - student_name
My answer-
select su.subject_name,st.student_name from subject su,student st,mark m
where m.student_id = st.student_id and m.subject_id = su.subject_id
and m.value = (select max(value) from mark group by subject_id);
Error-
and m.value = (select max(value) from mark group by subject_id)
*
ERROR at line 3:
ORA-01427: single-row subquery returns more than one row
What i know is I will have to make another subquery something like
and m.value =(select..... (select max(value) from mark group by subject_id));
But I am not getting it.
Nevermind got the answer, we will have to use IN operator and club value, subject_id as they are a set.
SELECT su.subject_name, st.student_name
FROM student s
JOIN mark m ON st.student_id = m.student_id
JOIN subject su ON m.subject_id = su.subject_id
WHERE (m.value, su.subject_id) IN (
SELECT max(value), subject_id FROM mark GROUP BY subject_id
)
ORDER BY su.subject_name, st.student_name;

How to get last inserted record Id in Gupta SQL Base

I am new to Gupta Sql Base. I would like to know how to get the last inserted record in Gupta SQL
If you are using SYSDBSequence.NextVal to generate your Primary Key, either within the Insert stmt, or prior to the Insert , then you can retrieve it back immediately after the Insert by Selecting Where [Primary Key] = SYSDBSequence.Currval e.g.
Select Name from Patient Where Patient_Id = SYSDBSequence.Currval
Alternatively, If your Primary Key column has been defined as AUTO_INCREMENT , you can select it back after the Insert using MAX( [Primary Key ] ) e.g.
Select Name from Patient Where Patient_Id = (Select MAX( Patient_Id) from Patient )
Alternatively, if none of the above, then write an Insert Trigger to either return it , or to store the PK in a table so you will always have the latest PK recorded for you.
You may like to join the Gupta users forum at enter link description here or there is much archived information at enter link description here

How to join two tables and return one row in PL/SQL?

I have 2 tables which names are FIRST and USERS. In FIRST table i have USERID1,USERID2 and USERID3 columns. And USERS table i save USERID's names and surnames values. If i join tables;
SELECT f.userid1, f.userid2, f.userid3, u.name, u.surname
FROM users u, first f
WHERE f.userid1=u.userid AND f.userid2=u.userid AND f.userid3=u.userid
i want to return this result in 1 row like;
121314 name surname 131415 name surname 141516 name surname
FIRST table result example;
no no2 userid1 userid2 userid3 ...
7 100000545 121314 131415 141516 ....
USER TABLE
id name surname
121314 black smoke
131415 jack shephard
141516 john locke
i want to result like user table but i have to join because coming result must according to records from FIRST table
Generally, to retrieve the result in one row, you will need to use aggregate function. You might try adapting the following query to your requirements:
WITH first AS
(
SELECT
7 AS no
,100000545 AS no2
,121314 AS userid1
,131415 AS userid2
,141516 AS userid3
FROM
dual
)
, users AS
(
SELECT
DECODE(LEVEL
,1,121314
,2,131415
,3,141516
) AS id
,DECODE(LEVEL
,1,'black'
,2,'jack'
,3,'john'
) AS name
,DECODE(LEVEL
,1,'smoke'
,2,'shephard'
,3,'locke'
) AS surname
FROM
dual
CONNECT BY LEVEL < 4
)
SELECT
TRIM(XMLAGG(XMLELEMENT(e, s.id||' '||s.name||' '||s.surname||' ')).EXTRACT('//text()').getClobVal()) AS one_row_result
FROM
first f
,users s
WHERE
s.id IN (f.userid1, f.userid2, f.userid3)
Not necessarily about plsql, but sql in general
In your query "u.userid" will always be a field in the same row. So, you won't get any result as long as f.userid1, f.userid2, f.userid3 don't hold the same value. What i think you want to achieve is to get 3 users, whose ids are stored in first table, so you have to reference "user"table three times.
To get a row as in your example, a query would look like:
SELECT f.userid1, u_first.name , u_first.surname,
f.userid2, u_second.name, u_second.surname,
f.userid3, u_third.name, u_third.surname
FROM users u_first, users u_second, users u_third, first f
WHERE f.userid1=u_first.userid
AND f.userid2=u_second.userid
AND f.userid3=u_third.userid
Someone will surely provide a correct join here.
Edit
For your example data this select will result in
121314 black smoke 131415 jack shepard 141516 john locke
Note however, that you don't specify for which row in First table you want to get this join, so you will get a result row for each row in First table, if rows from User table can satisfy those conditions (I hope you have constraints set properly)
If you want to fetch only one row for one specific Field row, add
AND f.no=7
at the end of the query
If the output you wish to receive is:
121314 black smoke
131415 jack shephard
141516 john locke
try:
SELECT f.userid1, u.name ,u.surname,
FROM users u, first f
WHERE f.userid1=u.userid
OR f.userid2=u.userid
OR f.userid3=u.userid
AND f.no=7
This is a good way to actually test, whether all users with those IDs exist in the database.
But remember, that the order in result will be the same as order of items in User table, so it won't neccessarly be the same as f.first and then f.second and then f.third

Join multiple FK values in one table

I'm writing some access queries and I'd like some help on a particular query. I'm still very new to SQL. Here is a simplified version of my tables:
Project Details
---------------
projectID (PK)
projectStartDate
projectEndDate
projectName
managerID (FK)
leadID (FK)
coleadID (FK)
Employee
--------
empID (PK)
empName
The managerID, leadID, and coleadID all correspond to an empID. I'd like to retrieve the Project Details table, but replace the IDs with the names of the employees. I've successfully been able to do this for one FK at a time using an inner join, but can't figure out a way to accomplish this for all roles.
It would also be nice to be able to change the attribute names on the results to managerName, leadName, and coleadName.
Thanks!
It's the same way, you probably have done it for one ID:
SELECT pd.*
, emp_m.empName ManagerName
, emp_l.empName LeadName
, emp_c.empName ColeadName
FROM ProjectDetails pd
, Employee emp_m
, Employee emp_l
, Employee emp_c
WHERE pd.managerID = emp_m.empID(+)
AND pd.leadID = emp_l.empID(+)
AND pd.coleadID = emp_c.empID(+)
The (+) is for an outer join, so it will select all records of the ProjectDetails table, no matter if it can match the manager, lead or colead.

SQL NOT IN Function

Iam trying to insert a record and i want to check that it is not already present in the table.
I try
INSERT INTO emp (empno, name)
VALUES(2, 'ram')
WHERE empno NOT IN (select empno from emp);
but it shows error 'incorrect syntax near where'
You can use following query to insert records into emp
If you are inserting one record at a time then following query will work as best as it can ...
insert into emp (empno,empname)
select distinct empno,empname
from ( 2 empno, 'ram' empname ) as a
where a.empname not in ( select empname from emp )
If you are willing to insert multiple records then just find below query
insert into emp (empno,empname)
select max(empno),empname
from ( select 2 empno, 'ram' empname
union
select 3 empno, 'ram1' empname
union
select 4 empno, 'ram' empname
) as a
where a.empname not in ( select empname from emp )
group by empname
You can't have WHERE clause on INSERT statements, you have WHERE clause only with SELECT/UPDATE
If you work with MySQL, you could do something like:
insert into emp (empno, name) values(2, 'ram') ON DUPLICATE KEY UPDATE name = 'ram'
And if you have a unique index on name column, you will be safe
You can use INSERT IGNORE to fail silently if the row exists. It will attempt the insert, but if the key exists it will do nothing.
INSERT IGNORE INTO emp (empno, name) VALUES (2, 'ram')
You might also want to take a look at INSERT ... ON DUPLICATE KEY UPDATE
you'd probably be looking for something like
insert into emp (empno, name)
SELECT 2 , 'ram'
FROM emp
WHERE 2 not in (select empno from emp)
???

Resources