| Author |
Topic |
|
feeka
Starting Member
2 Posts |
Posted - 2007-04-20 : 03:56:55
|
| i have following schemasClient_Department(departmentid,name)Profile (profileid,username,departmentid)Attribute(attributeid,attributename,isactive,iscorrect)ProfileAttribute(attributeid,profileid)my required query is i want following records grouped by client_department.nameprofile.username(total number of users in each department)users which are more then 20 times in profileAttribute "As Completed"RegardsFeeka |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-04-20 : 04:19:55
|
| Please post some sample data and desired results |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-20 : 04:22:18
|
Post your DDL, sample data and expected result KH |
 |
|
|
feeka
Starting Member
2 Posts |
Posted - 2007-04-20 : 04:34:04
|
quote: Originally posted by Vinnie881 Please post some sample data and desired results
here is sample data client_department(1,HR 2,mgm 3,HRC 4,HEAD)Profile(1,naveed,1 2,jhon,1 3,alen,4 4,xe,3)Attribute(11,question1,0,0 12,answer1,1,0 13,answer1,1,1)profileAttribute(11,1 11,2 12,3 13,2)my desired results are 1.all the records should be grouped by client_department.name2.total number of users in profile3.number of users > 1 in profileattributeRegardsfeeka |
 |
|
|
pbguy
Constraint Violating Yak Guru
319 Posts |
Posted - 2007-04-20 : 06:25:50
|
| i am not clear about the your requirement 1 which says all records should be grouped by department name always give the desired output u need...check the following helps udeclare @Client_Department table (departmentid int ,name varchar(30))declare @Profile table (profileid int ,username varchar(30) ,departmentid int )declare @Attribute table (attributeid int,attributename varchar(30),isactive int ,iscorrect int)declare @ProfileAttribute table (attributeid int ,profileid int )insert @client_departmentselect 1,'HR' unionselect 2,'mgm' unionselect 3,'HRC' unionselect 4,'HEAD'insert @Profileselect 1,'naveed',1 union select 2,'jhon',1 unionselect 3,'alen',4 unionselect 4,'xe',3Insert @Attributeselect 11,'question1',0,0 unionselect 12,'answer1',1,0 unionselect 13,'answer1',1,1insert @profileAttributeselect 11,1 unionselect 11,2 unionselect 12,3 unionselect 13,2--1.all the records should be grouped by client_department.name--2.total number of users in profileselect cd.departmentid, cd.name, No_user = case when a.cc is null then 0 else a.cc end from @Client_Department cd left outer join (select departmentid, cc = count(departmentid) from @Profile group by departmentid) a on cd.departmentid = a.departmentid --3.number of users > 1 in profileattributeselect username from @Profile p join(select profileid, cc = count(profileid) from @profileAttribute group by profileid) a on p.profileid = a.profileid and a.cc > 1 |
 |
|
|
|
|
|