| Author |
Topic |
|
Carl_Hardy
Starting Member
5 Posts |
Posted - 2010-09-24 : 13:05:01
|
| Hi, reasonably new to SQL programming and still getting tripped up by what some will see as basic SQL techniques.....here goes.I have a table with the Columns: Job_Role, User_IDThere are many people with the same job and a user can have more than one job.What I need is a summary of the Jobs by user, but here is the bit that confuses me. I also need to have a total of the number of people with the same job.Job_Role User_ID Users_With_JobJob1.......Carl......16Job2.......Carl......83Job12.....Carl......112Job86.....Carl......2I currently have two views set up to provide the Jobs by User and another to provide a total number of users with the Job.I then have a third viewed on the other two to combine the figures.Although it works, it seems a bit clunky. Any suggestions how to do this with one View (or sp)Carl |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-09-24 : 13:09:26
|
| Well it's confusing because ou are combing 2 different things in 1 result setThing 1 Job's a User HasThing 2 # of Users with a JobThe commonailty is the JobSo do use a favor and write 2 separate sql's...1 for thing, the other for thing 2...And post sample data for us in the form ofINSERT INTO myTable(Job_Role, User_ID)SELECT 'Job data', 'User Id' UNION ALLSELECT 'Job data', 'User Id' UNION ALLectBrett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspx |
 |
|
|
Carl_Hardy
Starting Member
5 Posts |
Posted - 2010-09-24 : 16:47:29
|
| No worries, I will write a sp using a temporary table. I thought someone might have an easier answer using subquery/nested select.Topic can be closed.Carl |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-09-25 : 00:05:06
|
if sql 2005 and above, you can do like thisCREATE VIEW YourViewASBEGINSELECT Job_Role,User_ID,COUNT(Job_ID) OVER (PARTITION BY User_ID) AS JobsByUser,COUNT(User_ID) OVER (PARTITION BY Job_ID) AS UsersByJobsFROM TableEND ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Carl_Hardy
Starting Member
5 Posts |
Posted - 2010-09-25 : 03:10:28
|
| Thanks visakh16,'OVER' and 'PARTITIION BY' are new ones to me, I will give it a go.Carl |
 |
|
|
Carl_Hardy
Starting Member
5 Posts |
Posted - 2010-09-25 : 10:16:01
|
| Even though I am running SQL server 2005 I am getting the message that 'over' is not supported.To ensure full understanding of what I am after:I want to return a list of all the Jobs a specific user has and for each job in the list, show the number of other users with the same job.Carl |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-09-25 : 12:05:52
|
quote: Originally posted by Carl_Hardy Even though I am running SQL server 2005 I am getting the message that 'over' is not supported.To ensure full understanding of what I am after:I want to return a list of all the Jobs a specific user has and for each job in the list, show the number of other users with the same job.Carl
Then your compatibility level will be less than 90. check it firstEXEC sp_dbcmptlevel 'your db name' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Carl_Hardy
Starting Member
5 Posts |
Posted - 2010-09-26 : 07:44:27
|
| did as you suggested and the message said curretn compatibility is at 90. Very strange.Learned something else new checking the compatibility.Carl |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-09-26 : 07:52:32
|
| which editor are you using?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|