Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with update query

Status
Not open for further replies.

egstatus

Programmer
Apr 14, 2005
143
US
Hi all, I need help with the following query.

I have a view [V_GEN_EMPLEADOS_CON_SALARIO_ZERO] that return all the employees with salary being equal to zero.
[There are 4 different companies within the corporation, so the view return records from each of those companies.]

This is the view code:
SQL:
SELECT        CORR_EMPRESA, CORR_EMPLEADO, NOMBRE_EMPLEADO, DOCUMENTO_IDENTIDAD, CORR_TIPO_EMPLEADO, FECHA_INGRESO, SEXO, 
                         DIRECCION_PARTICULAR, SALARIO_MENSUAL, CORR_PUESTO, ESTADO_ACTIVO
FROM            dbo.GEN_EMPLEADO
WHERE        (SALARIO_MENSUAL = 0.0)

I have been asked to update the salary of such employees based on the average monthly salary of other employees within the same company and the same position.

The following query returns the average salary for employees for the same company and same position:
SQL:
SELECT A.CORR_EMPRESA, B.CORR_PUESTO, B.NOMBRE_PUESTO,  AVG(A.SALARIO_MENSUAL) AS AVERAGE_SALARIO_MENSUAL
FROM PLA_DOCUMENTO_DETA AS A
INNER JOIN PLA_PUESTO AS B ON A.CORR_EMPRESA = B.CORR_EMPRESA
INNER JOIN GEN_EMPLEADO AS C ON A.CORR_EMPRESA=C.CORR_EMPRESA AND A.CORR_EMPLEADO=C.CORR_EMPLEADO AND B.CORR_PUESTO=C.CORR_PUESTO
WHERE B.CORR_PUESTO IN (SELECT DISTINCT CORR_PUESTO
	FROM V_GEN_EMPLEADOS_CON_SALARIO_ZERO
	WHERE CORR_PUESTO IS NOT NULL) 
	AND A.CORR_EMPRESA IN (SELECT DISTINCT CORR_EMPRESA
	FROM V_GEN_EMPLEADOS_CON_SALARIO_ZERO
	WHERE CORR_EMPRESA IS NOT NULL)
GROUP BY B.CORR_PUESTO, B.NOMBRE_PUESTO, A.CORR_EMPRESA

My problem is that I don't know how to update the salary for the records returned by the view:
I'm thinking something like:
SQL:
UPDATE V_GEN_EMPLEADOS_CON_SALARIO_ZERO
SET SALARIO_MENSUAL = AVERAGE_SALARIO_MENSUAL

AVERAGE_SALARIO_MENSUAL is a field calculated on the second query.

Can some one please tell me what is that I'm doing wrong or point me in the right direction?

Thanks in advance.

Ed

 
You're on the right track. What you need is to join the two tables, something like:

Code:
UPDATE V_GEN_EMPLEADOS_CON_SALARIO_ZERO
SET SALARIO_MENSUAL = AVERAGE_SALARIO_MENSUAL 
FROM YourAveragingView
WHERE <the company and position match>

I can't make it more specific because my Spanish isn't good enough to tell which are the relevant fields.

Tamar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top