Solved

SQL % Calculation

Posted on 2015-02-06
5
124 Views
Last Modified: 2015-02-06
have a very basic qry  that counts number of active customers vs those customers that have been verified with current data and shows the percent of "verified" accounts.

The date is showing 186 verified out of 325 Active, but my percent results show 39.42 not 57.23



SELECT  COUNT(uppnverify) AS 'Verified GM Customers'
FROM CONTACT2
WHERE UPPNVERIFY like '201%'


SELECT COUNT(ACCOUNTNO)AS 'Active Customers'
FROM CONTACT2
WHERE USALESYTD IS NOT NULL AND USALES2014 IS NOT NULL



SELECT CAST([Verified Customers] * 100.0 / [Active Customers] AS decimal(4, 2)) AS [%age of GM verified customers]
    --, [Verified Customers], [Active Customers]
FROM (
    SELECT
        --count number of GM company with verified field
        SUM(CASE WHEN UPPNVERIFY like '201%' THEN 1 ELSE 0 END) AS [Verified Customers],
        -- count number of GM company with active status (sales in last 2 yr)
        SUM(CASE WHEN USALESYTD IS NOT NULL AND USALES2013 IS NOT NULL THEN 1 ELSE 0 END) AS [Active Customers]
    FROM CONTACT2
    WHERE
        (UPPNVERIFY like '2015%') OR
        (USALESYTD IS NOT NULL AND USALES2014 IS NOT NULL)
) AS subquery1
0
Comment
Question by:Jeremy Kirkbride
  • 2
  • 2
5 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40593645
Your WHERE clause uses (UPPNVERIFY like '2015%') which may be excluding results you are expecting with just (UPPNVERIFY like '201%').
0
 

Author Comment

by:Jeremy Kirkbride
ID: 40593845
Gotcha, changed the statement to 201%, the percentage is much closer but still calculating 59.6, math has never been my strong point, but I get 57% from 186 out of 325

SELECT  COUNT(uppnverify) AS 'Verified GM Customers'
FROM CONTACT2
WHERE UPPNVERIFY like '201%'


SELECT COUNT(ACCOUNTNO)AS 'Active Customers'
FROM CONTACT2
WHERE USALESYTD IS NOT NULL AND USALES2014 IS NOT NULL



SELECT CAST([Verified Customers] * 100.0 / [Active Customers] AS decimal(4, 1)) AS [%age of GM verified customers]
    --, [Verified Customers], [Active Customers]
FROM (
    SELECT
        --count number of GM company with verified field
        SUM(CASE WHEN UPPNVERIFY like '201%' THEN 1 ELSE 0 END) AS [Verified Customers],
        -- count number of GM company with active status (sales in last 2 yr)
        SUM(CASE WHEN USALESYTD IS NOT NULL AND USALES2013 IS NOT NULL THEN 1 ELSE 0 END) AS [Active Customers]
    FROM CONTACT2
    WHERE
        (UPPNVERIFY like '201%') OR
        (USALESYTD IS NOT NULL AND USALES2014 IS NOT NULL)
) AS subquery1
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40593908
The calc will work, since computers don't mess up simple math :-).

Uncomment the other values in the outer query and you can see the actual totals SQL is using, which will differ from your expected totals of 186 and 325 somewhat, which will explain the difference in %.

SELECT CAST([Verified Customers] * 100.0 / [Active Customers] AS decimal(4, 1)) AS [%age of GM verified customers]
     , [Verified Customers], [Active Customers]


Edit: Just saw the data conflict in the query:
...
 SUM(CASE WHEN USALESYTD IS NOT NULL AND USALES2013 IS NOT NULL THEN 1 ELSE 0 END) AS [Active Customers]
     FROM CONTACT2
     WHERE
         (UPPNVERIFY like '201%') OR
         (USALESYTD IS NOT NULL AND USALES2014 IS NOT NULL)
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40593912
Another WHERE Clause discrepancy:
SUM(CASE WHEN USALESYTD IS NOT NULL AND USALES2013 IS NOT NULL THEN 1 ELSE 0 END)

versus:
 (USALESYTD IS NOT NULL AND USALES2014 IS NOT NULL)

Looks like your result set may not be returning the records you are expecting.
0
 

Author Closing Comment

by:Jeremy Kirkbride
ID: 40594062
Thanks so much. All good now
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Backup skipping a few tables 7 29
sql query 7 37
SQL Connection (Error 18456) 14 36
Change part of a string 2 24
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now