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 |
|
mohdmartin
Starting Member
22 Posts |
Posted - 2007-06-26 : 08:00:43
|
| I have following tables structureEmployee---------EMP_ID varchar PKNAME varcharDEPARTMENT_CODE varcharPOSITION_CODE intPosition--------POSITION_CODE int PKPOSITION_NAME varcharDepartment----------DEPARTMENT_CODE varchar PKDEPARTMENT_NAME varcharTraining_Module---------------TRA_ID varchar PKTRA_NAME varcharTRA_GROUP varchar (three group, A,B and C)View_Training_Module--------------------MV_ID int PKEMP_ID varcharTRA_ID varcharVIEW_DATE datetimeTraining_Module table data like thisTRA_ID..TRA_NAME..TRA_GROUP--------------------------------------v01 SafetyVideo1 G1v02 SafetyVideo2 G1v03 SafetyVideo3 G1V04 SafetyVideo4 G2V05 SafetyVideo5 G2v06 SafetyVideo6 G2v07 SafetyVideo7 G3v08 SafetyVideo8 G3v09 SafetyVideo9 G3View_Training_Module table data like thisEMP_ID.........TRA_ID....VIEW_DATE-------------------------------------------p0006367 V01 6/2/2007p0006367 V02 6/2/2007p0006367 V03 6/2/2007p0003892 V01 6/12/2007p0003892 V02 6/12/2007p0003892 V03 6/12/2007p0003890 V01 6/15/2007p0003890 V02 6/15/2007p0003890 V03 6/15/2007p0001232 V04 6/16/2007p0001232 V05 6/16/2007p0001232 V06 6/16/2007p0001230 V07 6/17/2007p0001230 V08 6/18/2007p0001230 V09 6/18/2007We have 44 Safety training videos (15 minutes)How can calculate the percentage of each employee in query ?if emploee view 22 video it means that this employee 50% view the videos.We have to calcuate 1. total number of video view by each employee, sum2. each employee perentage of viewing.ie. percentage %3. Group wise percentage, ? we have three group A,B,and C, calcuate the each group percentage in query ?regardsMartin |
|
|
influent
Constraint Violating Yak Guru
367 Posts |
Posted - 2007-06-26 : 16:18:28
|
| Assuming no employee views the same video twice, here are the first two:DECLARE @totalVideos intSET @totalVideos = (SELECT COUNT(*) FROM TrainingModule)SELECT t2.Name, COUNT(t1.emp_id) as ViewCount, COUNT(t1.emp_id)/@totalVideos as ViewPercentage FROM View_Training_Module t1 JOIN Employee t2 ON t1.emp_id = t2.emp_id GROUP BY t1.emp_id |
 |
|
|
mohdmartin
Starting Member
22 Posts |
Posted - 2007-06-27 : 02:33:58
|
| Thanks for your response.it is working,I use query like this.DECLARE @totalVideos int SET @totalVideos = (SELECT COUNT(*) FROM Training_Module)SELECT t2.Name,t2.department_code, COUNT(t1.emp_id) as ViewCount,COUNT(t1.emp_id)*100/44 as ViewPercentageFROM Module_View t1 JOIN Employee t2 ON t1.emp_id = t2.emp_idGROUP BY t1.emp_id,t2.Name,t2.department_codewhen I use @totalVideos in the query it show wrong percentage (high per)hence I use like this => COUNT(t1.emp_id)*100/44 It should be work with @totalvideos because number of total video can be increase ?How can display the department_name, Position_name in above query ?regardsMartin |
 |
|
|
influent
Constraint Violating Yak Guru
367 Posts |
Posted - 2007-06-27 : 11:47:01
|
| Glad you got it working, I just noticed it wouldn't have worked the way I did it because I didn't include Name in the grouping. You should be able to replace the 44 with @totalVideos now that you've added the "*100", I'm not sure why it would be too high unless people are watching the same video more than once. To display the department name and position name:DECLARE @totalVideos int SET @totalVideos = (SELECT COUNT(*) FROM Training_Module)SELECT t2.Name,t3.Department_Name, t4.Position_Name, COUNT(t1.emp_id) as ViewCount,COUNT(t1.emp_id)*100/@totalVideo as ViewPercentageFROM Module_View t1 JOIN Employee t2 ON t1.emp_id = t2.emp_idJOIN Department t3 ON t2.Department_Code = t3.Department_CodeJOIN Position t4 ON t2.Position_Code = t4.Position_CodeGROUP BY t1.emp_id,t2.Name,t3.Department_Name, t4.Position_Name |
 |
|
|
mohdmartin
Starting Member
22 Posts |
Posted - 2007-06-28 : 08:29:48
|
| Thanks you very much. it is working.but when I use => *100/@totalVideo as ViewPercentage it show wrong per./44 is OK.We have 300 employees, and training_module have three group, 1,2 and 3. different video belong to differnt group.How can use the same query in further group by ?it retrived records group wise ie... GROUP 1..viewcount..viewpercentage-----------------------------------Peter 10 22Jone 16 36Marks 8 18...GROUP 2.viewcount..viewpercentage ---------------------------------Khan 15 25Clerk 20 40wagner 8 18...GROUP 3..viewcount..viewpercentage----------------------------------Martin 20 24Syed 16 36Qazi 8 18....or for Module_viwe group by separate query will use ?regardsMartin |
 |
|
|
|
|
|
|
|