DECLARE @dd TABLE (Username VARCHAR(250), WorkArea VARCHAR(50), Scope VARCHAR(50), Quality INT)
insert into @dd (Username ,WorkArea ,Scope ,Quality) select QAResource,WorkArea,ProjectScope ,quality from[PMT] .dbo.QADetailsnew
insert into @dd (Username ,WorkArea ,Scope ,Quality)select a.resources,a.work_area,a.scope, a.Quality from [workalloc] .[dbo] .work a
;WITH UserTotals AS
(
SELECT LEFT(Username, CHARINDEX(',', Username )-1) AS SingleUser, Quality, RIGHT(Username, LEN(Username) - CHARINDEX(',', Username)) AS Remainder
FROM @dd
WHERE CHARINDEX(',', Username)>0
union all
select Username as SingleUser,Quality, NULL as Remainder
FROM @dd
WHERE CHARINDEX(',',Username )=0
UNION ALL
SELECT LEFT(Remainder, CHARINDEX(',', Remainder)-1) AS SingleUser, Quality, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(',', Remainder)) AS Remainder
FROM UserTotals
WHERE CHARINDEX(',', Remainder)>0
UNION ALL
SELECT Remainder AS SingleUser, Quality, NULL AS Remainder
FROM UserTotals
WHERE CHARINDEX(',', Remainder)=0
)
select distinct(SingleUser),(sum(Quality)/COUNT(SingleUser)) as QualityPercentage, 100-sum(Quality)/COUNT(SingleUser ) as ErrorPercentage
from UserTotals
group by SingleUser
SINGLEUSER Quality% Error%
John 100 0
Robinson 100 0
Mary 99 1
Rada 66 34
Anurag 55 45
Ashish 32 68
Denzil 98 2
Reena 88 12
Martin 55 45
Robinson 94 6
I have two tables from different databases. First database is
PMT
in which i have named
QADetailsnew
Table.
In the second database is
Workalloc
and the table name is
work
.
In both of the tables i have the entry for the Username Robinson which gets executed twice because there is entry in the both the tables in different databases i need to get the sum of both. I have used the keyword
distinct
but it doesnt work in this case.