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 |
everbright
Starting Member
1 Post |
Posted - 2007-04-20 : 00:58:25
|
Hi all,I have 2 tables as follows:Table Aid name department1 Kev OSD2 Roger LPD3 Bim PWD4 Conny PWD5 Dan CPDTable Bid table_a_id logindate1 5 17/04/20072 1 16/04/20073 2 15/04/20074 2 16/04/2007I want to form a query that retrives a count of unique user logins by departments, for a range of login dates.So if I run the query for logins before 18/04/2007. I should get the following resultsA.department LoginsOSD 1LSD 1PWD 0CPD 1Note that the count for LSD should be 1, although Kev logged in twice in the time period.I tried some queries, but none was able to give me what I want. Most of these queries will drop the departments with no logins at all (i.e. no corresponding entries in table B).Here's what I tried:select A.department, count(distinct B.table_a_id) from A left outer join B on A.id=B.table_a_id where logindate < convert(datetime, '18/04/2007', 103) group by departmentAnyone has done something similar and succeeded in getting the results?Appreciate any help with this! Thanks! |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-20 : 03:57:19
|
use LEFT JOINselect A.department, Logins = isnull(B.id, 0)from tablea A left join talbeb Bon A.id = B.id KH |
 |
|
|
|
|