Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Sql query

Posted on 2010-01-07
9
Medium Priority
?
952 Views
Last Modified: 2012-05-08
Write a query that returns a list of the Regions and the number of employees in each region.  The employee only gets added to the count if they had total sales greater than $50,000 last month.  Sort the information by the number of employees per region who fit this criteria.

I have written the query already but i have entered the dates manually which needs to be automated and then whole would be fixed.
select   r.regiod_id, r.region_name,count(*) as emlopyee_count_with_sal_gt_50k   from 
employee e, region_id r
 
where e.employee_id in
(
(select employee_id  from sales where month(sale_date) >= 12/01/2009 and sale_date <= 12/31/2009= month 
and sum(sales_amount) > 50000  group by employee_id)
)
and e.region_id =r.region_id
group by r.region_id

Open in new window

0
Comment
Question by:onebite2
  • 2
  • 2
  • 2
  • +2
8 Comments
 

Author Comment

by:onebite2
ID: 26200401
I need this in Sql server 2005.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 26200457
you know the function "getdate()" to get the current date?
along with datepart, and dateadd functions, you can dynamically come to the first day of the month, and from there remove 1 more month ...
0
 
LVL 26

Expert Comment

by:tigin44
ID: 26200509
try
select   r.regiod_id, r.region_name,count(*) as emlopyee_count_with_sal_gt_50k   
from employee e 
		INNER JOIN  region_id r ON e.region_id =r.region_id
		INNER JOIN ( select employee_id  
					 from sales 
					 where slae_date between '12/01/2009' and '12/31/2009'
					 group by employee_id
					 having sum(sales_amount) > 50000 ) s ON e.employee_id = s.employee_id
group by r.region_id, r.region_name

Open in new window

0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 26

Expert Comment

by:tigin44
ID: 26200533
also date values paramatrized
declare @startDate	datetime;
declare @endDate	datetime;

select   r.regiod_id, r.region_name,count(*) as emlopyee_count_with_sal_gt_50k   
from employee e 
		INNER JOIN  region_id r ON e.region_id =r.region_id
		INNER JOIN ( select employee_id  
					 from sales 
					 where slae_date between @startDate and @endDate
					 group by employee_id
					 having sum(sales_amount) > 50000 ) s ON e.employee_id = s.employee_id
group by r.region_id, r.region_name

Open in new window

0
 
LVL 12

Expert Comment

by:HugoHiasl
ID: 26200559
Not tested but should point to the right direction...

select   r.regiod_id, r.region_name,count(*) as emlopyee_count_with_sal_gt_50k
from
employee where employee_id in
(  
select i.employee_id
from employee i
where month(i.sale_date) = 12
group by i.employee_id
having sum(i.sales_amount) > 50000
)
group by region_id, region_name
order by count(*)

 

0
 
LVL 61

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 26201133
here it is

where

[first_day_of_last_month] = convert(datetime, CONVERT(VARCHAR(10),DATEADD(MONTH, -1, DATEADD(DAY, - DAY(getdate()) + 1, getdate())),101), 101)
[last_day_of_last_month] = convert(datetime, CONVERT(VARCHAR(10),DATEADD(DAY, -DATEPART(DAY, getdate()), getdate()),101), 101)



select r.regiod_id, r.region_name, count(*) as emlopyee_count_with_sal_gt_50k   
  from employee e, region_id r 
 where e.employee_id in 
( 
select employee_id
  from sales 
 where sale_date  between [first_day_of_last_month] and [last_day_of_last_month]
 group by employee_id 
having sum(sales_amount)>50000
) 
   and e.region_id = r.region_id 
 group by r.regiod_id, r.region_name

Open in new window

0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26201157
you can add "order by 3 desc" or "order by emlopyee_count_with_sal_gt_50k desc" to the query above to get it sorted...
0
 

Author Closing Comment

by:onebite2
ID: 31674019
Perfect answer to my question
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Among the most obnoxious of Exchange errors is error 1216 – Attached Database Mismatch error of the Jet Database Engine. When faced with this error, users may have to suffer from mailbox inaccessibility and in worst situations, permanent data loss.
Exchange database can often fail to mount thereby halting the work of all users connected to it. Finding out why database isn’t mounting is crucial and getting the server back online. Stellar Phoenix Mailbox Exchange Recovery is a champion product t…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question