Employees
=============
ID
First
Last
Office (same as [Offices.Name])
Offices
=============
ID
Name
PostcardTracking
=============
ID
Agent (same as [Employees.ID])
Office (same as [Offices.Name])
mListQty
I need to display this information (in a web report - don't include details about formatting or anything):
Office | #Employees/Office | Mailing | Total Pieces
This is what needs to happen... I need to display each office name once in the Office field of the web report. Along with each office I need to display the number of employees in the office (each is in the Employees table only one time), the % of Employees that show up in the PostcardTracking table per office, and the total pieces of mail (mListQty) sent from users in that office.
I need to build this information into rows (JOIN) so I can output it to a dataset and write it to screen.
This seems like a trivial task, but my mind has come to a halt and im just totally stumped... please help!!!:: the % of Employees that show up in the PostcardTracking table per office
can you xplain a little more of this..
also, you might want to change the 'Id' in each table to the appropriate Id
employees_Id, office_id, postcard_id to avoid confusion about the ids. also i think Id is key word.|||Ok, so I'm not a SQL Guru but luckily you don't need one.
For the number of employees per office:
select O.ID as OfficeId,
O.Name as Office,
count(*) as [Employees/Office]
from Offices O
join Employees E
on O.Name = E.Office
group by O.ID, O.Name
For the percentage of employees that's tracking postcards (whatever that means):
select O.ID as OfficeId,
O.Name as Office,
(select count (distinct Agent)
from PostcardTracking T
where T.Office = O.Name)
/ count(*) * 100 as Mailing
from Offices O
join Employees E
on O.Name = E.Office
group by O.ID, O.Name
For the total number of pieces modify the first query. Then whack them all together any way you want - you can use one big select, or a temp table - whatever your brain can make work. My solution assums you don't want to see offices with no employees, which is probably a fair assumption.
Couple of things please:
1) Normalise your DB
2) Use OfficeID instead of just ID
3) Use singulars for table names, because if you think about it all tables will have more than one row and so end up all being plural. Use singulars and you save yourself from typing an 's' everytime you use a table.
4) Add some foreign key constraints to your tables. How do I know you don't use them? I'm a clairvoyant.
5) Normalise your DB.|||Thanks, Pierre.
I appreciate the tips. Most of what's there was there when I got where I am ;) None the less, I'm new at database design/engineering and have learned a bunch in the short amount of time that i've been doing this. Your input is much appreciated.
And thanks for the help with the queries :) they did the trick!!!
No comments:
Post a Comment