calculating percentage in query

i am trying to calculate dispositions for certain agents in the table. I am trying to calculate the percentage of SAVES(disposition) for an agent by total number of calls taken by him.  I have many databases. i am union the databases. For right now i am using only 2. There are many different kinds of dispositions. like cancel, wrong number etc. I am looking for particular disposition
This is how the data should looks like.

agnetname   disposition    database1         database2         TotalCalls     percentofcalls
bill                    save               1                             2                     5                      60%
Joe                   save               0                            5                     10                     50%
Mike                  save               2                           5                      20                     35%

This is kind of sql i have.  I need to look for a particular disp and then calculate the percentage.

declare @StartDate datetime
declare @EndDate datetime
set @StartDate='4/15/2009'
set @EndDate='6/20/2009'

;with rawdata as (

SELECT LocalUserId as [name],disposition,
COUNT(*) AS TCDatabase1, 0 as TCDatabase2
FROM
test1.dbo.tblCallReceived
WHERE
CallDate BETWEEN @StartDate AND @EndDate

GROUP BY Disposition, LocalUserId
-----------------------------------------------------------------------------------------------------------
union all

SELECT LocalUserId as [name],disposition,
0 AS TCDatabase1,count(*) as TCDatabase2
FROM
test2.dbo.tblCallReceived
WHERE
CallDate BETWEEN @StartDate AND @EndDate
GROUP BY Disposition, LocalUserId
), totals as
select [name],disposition,sum(TCDatabase1) as ACAI, sum(TCDatabase2)AS ACAIXS,

SUM(TCDatabase1+TCDatabase2)

as Total_Calls
from rawdata
GROUP BY Disposition, name
)
SELECT *
, Total_Calls * 100.0 / (SELECT SUM(Total_Calls) FROM totals) AS PercentOfCalls
FROM totals
ORDER BY name, Disposition

                     
LVL 1
soorrajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pssandhuCommented:
Try this:
declare @StartDate datetime
declare @EndDate datetime
set @StartDate='4/15/2009'
set @EndDate='6/20/2009'
 
;With rawdata as (
 
SELECT LocalUserId as [name],disposition, COUNT(*) AS TCDatabase1, 0 as TCDatabase2
FROM test1.dbo.tblCallReceived
WHERE CallDate BETWEEN @StartDate AND @EndDate
GROUP BY Disposition, LocalUserId
 
UNION ALL
 
SELECT LocalUserId as [name],disposition,0 AS TCDatabase1,count(*) as TCDatabase2
FROM test2.dbo.tblCallReceived
WHERE CallDate BETWEEN @StartDate AND @EndDate
GROUP BY Disposition, LocalUserId
), Totals as 
Select	LocalUserID, 
		[name], 
		disposition, 
		SUM(TCDatabase1) as ACAI, 
		SUM(TCDatabase2)AS ACAIXS,
		SUM(TCDatabase1+TCDatabase2) as DispTotal, 
		SUM(TCDatabase1+TCDatabase2) OVER(partition by LocalUserID Order by LocalUserID)  as Total_Calls
From	rawdata   
GROUP BY LocalUserID, Disposition, name
Order by LocalUserID
 
 
SELECT *, DispTotal * 100.0 / Total_Calls AS PercentOfCalls 
FROM totals
ORDER BY name, Disposition

Open in new window

0
soorrajAuthor Commented:
WIth you query i am geting an error
Msg 156, Level 15, State 1, Line 20
Incorrect syntax near the keyword 'Select'.
Msg 102, Level 15, State 1, Line 26
Incorrect syntax near 'order'.

I also did it but i am getting two rows for agent for two different databases.

This is how i did it

;with rawdata as (

SELECT LocalUserId as [name],
sum(case disposition when 'save' then 1 else 0 end) as saves,
COUNT(*) AS TCDatabase1, 0 as TCDatabase2
FROM
test1.dbo.tblCallReceived
WHERE
CallDate BETWEEN '5/15/2009' AND '7/16/2009' AND (callid is NULL OR len(callid) > 2)
GROUP BY LocalUserId

-----------------------------------------------------------------------------------------------------------
union all
-----------------------------------------------------------------------------------------------------------

SELECT LocalUserId as [name],sum(case disposition when 'save' then 1 else 0 end) as saves,
0 AS TCDatabase1,count(*) as TCDatabase2
FROM
test2.dbo.tblCallReceived
WHERE
CallDate BETWEEN '5/15/2009' AND '7/16/2009' AND (callid is NULL OR len(callid) > 2)
GROUP BY  LocalUserId
),

totals as (
select [name],saves,sum(TCDatabase1) as test1, sum(TCDatabase2)AS test2,

SUM(TCDatabase1+TCDatabase2)

as Total_Calls
from rawdata
GROUP BY [name],saves
)
SELECT *
, saves * 100.0 / Total_Calls AS PercentOfCalls
FROM totals
ORDER BY name

The data is showing up like this
name                saves               test1                   test2                 totacalls               percentofcalls
jon                         2                    0                        2                             2                               100
jon                          3                    5                       0                             5                                60%

but i want data to show up like this

name                saves               test1                   test2                 totacalls               percentofcalls
jon                         5                   5                          2                           7                             71.42



0
pssandhuCommented:
Okay, I am little confused. Don't you have more then one disposition? If yes, then are you not calculating for all dispositions or just for save as you posted above.
In your above code do not group by column "saves" and you will get one record for each user.
Also, try this, I was missing brackets:

declare @StartDate datetime
declare @EndDate datetime
set @StartDate='4/15/2009'
set @EndDate='6/20/2009'
 
