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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Distinct from mulitple team entries on 2nd table j

Author  Topic 

sz1
Aged Yak Warrior

555 Posts

Posted - 2012-03-21 : 09:03:22
Hey

I have another issue with this query. I'm getting too many counts but this is because the t.[OwnerTeam] = 'Sales' can appear multiple times assignment wise. so, think I need another distinct or count only once when Sales record found in OwnerTeam NOT Multiple...
can someone advise?
Thanks



ALTER View [dbo].[STCalls]
AS

SELECT COUNT(Distinct i.[IncidentNumber]) As [Live ITSM Calls]
FROM
dbo.Incident AS i
Join dbo.Task AS t ON i.[Recid] = t.[ParentLink_RecID]
Where
i.[Status] IN ('Active','Waiting')
And t.[OwnerTeam] = 'Sales'



GO

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-21 : 10:22:22
we cant suggest anything unless we see some data and understand what you're talking about. You already have count(distinct ...) and if its not working show some data and explain what count you're expecting out of them

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sz1
Aged Yak Warrior

555 Posts

Posted - 2012-03-21 : 11:20:09
thats fair enough, I've just run this and I know whats happening now.I'm getting more records back because the sales team has an Incident somewhere in the record, so If I could only say give me the last record (the one which is live) that = sales then that would do it. Tried putting a MAX in front as below but still getting same result, thought that would have done it as Max should be the last record = sales?

Does this help?

ALTER View [dbo].[SDCalls]
AS


SELECT i.[IncidentNumber],Max(t.[OwnerTeam]) As [OwnerTeam],i.[Status],
COUNT(Distinct i.[IncidentNumber]) As [LiveCalls]
FROM
dbo.Incident AS i
Join dbo.Task AS t ON i.[Recid] = t.[ParentLink_RecID]
Where
i.[Status] IN ('Active','Waiting')
and t.[OwnerTeam] = 'Sales'
Group by
i.IncidentNumber,
i.Status,
t.OwnerTeam

GO

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-21 : 11:32:30
Please post sample Data (in DML Form), your expected Result, and the Table DDL

Read the hint link in my Sig


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-21 : 12:15:58
quote:
Originally posted by sz1

thats fair enough, I've just run this and I know whats happening now.I'm getting more records back because the sales team has an Incident somewhere in the record, so If I could only say give me the last record (the one which is live) that = sales then that would do it. Tried putting a MAX in front as below but still getting same result, thought that would have done it as Max should be the last record = sales?

Does this help?

ALTER View [dbo].[SDCalls]
AS


SELECT i.[IncidentNumber],Max(t.[OwnerTeam]) As [OwnerTeam],i.[Status],
COUNT(Distinct i.[IncidentNumber]) As [LiveCalls]
FROM
dbo.Incident AS i
Join dbo.Task AS t ON i.[Recid] = t.[ParentLink_RecID]
Where
i.[Status] IN ('Active','Waiting')
and t.[OwnerTeam] = 'Sales'
Group by
i.IncidentNumber,
i.Status,
t.OwnerTeam

GO




again this query makes no sense to us as we dont know how tables are related and have no idea of data it contains
so please post what Brett has asked for and then somebody will be able to help

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sz1
Aged Yak Warrior

555 Posts

Posted - 2012-03-22 : 08:08:09
Ok, I've looked thru an old view I created and its the CreatedDateTime field from the Task table I need to be the last record so I need to Max this for the last record that = 'ServiceDesk'

Here is some data with the duplicates (the duplicates are other teams, dont want other records just one unique, I know my Distinct clause is doing nothing!)
Meed to just show the last record from the CreateddateTime field

