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.
| Author |
Topic |
|
boybles
Starting Member
23 Posts |
Posted - 2008-07-06 : 21:25:28
|
I'm trying to figure out how to join these tables together to get the following result. Please please help. Thanks!!!Boybles TABLES:Employee----------EmployeeID (smallint)EmployeeName (varchar)DepartmentID (smallint)Mail----------MailID (smallint)MailName (varchar)MailFrom (smallint)MailTo (smallint)Department------------------DepartmentID (smallint)DepartmentName (varchar)SAMPLE DATA:Employee-------1|Joe|12|Jane|23|Fred|34|Frank|15|James|3Mail-------1|Test Mail|1|22|Today....|4|33|About Joe|5|34|Joe Rocks|2|15|Tomorrow |2|36|DailyNews|5|1Department-----------1|Sales2|Marketing3|AccountingDESIRED RESULT:EmployeeName|DepartmentName|TotalMailReceived---------------------------------------------Joe|Marketing|2Jane|Marketing|1Fred|Accounting|3Frank|Sales|0James|Accounting|0(TotalMailReceived is an alias of a count of MailTo for every employee) |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-06 : 21:29:50
|
[code]select e.EmployeeName, d.Departmentname, TotalMailReceived = count(*)from mail m inner join Employee e on m.MailTo = e.EmployeeID inner join Department d on e.DepartmentID = d.DepartmentID group by e.EmployeeName, d.Departmentname[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
EugeneLim11
Posting Yak Master
167 Posts |
Posted - 2008-07-07 : 06:05:44
|
| Select EmployeeName, DepartmentName, Count(MailID) as TotalMailReceivedfrom Employee E, Department D, Mail M where E.EmployeeId = M.MailToand D.DepartmentID = E.DepartmentIDgroup by EmployeeName, DepartmentName |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2008-07-07 : 06:09:18
|
quote: Originally posted by EugeneLim11 Select EmployeeName, DepartmentName, Count(MailID) as TotalMailReceivedfrom Employee E, Department D, Mail M where E.EmployeeId = M.MailToand D.DepartmentID = E.DepartmentIDgroup by EmployeeName, DepartmentName
No, use Khtan's example. The above example does not use T-SQL, it uses ansi-sql. |
 |
|
|
boybles
Starting Member
23 Posts |
Posted - 2008-07-08 : 21:12:59
|
Khtan,You rock! Once again, thank you for your help.Boybles |
 |
|
|
boybles
Starting Member
23 Posts |
Posted - 2009-05-06 : 04:02:09
|
| Hey guys,I'm having trouble when adding datetime conditions to this scenario.If I expand my Mail table with a SentDate column:Mail----------MailID (smallint)MailName (varchar)MailFrom (smallint)MailTo (smallint)SentDate (datetime)And fill in the dates as such:Mail-------1|Test Mail|1|2|1/1/20082|Today....|4|3|1/2/20083|About Joe|5|3|1/3/20084|Joe Rocks|2|1|2/1/20085|Tomorrow |2|3|2/2/20086|DailyNews|5|1|2/3/2008I'd like to expand the SQL statement to get the following result:EmployeeName|DepartmentName|TotalMailReceived---------------------------------------------Joe|Marketing|2Jane|Marketing|0Fred|Accounting|1Frank|Sales|0James|Accounting|0I've tried the following:select e.EmployeeName, d.Departmentname, TotalMailReceived = count(*)from mail m inner join Employee e on m.MailTo = e.EmployeeID inner join Department d on e.DepartmentID = d.DepartmentID where m.SentDate >= '2008-02-01' and m.SentDate <= '2008-03-01' group by e.EmployeeName, d.DepartmentBut I still get the old result. What do I need to do to change the sql statement so that it will reflect the count accurately?Thanks,Boybles |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-06 : 06:13:50
|
[code]DECLARE @Employee TABLE( EmployeeID smallint, EmployeeName varchar(10), DepartmentID smallint)DECLARE @Mail TABLE( MailID smallint, MailName varchar(10), MailFrom smallint, MailTo smallint, SentDate datetime)DECLARE @Department TABLE( DepartmentID smallint, DepartmentName varchar(10))INSERT INTO @EmployeeSELECT 1, 'Joe', 1 UNION ALLSELECT 2, 'Jane', 2 UNION ALLSELECT 3, 'Fred', 3 UNION ALLSELECT 4, 'Frank', 1 UNION ALLSELECT 5, 'James', 3INSERT INTO @MailSELECT 1, 'Test Mail', 1, 2, '2008-01-01' UNION ALLSELECT 2, 'Today....', 4, 3, '2008-01-02' UNION ALLSELECT 3, 'About Joe', 5, 3, '2008-01-03' UNION ALLSELECT 4, 'Joe Rocks', 2, 1, '2008-01-02' UNION ALLSELECT 5, 'Tomorrow' , 2, 3, '2008-02-02' UNION ALLSELECT 6, 'DailyNews', 5, 1, '2008-02-03'INSERT INTO @DepartmentSELECT 1, 'Sales' UNION ALLSELECT 2, 'Marketing' UNION ALLSELECT 3, 'Accounting'SELECT e.EmployeeName, d.DepartmentName, TotalMailReceived = COUNT(m.MailTo)FROM @Employee e INNER JOIN @Department d ON e.DepartmentID = d.DepartmentID left JOIN @Mail m ON e.EmployeeID = m.MailTo AND m.SentDate >= '2008-02-01' AND m.SentDate <= '2008-03-01'GROUP BY e.EmployeeName, d.DepartmentName/*EmployeeName DepartmentName TotalMailReceived ------------ -------------- ----------------- Fred Accounting 1James Accounting 0Jane Marketing 0Frank Sales 0Joe Sales 1(5 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
boybles
Starting Member
23 Posts |
Posted - 2009-05-06 : 10:32:19
|
| Khatan, That works wonderfully. What if I wanted to select only records where the TotalMailReceived count was greater than 0?Thanks again,Boybles |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-06 : 10:33:09
|
change LEFT JOIN to INNER JOIN KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
boybles
Starting Member
23 Posts |
Posted - 2009-05-06 : 20:21:36
|
| That was it. U=Da BOMB! Thanks again for your help and insight!Boybles |
 |
|
|
|
|
|
|
|