SQL Variables

SQL 2008 Management Studio

The attached scripting works fine ... but as this is calculating totals, is there a way of utilising those values are variable (to prevent unncessary further sql searches)...such as adding numbers together or percentages etc.

the existing values created by script are as follows:

TotalCreated, Hoax, Invalids, DNCA

would like add more columns to be displayed as followed:

TotalNonActive == (Hoax+Invalids+DNCA)
TotalAvail == (TotalCreated-TotalNonActive)

other calculations such as percentages:
PercentageAvail == TotalAvail as a percentage of TotalCreated
PercentageDNCA == DNCA as a percentage of TotalCreated

Is this possible in a SQL query ?
SELECT a.LeadSourceProvider_ID, a.LeadSourceProviderName, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID ) as TotalCreated, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND HoaxLeadStatus=1) as Hoax, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND InvalidNumbersStatus=1) as Invalids, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND DoNotCallAgainStatus=1) as DNCA

FROM LeadSourceProviders a

Open in new window

amillyardAsked:
Who is Participating?
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.

Aneesh RetnakaranDatabase AdministratorCommented:
SELECT a.*, TotalNonActive == (Hoax+Invalids+DNCA) , TotalAvail == (TotalCreated-TotalNonActive)
FROM
(
SELECT a.LeadSourceProvider_ID, a.LeadSourceProviderName, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID ) as TotalCreated, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND HoaxLeadStatus=1) as Hoax, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND InvalidNumbersStatus=1) as Invalids, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND DoNotCallAgainStatus=1) as DNCA
FROM LeadSourceProviders a
) A
0
amillyardAuthor Commented:
Hi there, thanks for the response.

I am getting the following syntax error as follows:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near 'A'.
0
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT a.*, TotalNonActive = (Hoax+Invalids+DNCA) , TotalAvail = (TotalCreated-TotalNonActive)
FROM
(
SELECT a.LeadSourceProvider_ID, a.LeadSourceProviderName, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID ) as TotalCreated, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND HoaxLeadStatus=1) as Hoax, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND InvalidNumbersStatus=1) as Invalids, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND DoNotCallAgainStatus=1) as DNCA
FROM LeadSourceProviders a
) A
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

amillyardAuthor Commented:
almost there -- just this syntax error as follows:

Msg 207, Level 16, State 1, Line 2
Invalid column name 'TotalNonActive'.
0
Chris LuttrellSenior Database ArchitectCommented:
And in the same line, adding the percentage calcs are as easy as this:
SELECT a.*, TotalNonActive = (Hoax+Invalids+DNCA) , TotalAvail = (TotalCreated-TotalNonActive), PercentageAvail = (TotalCreated-TotalNonActive) / TotalCreated * 100.0, PercentageDNCA = DNCA / TotalCreated * 100.0
FROM 
(
SELECT a.LeadSourceProvider_ID, a.LeadSourceProviderName, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID ) as TotalCreated, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND HoaxLeadStatus=1) as Hoax, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND InvalidNumbersStatus=1) as Invalids, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND DoNotCallAgainStatus=1) as DNCA
FROM LeadSourceProviders a
) A

Open in new window

0
Chris LuttrellSenior Database ArchitectCommented:
oops, just saw your last post, try this one then
SELECT a.*, TotalNonActive = (Hoax+Invalids+DNCA) , TotalAvail = (TotalCreated-(Hoax+Invalids+DNCA)), PercentageAvail = (TotalCreated-(Hoax+Invalids+DNCA)) / TotalCreated * 100.0, PercentageDNCA = DNCA / TotalCreated * 100.0 
FROM  
( 
SELECT a.LeadSourceProvider_ID, a.LeadSourceProviderName, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID ) as TotalCreated, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND HoaxLeadStatus=1) as Hoax, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND InvalidNumbersStatus=1) as Invalids, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND DoNotCallAgainStatus=1) as DNCA 
FROM LeadSourceProviders a 
) A

Open in new window

0
amillyardAuthor Commented:
CGLuttrell:

I see.  I'm sure that works -- just that I am still getting a syntax error relating to the variable:

TotalNonActive -- this compiles and displays fine if used by itself...just when referenced in the scripting (on right of that same line).
0
amillyardAuthor Commented:
ok thanks - have updated.

am getting computational error as follows:

Msg 8134, Level 16, State 1, Line 2
Divide by zero error encountered.

I suspect a couple of the fields are    zero   value - how does SQL handle this please?
0
Chris LuttrellSenior Database ArchitectCommented:
You have to wrap those with a case statement.
SELECT a.*, TotalNonActive = (Hoax+Invalids+DNCA) , TotalAvail = (TotalCreated-(Hoax+Invalids+DNCA)), PercentageAvail = CASE when TotalCreated = 0 THEN 0 ELSE (TotalCreated-(Hoax+Invalids+DNCA)) / TotalCreated * 100.0) END, PercentageDNCA = CASE when TotalCreated = 0 THEN 0 ELSE DNCA / TotalCreated * 100.0  ) END
FROM   
(  
SELECT a.LeadSourceProvider_ID, a.LeadSourceProviderName, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID ) as TotalCreated, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND HoaxLeadStatus=1) as Hoax, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND InvalidNumbersStatus=1) as Invalids, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND DoNotCallAgainStatus=1) as DNCA  
FROM LeadSourceProviders a  
) A

Open in new window

0
amillyardAuthor Commented:
sorry... getting syntax error:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near ')'.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near 'A'.
0
Chris LuttrellSenior Database ArchitectCommented:
Sorry, a couple misplaced ).  Hard to add logic on the fly without being able to run it.  Try this one.
SELECT a.*, TotalNonActive = (Hoax+Invalids+DNCA) , TotalAvail = (TotalCreated-(Hoax+Invalids+DNCA)), PercentageAvail = CASE when TotalCreated = 0 THEN 0 ELSE (TotalCreated-(Hoax+Invalids+DNCA)) / TotalCreated * 100.0 END, PercentageDNCA = CASE when TotalCreated = 0 THEN 0 ELSE DNCA / TotalCreated * 100.0 END 
FROM    
(   
SELECT a.LeadSourceProvider_ID, a.LeadSourceProviderName, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID ) as TotalCreated, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND HoaxLeadStatus=1) as Hoax, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND InvalidNumbersStatus=1) as Invalids, (SELECT COUNT(*) FROM MasterAccounts m WHERE m.LeadSourceProvider_ID = a.LeadSourceProvider_ID AND DoNotCallAgainStatus=1) as DNCA   
FROM LeadSourceProviders a   
) A

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
amillyardAuthor Commented:
CGLuttrell: spot - on -- great :-)   thank you for persisting.
0
Chris LuttrellSenior Database ArchitectCommented:
Your Welcome! Glad I could help.
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
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.