Home » SQL & PL/SQL » SQL & PL/SQL » sql (sql)
sql [message #627554] Wed, 12 November 2014 13:21 Go to next message
sanodani
Messages: 98
Registered: October 2014
Member
Hallo Smile

I am trying to make a query, the all departments, that has minimum one workers, that is more than 20 year working in the same department.
and I wanna display: name of dept, city, country, region, the number of workers that, they are more than 20 years working in the dept, and total number of workers.

but my query did not show me result ?:/ can anyone help me please ?
thnakx in advance
Select d.department_id, e.employee_id, d.department_name
       , c.country_name, r.region_name, count(*)
       , sum(salary)
from employees e, locations l, countries c, regions r, departments d
where e.department_id = d.department_id 
and   d.location_id = l.location_id
and   l.country_id = c.country_id
and   c.region_id = r.region_id
and   trunc(sysdate) -hire_date > 20*365
group by  d.department_id, e.employee_id, d.department_name, l.city, c.country_name, r.region_name
order by employee_id;
Re: sql [message #627555 is a reply to message #627554] Wed, 12 November 2014 13:28 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
select count(*) from employees where trunc(sysdate) -hire_date > 20*365;

post results from SQL above
Re: sql [message #627556 is a reply to message #627555] Wed, 12 November 2014 13:32 Go to previous messageGo to next message
sanodani
Messages: 98
Registered: October 2014
Member
Sorry, I did not understand what you mean :/
can you please make me clear regarding my problem?
Re: sql [message #627558 is a reply to message #627556] Wed, 12 November 2014 13:35 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
post results from SQL below

select count(*) from employees where trunc(sysdate) -hire_date > 20*365;
Re: sql [message #627559 is a reply to message #627558] Wed, 12 November 2014 13:40 Go to previous messageGo to next message
sanodani
Messages: 98
Registered: October 2014
Member
query returns = 0
Re: sql [message #627560 is a reply to message #627559] Wed, 12 November 2014 13:43 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
post results from SQL below

select min(hire_date) from employees;
Re: sql [message #627561 is a reply to message #627559] Wed, 12 November 2014 13:45 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So you have your answer: no employee was hired more than 20 years ago, so your query can't result anything.

Note that 20 years ago is not sysdate-20*365 but add_months(sysdate,-20*12).

Re: sql [message #627562 is a reply to message #627560] Wed, 12 November 2014 13:52 Go to previous messageGo to next message
sanodani
Messages: 98
Registered: October 2014
Member
13/01/2001 Smile

well, may be i got now what you mean Smile thankyou very much
there is no one working since 20 years.
but, do you think, i am correct to get my result as asked in the question in first post?

Select d.department_id, e.employee_id, d.department_name
       , c.country_name, r.region_name, count(*)
       , sum(salary)
from employees e, locations l, countries c, regions r, departments d
where e.department_id = d.department_id 
and   d.location_id = l.location_id
and   l.country_id = c.country_id
and   c.region_id = r.region_id
and   trunc(sysdate) -hire_date > 10*365   
group by  d.department_id, e.employee_id, d.department_name, l.city, c.country_name, r.region_name
order by department_id;
Re: sql [message #627564 is a reply to message #627562] Wed, 12 November 2014 14:00 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Michel Cadot wrote on Wed, 12 November 2014 20:45

So you have your answer: no employee was hired more than 20 years ago, so your query can't result anything.

Note that 20 years ago is not sysdate-20*365 but add_months(sysdate,-20*12).


Re: sql [message #627565 is a reply to message #627564] Wed, 12 November 2014 14:05 Go to previous messageGo to next message
sanodani
Messages: 98
Registered: October 2014
Member
sorry i have seen your reply late..

so here is my query again, but still i have error Sad

Select d.department_id, e.employee_id, d.department_name
       , c.country_name, r.region_name, count(*)
       , sum(salary)
from employees e, locations l, countries c, regions r, departments d
where e.department_id = d.department_id 
and   d.location_id = l.location_id
and   l.country_id = c.country_id
and   c.region_id = r.region_id
and   add_months(trunc(sysdate,-20)*12) 
group by  d.department_id, e.employee_id, d.department_name, l.city, c.country_name, r.region_name
order by department_id;
Re: sql [message #627567 is a reply to message #627565] Wed, 12 November 2014 14:13 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
and add_months(trunc(sysdate,-20)*12)


Parentheses are misplaced.

Re: sql [message #627570 is a reply to message #627565] Wed, 12 November 2014 14:16 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>and add_months(trunc(sysdate,-20)*12)

not as above but as below

and hire_date < add_months(trunc(sysdate),-20*12)
Re: sql [message #627571 is a reply to message #627570] Wed, 12 November 2014 14:26 Go to previous message
sanodani
Messages: 98
Registered: October 2014
Member
Thank you very much Smile
it works now Smile
Previous Topic: Need Backup of Objects
Next Topic: Achieving Report Format in PL/SQL or SQL
Goto Forum:
  


Current Time: Thu Apr 25 15:03:50 CDT 2024