Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 MS Access
 UNION Query

Author  Topic 

andreei87
Starting Member

2 Posts

Posted - 2011-05-06 : 17:58:44
Hi guys ... i was wondering if any of you can help me on a little problem i have ... i need to build a SQL UNION query using SELECT, UNION, WHERE, GROUP BY (no INNER JOIN like the design view in MS Access does). I've been trying to do it for the last few weeks but nothing!

Here are my tables:
Employee: id_employee, salary
penalty: id_employee, penalty

I need to take the 2 tables and create a query to calculate the final salary final_salary=salary-penaltyand print the results in a new table with the structure: Table:id_employee, salary, penalty, final_salary.

the syntax for query in design view is :

SELECT tabel.id_angajat, Angajati.Salariu, Retineri.Data_retinere, Retineri.Suma_retinuta, Angajati.[Salariu]-[Suma_retinuta] AS Salariul_final
FROM ((SELECT id_angajat, salariu from angajati UNION select id_angajat, suma_retinuta from retineri) AS tabel INNER JOIN Angajati ON tabel.id_angajat=Angajati.id_angajat) INNER JOIN Retineri ON Angajati.id_angajat=Retineri.id_retinere
GROUP BY tabel.id_angajat, Angajati.Salariu, Retineri.Data_retinere, Retineri.Suma_retinuta;

if any of you can help get rid of the INNER JOIN .. ON and use only SELECT FROM WHERE GROUP BY i wold be very thankful!

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-05-06 : 18:13:38
Why? Are you trying to get the correct results, or is this a homework question?

Can the same employee appear in either table multiple times?
Go to Top of Page

andreei87
Starting Member

2 Posts

Posted - 2011-05-06 : 18:36:24
quote:
Originally posted by russell

Why? Are you trying to get the correct results, or is this a homework question?

Can the same employee appear in either table multiple times?



yes it is a homework! the same elmployee can apear multiple times in the penalty table but not in the employee table!
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2011-05-10 : 04:44:23
1. we don't do homework, because you won't learn...READ the FAQ's.
2. you have some mitigation in that you have at least TRIED.
3. Read the FAQ's - Post some sample input data and MATCHING expected results.

4. You might get lucky.
Go to Top of Page
   

- Advertisement -