;With rawdata as (
 
SELECT LocalUserId as [name],disposition, COUNT(*) AS TCDatabase1, 0 as TCDatabase2
FROM test1.dbo.tblCallReceived
WHERE CallDate BETWEEN @StartDate AND @EndDate
GROUP BY Disposition, LocalUserId
 
UNION ALL
 
SELECT LocalUserId as [name],disposition,0 AS TCDatabase1,count(*) as TCDatabase2
FROM test2.dbo.tblCallReceived
WHERE CallDate BETWEEN @StartDate AND @EndDate
GROUP BY Disposition, LocalUserId
), Totals as (
Select	[name], 
		disposition, 
		SUM(TCDatabase1) as ACAI, 
		SUM(TCDatabase2)AS ACAIXS,
		SUM(TCDatabase1+TCDatabase2) as DispTotal, 
		SUM(TCDatabase1+TCDatabase2) OVER(partition by [name] Order by [name])  as Total_Calls
From	rawdata   
GROUP BY Name, Disposition
Order by name, 
) 
-- For each disposition 
SELECT *, DispTotal * 100.0 / Total_Calls AS PercentOfCalls 
FROM totals
ORDER BY name, Disposition

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

soorrajAuthor Commented:
i have never used over Partition . just curious how will i use SAVE disp in your query.
it is still throwing an error on this line

  SUM(TCDatabase1+TCDatabase2) OVER(partition by [name] Order by [name])  as Total_Calls

Incorrect syntax near 'order'.
0
pssandhuCommented:
Sorry yea, have to take out the order by clause:

declare @StartDate datetime
declare @EndDate datetime
set @StartDate='4/15/2009'
set @EndDate='6/20/2009'
 
;With rawdata as (
 
SELECT LocalUserId as [name],disposition, COUNT(*) AS TCDatabase1, 0 as TCDatabase2
FROM test1.dbo.tblCallReceived
WHERE CallDate BETWEEN @StartDate AND @EndDate
GROUP BY Disposition, LocalUserId
 
UNION ALL
 
SELECT LocalUserId as [name],disposition,0 AS TCDatabase1,count(*) as TCDatabase2
FROM test2.dbo.tblCallReceived
WHERE CallDate BETWEEN @StartDate AND @EndDate
GROUP BY Disposition, LocalUserId
), Totals as (
Select	[name], 
		disposition, 
		SUM(TCDatabase1) as ACAI, 
		SUM(TCDatabase2)AS ACAIXS,
		SUM(TCDatabase1+TCDatabase2) as DispTotal, 
		SUM(TCDatabase1+TCDatabase2) OVER(partition by [name])  as Total_Calls
From	rawdata   
GROUP BY Name, Disposition
Order by name, 
) 
-- For each disposition 
SELECT *, DispTotal * 100.0 / Total_Calls AS PercentOfCalls 
FROM totals
ORDER BY name, Disposition

Open in new window

0
pssandhuCommented:
Please run to see if those are the results you are looking for. If not, then please post back how they are and how you want them to be based on the result set you get from the above query. Hopefully there should be no more errors.
P.
0
soorrajAuthor Commented:
Well there are still errors
It gives this error  on this line Order by name
the ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

If i comment it out then it gives
Msg 8120, Level 16, State 1, Line 6
Column 'rawdata.TCDatabase1' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 6
Column 'rawdata.TCDatabase2' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

When i add the TCDatabase1,TCDatabase2 in the group by clause it works but duplicate data.
 It should look like this. i am looking for the disposition SAVE. This needs to be in the query

name                saves               test1                   test2                 totacalls               percentofcalls
jon                         5                   5                          2                           7                             71.42
Luke                      7                   2                           10                       12                              58.33

0
pssandhuCommented:
Alright, I have looked at the errors and have tried to clean up the syntax, please see if this runs okay and you get your desired results:
DECLARE	@StartDate datetime,
		@EndDate datetime
 
SET @StartDate	= '4/15/2009'
SET @EndDate	= '6/20/2009'
 
;With RawData as (
 
SELECT LocalUserId as UserName,Disposition, COUNT(*) AS TCDatabase1, 0 as TCDatabase2
FROM test1.dbo.tblCallReceived
WHERE CallDate BETWEEN @StartDate AND @EndDate
GROUP BY Disposition, LocalUserId
 
UNION ALL
 
SELECT LocalUserId as UserName,Disposition,0 AS TCDatabase1,count(*) as TCDatabase2
FROM test2.dbo.tblCallReceived
WHERE CallDate BETWEEN @StartDate AND @EndDate
GROUP BY Disposition, LocalUserId
), Totals as ( 
Select	UserName, 
		Disposition, 
		TCDatabase1, 
		TCDatabase2,
		TCDatabase1+TCDatabase2 as Disp, 
		SUM(TCDatabase1+TCDatabase2) OVER(partition by UserName)  as Total_Calls
From	RawData
), GrandTotal as (
Select	UserName, 
		Disposition, 
		SUM(TCDatabase1) as ACAI, 
		SUM(TCDatabase2) as ACAIXS,
		SUM(Disp) as DispTotal, 
		MAX(Total_Calls) as Ttl_Calls
From	Totals
Group by UserName, Disposition
)
 
SELECT	*, (DispTotal*100)/Ttl_Calls AS PercentOfCalls 
FROM	GrandTotal
ORDER BY UserName, Disposition

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
soorrajAuthor Commented:
Well it i still not giving the right results. I played around with your sql and i then i figured it out. Thanks for your help
0
pssandhuCommented:
Sometimes it is nice to have an example dataset to work to get exact results, mostly when the query is a little complicated. But on the other hand figuring it out yourself is always and good learning curve.
No problem. I am glad to what ever extent I could help.
Cheers!
P.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.