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 |
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-10-21 : 09:43:30
|
Hi Im trying to show records that only contain 1 assignment per team selection, Im trying to use row_number and thought the RankNum = 1 would lookup records and only show the records where a team has one assignment, if a team has more than one assignment dont show.CAn anyone assist please?Here is what I have so far...Use TESTGoWith RankResult as(select i.[IncidentNumber], i.[CreatedDateTime], i.[ResolutionDateAndTime], i.[Priority], i.[Status], i.[ClientName], i.[ClientSite], t.[OwnerTeam], t.[Owner], row_number() over( partition by i.RecID order by t.CreatedDateTime desc, t.Owner ) seq, RankNum = Row_Number() Over(Partition By t.ParentLink_RecID Order By t.CreatedDateTime Desc) From Incident as i Left Join Task as t On i.RecID = t.ParentLink_RecID Where t.OwnerTeam = 'Info Services' And i.CreatedDateTime >= '2012-10-01' And i.CreatedDateTime <= '2013-10-01' ) select Distinct [IncidentNumber], [CreatedDateTime], [ResolutionDateAndTime], [Priority], [Status], [ClientName], [ClientSite], [OwnerTeam], [Owner] From RankResult Where seq = 1 And RankNum = 1 Order By IncidentNumber Asc GOSZ1Please help me to enable me to help others! |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-21 : 09:47:25
|
nope. if you're looking to return teams having only one assignment, then what you need is thisUse TESTGoWith RankResult as(select i.[IncidentNumber],i.[CreatedDateTime],i.[ResolutionDateAndTime],i.[Priority],i.[Status],i.[ClientName],i.[ClientSite],t.[OwnerTeam],t.[Owner],row_number() over( partition by i.RecID order by t.CreatedDateTime desc, t.Owner ) seq,Cnt = COUNT(1) Over(Partition By i.RecID)From Incident as iLeft Join Task as tOn i.RecID = t.ParentLink_RecIDWhere t.OwnerTeam = 'Info Services'And i.CreatedDateTime >= '2012-10-01'And i.CreatedDateTime <= '2013-10-01')select Distinct [IncidentNumber],[CreatedDateTime],[ResolutionDateAndTime],[Priority],[Status],[ClientName],[ClientSite],[OwnerTeam],[Owner]From RankResultWhere Cnt = 1Order By IncidentNumber AscGO Assuming i.RecID is team information stored------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-10-21 : 10:04:45
|
Thats seems more like it :)The assignments come from the Task table which uses ParentLink_RecID to join to the Incident table RecID, so each assignment will use the ParentLink_RecID for each Task Assignment where the RecID is the actual main Incident the assignments are attached to, so there will always be one IncidentNumber although it will repeat as assignments are added to it hence the seq = 1 although I dont think I need this now by the looks of things with your bit of code? So I think I can change the Cnt code to:Cnt = COUNT(1) Over(Partition By t.ParentLink_RecID)I have tried this and it comes back with the same amount of records, also on checking records I only see one instance for the team I'm filtering on which is what I want, there are other teams but as long as it only shows one per the team I select I think its good.Does that make sense changing the Cnt = code?Many thanks mate!SZ1Please help me to enable me to help others! |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-10-21 : 10:15:40
|
mmm I'm seeing the odd record with 2 assignments for the team I select although its looking better as the number of records have reduced quite a lot too, is there a way to add a count to see how many assignments are on each IncidentNumber?ThanksSZ1Please help me to enable me to help others! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-21 : 14:35:40
|
quote: Originally posted by sz1 mmm I'm seeing the odd record with 2 assignments for the team I select although its looking better as the number of records have reduced quite a lot too, is there a way to add a count to see how many assignments are on each IncidentNumber?ThanksSZ1Please help me to enable me to help others!
Thats not correct. So far as RecID has only one associated entry in the then it will return it. Can you post RecID and IncidentNumber value of one such case where you feel they're duplicates------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-10-22 : 05:04:14
|
The RecID is the main key for Incident, there will only be one row with the IncidentNumber unless there are more assignments in Task, each time a new assignment in Task is created another IncidentNumber row will appear to count for the extra assignments from Task. So even though the code above removes the duplicates IncidentNumber rows when I open up an IncidentNumber I can see the 2 instances of the same team. For example record 332837 has 2 assignments for the same team. I can only tell by opening up an Incident to check so I cant really add an image of the live db. If we can add a count for number of assignments attached to the Incident Number that might help?There can be any number of assignments on one RecID (IncidentNumber) from the Task table, so its really a matter of saying show me all the records that have the team selected but with only 1 assignment, if that team has more than 1 as in case 332837 dont return.ThanksSZ1Please help me to enable me to help others! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-22 : 07:52:26
|
quote: Originally posted by sz1 The RecID is the main key for Incident, there will only be one row with the IncidentNumber unless there are more assignments in Task, each time a new assignment in Task is created another IncidentNumber row will appear to count for the extra assignments from Task. So even though the code above removes the duplicates IncidentNumber rows when I open up an IncidentNumber I can see the 2 instances of the same team. For example record 332837 has 2 assignments for the same team. I can only tell by opening up an Incident to check so I cant really add an image of the live db. If we can add a count for number of assignments attached to the Incident Number that might help?There can be any number of assignments on one RecID (IncidentNumber) from the Task table, so its really a matter of saying show me all the records that have the team selected but with only 1 assignment, if that team has more than 1 as in case 332837 dont return.ThanksSZ1Please help me to enable me to help others!
can you post some data which are duplicates according to you and explain how you want output to appear?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2013-10-22 : 08:10:29
|
Sorted now mate, the extra records were from a different source on the Incident and so I dont need to dig deeper.Many thanks for helping me out :)As always much appreciated!SZ1Please help me to enable me to help others! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-22 : 08:20:26
|
Oh ok..Glad that you get it sorted------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|