Click here to Skip to main content
16,006,355 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
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
(
        -- initial values
 
	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
	
        -- here starts recursive part
	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.
Posted
Updated 18-May-15 2:33am
v4

1 solution

Replace this:
SQL
select distinct(SingleUser),(sum(Quality)/COUNT(SingleUser)) as QualityPercentage, 100-sum(Quality)/COUNT(SingleUser ) as ErrorPercentage
from UserTotals

with:
SQL
SELECT SingleUser, AVG(Quality) as QualityPercentage, 100-AVG(Quality) as ErrorPercentage
FROM UserTotals
GROUP BY SingleUser


For further information , please see:
AVG (T-SQL)[^]
GROUP BY (T-SQL)[^]
Aggregate functions[^]
 
Share this answer
 
Comments
Sascha Lefèvre 18-May-15 12:11pm    
5ed
Maciej Los 18-May-15 13:26pm    
Thank you, Sascha ;)
SukirtiShetty 19-May-15 0:37am    
It doesnot work. It showed the same result.
Please help me on this
Maciej Los 19-May-15 2:17am    
I have no idea why it deosn't work. Note that i can't see your data... Generally, the answer is correct. You have to detect where did you make a mistake.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900