IncidentNum
1709 2009-04-30 14:14:57.000 Service Desk Waiting 1
1952 2009-05-01 10:10:22.000 Service Desk Waiting 1
1956 2009-05-01 10:13:24.000 Service Desk Waiting 1
1958 2009-05-01 10:16:25.000 Service Desk Waiting 1
1971 2009-05-01 10:39:52.000 Service Desk Waiting 1
1975 2009-05-01 10:45:02.000 Service Desk Waiting 1
1987 2009-05-01 11:01:07.000 Service Desk Waiting 1
1989 2009-05-01 11:06:21.000 Service Desk Waiting 1
1993 2009-05-01 11:11:19.000 Service Desk Waiting 1
1994 2009-05-01 11:13:40.000 Service Desk Waiting 1
1997 2009-05-01 11:15:59.000 Service Desk Waiting 1
2013 2009-05-01 11:46:48.000 Service Desk Waiting 1
2016 2009-05-01 11:49:07.000 Service Desk Waiting 1
2021 2009-05-01 11:57:08.000 Service Desk Waiting 1
2022 2009-05-01 11:59:25.000 Service Desk Waiting 1
2025 2009-05-01 12:02:19.000 Service Desk Waiting 1
2026 2009-05-01 12:09:49.000 Service Desk Waiting 1
4561 2009-05-13 11:39:48.000 Service Desk Waiting 1
6182 2009-05-20 10:49:07.000 Service Desk Waiting 1
18145 2009-07-14 13:42:24.000 Service Desk Waiting 1
18145 2009-07-14 15:42:20.000 Service Desk Waiting 1
--want this one, thought the Max would do it?






USE [IT1]
GO

/****** Object: View [dbo].[SDeskCalls] Script Date: 03/22/2012 11:38:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER View [dbo].[SDeskCalls]
AS

SELECT i.[IncidentNumber],
Max(t.[CreatedDateTime]) As LastAssignment, --only show last record
t.[OwnerTeam],
i.[Status],
COUNT(Distinct i.[IncidentNumber]) As [Live ITSM Calls]
FROM
dbo.Incident AS i
Join dbo.Task AS t ON i.[Recid] = t.[ParentLink_RecID]
Where
i.[Status] IN ('Active','Waiting')
and t.[OwnerTeam] = 'Service Desk'
--and t.[OwnerTeam] Not In ('Asset Management','Systems')
Group by
i.IncidentNumber,
t.CreatedDateTime,
i.Status,
t.OwnerTeam

--Order By i.[IncidentNumber]

--select * from [SDeskCalls]

GO


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-22 : 15:40:06
so count should come as 1 or 2?


USE [IT1]
GO

/****** Object: View [dbo].[SDeskCalls] Script Date: 03/22/2012 11:38:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER View [dbo].[SDeskCalls]
AS

SELECT i.[IncidentNumber],
Max(t.[CreatedDateTime]) As LastAssignment, --only show last record
t.[OwnerTeam],
i.[Status],
COUNT(Distinct i.[IncidentNumber]) As [Live ITSM Calls]
FROM
dbo.Incident AS i
Join dbo.Task AS t ON i.[Recid] = t.[ParentLink_RecID]
Where
i.[Status] IN ('Active','Waiting')
and t.[OwnerTeam] = 'Service Desk'
--and t.[OwnerTeam] Not In ('Asset Management','Systems')
Group by
i.IncidentNumber,
i.Status,
t.OwnerTeam

--Order By i.[IncidentNumber]

--select * from [SDeskCalls]

GO





------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sz1
Aged Yak Warrior

555 Posts

Posted - 2012-03-22 : 16:09:50
Well you could say that..for the incident number below "18145" there are 2 records that = Service Desk, but there could be multiple records, you could say each time one is found for that team count up so 1,2,3...but there are other teams that have records too, I just want to say give me the latest/last CreatedDateTime record = Service Desk, so all other records with another team that is last I dont want to see.

Any good?

18145 2009-07-14 13:42:24.000 Service Desk Waiting 1
18145 2009-07-14 15:42:20.000 Service Desk Waiting 1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-22 : 21:19:21
nope...
see how to give correct information for a question

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -