| Author |
Topic |
|
claud53
Starting Member
4 Posts |
Posted - 2011-02-14 : 05:59:06
|
| Hello,I need some help to create a SQL query. I have three tables:Employer(ID_E, Name_Surname, Function, ID_D)Department(ID_D, Description)Vacation(ID_E, Start_Date, Period)I want the query to find:The ID of the employers and Name_surname for the employers that have done longer vacations that the average of the days of the employers of the same Function in the same Department.here are some sample data:Table Employer:ID_E Name_Surname Function ID_D1 A.Done IT 22 B.Funny IT 23 M.Douglas IT 24 A.Pata Accountant 15 G.Nesa Accountant 1Table Department:ID_D Description 1 Economic Department2 Technical Department Table Vacation:ID_E Start_Date Period1 1/1/2011 152 2/2/2011 53 15/1/2011 104 15/02/2011 95 17/02/2011 7From the data in simple words I want the query to displayID_E Name_Surname1 A.Done4 A.PataCould you please help me! |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-02-14 : 08:24:45
|
| Hi Claud53,Most people here won't do your homework for you, but if you try to do it yourself, poeple here are more than happy to help. Post the query you have so far (even if it doesn't work) and we'll help you from there.JimEveryday I learn something that somebody else already knew |
 |
|
|
claud53
Starting Member
4 Posts |
Posted - 2011-02-14 : 08:45:02
|
| The query I have until now is:Select E.ID_E, E.Name_Surnamefrom Employer E, Department D, Vacation VWhere E.ID_E=V.ID_Eand E.ID_D=D.ID_DHaving V.Period >(select E1.Function, D1.ID_D, AVG(V1.Period)as averFrom Employer E1, Vacation V1, Department D1Where E1.ID_E=V1.ID_E and D1.ID_D=E1.ID_DGroup by E1.Function, D1.ID_D)I know that there is a problem with the having part but I don't know how to proceed.Thanks for your help |
 |
|
|
claud53
Starting Member
4 Posts |
Posted - 2011-02-14 : 08:47:29
|
| let say that I don't know even if it is possible to build such a query because I am new to SQL |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2011-02-14 : 09:28:08
|
When asking a question you should provide test data in an easily consumable format.Dates should be ISO format.CREATE TABLE #Employer( ID_E int NOT NULL ,Name_Surname varchar(20) NOT NULL ,[Function] varchar(20) NOT NULL ,ID_D int NOT NULL)INSERT INTO #EmployerSELECT 1, 'A.Done', 'IT', 2UNION ALL SELECT 2, 'B.Funny', 'IT', 2UNION ALL SELECT 3, 'M.Douglas', 'IT', 2UNION ALL SELECT 4, 'A.Pata', 'Accountant', 1UNION ALL SELECT 5, 'G.Nesa', 'Accountant', 1CREATE TABLE #Vacation( ID_E int NOT NULL ,[Start_Date] datetime NOT NULL ,Period int NOT NULL)INSERT INTO #VacationSELECT 1, '20110101', 15UNION ALL SELECT 2, '20110202', 5UNION ALL SELECT 3, '20110115', 10UNION ALL SELECT 4, '20110215', 9UNION ALL SELECT 5, '20110217', 7 Assuming you are using SQL2005 or above, the following query, which uses the windowed functions, works with the above test data:;WITH VacationAggregatesAS( SELECT E.ID_E, E.Name_Surname ,SUM(V.Period) OVER (PARTITION BY E.ID_E) AS Duration ,AVG(V.Period) OVER (PARTITION BY E.ID_D) AS DeptAvg FROM #Employer E JOIN #Vacation V ON E.ID_E = V.ID_E)SELECT ID_E, Name_SurnameFROM VacationAggregatesWHERE Duration > DeptAvgORDER BY ID_E |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-02-14 : 09:28:24
|
Here is a way, it may not be the best, (but hey, it's free!)JimDECLARE @Employer TABLE (ID_E tinyint, Name_Surname varchar(50),fcn varchar(20),ID_D tinyint)INSERT INTO @EmployerSELECT 1,'A.Done','IT' ,2 UNION ALL---SELECT 2,'B.Funny','IT' ,2 UNION ALLSELECT 3,'M.Douglas','IT' ,2 UNION ALLSELECT 4,'A.Pata','Accountant' ,1 UNION ALL----SELECT 5,'G.Nesa','Accountant' ,1DECLARE @Department TABLE (ID_D tinyint,Descr varchar(50))INSERT INTO @DepartmentSELECT 1,'Economic Department' UNION ALLSELECT 2,'Technical Department 'DECLARE @Vacation TABLE(ID_E tinyint, startDate datetime,Period tinyint)INSERT INTO @VacationSELECT 1,'20110101', 15 UNION ALLSELECT 2,'20110202', 5 UNION ALLSELECT 3,'20110115', 10 UNION ALLSELECT 4,'20110215', 9 UNION ALLSELECT 5,'20110217', 7 SELECT e.ID_E,e.name_surnameFROM @employer eINNER JOIN ( select e.id_e ,[rank] = rank() over(partition by e.id_d order by v.period desc) from @employer e inner join @vacation v on e.id_e = v.id_e ) tON e.id_e = t.id_eWHERE t.rank = 1 Everyday I learn something that somebody else already knew |
 |
|
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2011-02-14 : 10:46:16
|
|
 |
|
|
claud53
Starting Member
4 Posts |
Posted - 2011-02-16 : 04:07:20
|
| Thank you very much for your help |
 |
|
|
|
|
|