Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4347404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:18:06+00:00 2026-05-21T12:18:06+00:00

I am trying to develop a T-SQL query which will do the following: ROUND(100

  • 0

I am trying to develop a T-SQL query which will do the following:

ROUND(100 * A / B, 1)

Simple in concept, but it’s tricky because of possible B=0 denominator and also because of A and B variables. What I expect is a percent value like 93.2 (given in this format without %). Or even 932 would be acceptable since I could convert it later.

But instead, I’m currently getting 151, which is the number of records.

A = CASE WHEN A.MFG IS NULL AND A.MFG2 IS NULL AND A.QC IS NULL AND A.QC2 IS NULL THEN 1 ELSE 0 END
B = CASE WHEN [Date_Completed] IS NOT NULL THEN 1 ELSE 0 END

My current logic only divides A/B if B is not equal to zero. Can you please help me fix this? p.s. all fields above are from the same table A.

I tried:

SELECT CASE WHEN t.VarB<>0 THEN ROUND(100 * t.VarA / t.VarB, 1) 
ELSE 0 /* or whatever you'd want to return in this case */ 
END 
FROM (SELECT CASE WHEN A.MFG IS NULL AND A.MFG2 IS NULL AND A.QC IS NULL AND A.QC2 IS NULL THEN 1 
ELSE 0 
END AS VarA, 
CASE WHEN [Date_Completed] IS NOT NULL THEN 1 
ELSE 0 
END AS VarB 
FROM EXCEL.Batch_Records A) t

But I got 33000 rows returned instead of just one, where each row = 100 or 0.

Good idea, Conrad! I tested your solution and it works if I just want that one value. But what I didn’t tell you was that there are additional values I need returned from same query. When I tried adding in the other value calculations, I got syntax errors. So here is my current query. How should htis be rewritten please?

select 
SUM(CASE WHEN A.DATE_RECEIVED IS NOT NULL THEN 1 ELSE 0 END) AS NUM_RECEIVED,
SUM(CASE WHEN [Date_Completed] IS NOT NULL THEN 1 ELSE 0 END) AS NUM_COMPLETE_OF_OPENED,
SUM(CASE WHEN A.DATE_COMPLETED IS NOT NULL THEN 1 ELSE 0 END) AS NUM_COMPLETED_IN_MONTH,
SUM(CASE WHEN A.MFG IS NULL AND A.MFG2 IS NULL AND A.QC IS NULL AND A.QC2 IS NULL THEN 1 ELSE 0 END) AS NUM_WITHOUT_ERROR,

round(100 * a/b , 1) 
from 
(select 
    sum(CASE  
        WHEN A.MFG IS NULL AND A.MFG2 IS NULL AND A.QC IS NULL AND A.QC2 IS NULL THEN  
            1.0  
        ELSE 0.0 END) A, 
    sum(CASE WHEN [Date_Completed] IS NOT NULL THEN 

1.0 ELSE 0.0 END) B 

FROM EXCEL.Batch_Records a 
LEFT JOIN EXCEL.QC_CODES d ON a.Part_Number = d.CODE_ID    
WHERE (a.[Group] = @GROUP or @GROUP = '' OR @GROUP IS NULL) AND A.Date_Received >= @STARTDATE AND A.Date_Received <= @ENDDATE

Conrad correctly advised me that #TEMP1 was an empty table. But now I populated it and successfully designed this query with his help:

SET @STARTDATE = '1/1/11'
SET @ENDDATE = '1/31/11'
SET @GROUP = 'INTERMEDIATES_FISH'
--SET @TABLE_TITLE = 'BATCH RECORD SUCCESS RATE'
--SET @DEPT = 'QC'     

IF EXISTS(SELECT * FROM TEMPDB.INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '#TEMP1%')
DROP TABLE #TEMP1

--CREATE TABLE #TEMP1 (     MFG int ,      MFG2 int ,     QC int,      QC2 INT ,      [Group] NVARCHAR(MAX),     [Date_Completed] datetime,     Date_Received datetime)
SELECT
MFG, MFG2, QC, QC2, [GROUP], [DATE_COMPLETED], [DATE_RECEIVED]
INTO #TEMP1
FROM EXCEL.Batch_Records a 
WHERE (a.[Group] = @GROUP or @GROUP = '' OR @GROUP IS NULL) AND A.Date_Received >= @STARTDATE AND A.Date_Received <= @ENDDATE

------------------------------------------  
;WITH CTE AS 
( 
SELECT 
CASE 
WHEN A.MFG IS NULL AND A.MFG2 IS NULL AND A.QC IS NULL AND A.QC2 IS NULL THEN 
1.0 
ELSE 0.0 END A, 
CASE WHEN [Date_Completed] IS NOT NULL THEN 1.0 ELSE 0.0 END B, 
CASE WHEN A.Date_Received IS NOT NULL THEN 1 ELSE 0 END NUM_RECEIVED, 
CASE WHEN [Date_Completed] IS NOT NULL THEN 1 ELSE 0 END NUM_COMPLETE_OF_OPENED, 
CASE WHEN A.DATE_COMPLETED IS NOT NULL THEN 1 ELSE 0 END NUM_COMPLETED_IN_MONTH, 
CASE WHEN A.MFG IS NULL AND A.MFG2 IS NULL AND A.QC IS NULL AND A.QC2 IS NULL THEN 1 ELSE 0 END AS NUM_WITHOUT_ERROR 
FROM 
#TEMP1 a 
--WHERE (a.[Group] = @GROUP or @GROUP = '' OR @GROUP IS NULL) AND A.Date_Received >= @STARTDATE AND A.Date_Received <= @ENDDATE
) 

select 
round(100 * SUM(A)/SUM(b) , 1) , 
SUM(NUM_RECEIVED) NUM_RECEIVED, 
SUM(NUM_COMPLETE_OF_OPENED) NUM_COMPLETE_OF_OPENED, 
SUM(NUM_COMPLETED_IN_MONTH) NUM_COMPLETED_IN_MONTH, 
SUM(NUM_WITHOUT_ERROR) NUM_WITHOUT_ERROR 


FROM CTE 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:18:07+00:00Added an answer on May 21, 2026 at 12:18 pm

    Basically you need to use SUM() to get the sum. You should also use 1.0 and 0.0 so you get decimal values.

    You should also do the SUM before the Division

    UPDATE
    Since you’re adding in a number of SUM(CASE statements its probably more readable to move the CASE statments out to a CTE.

    CREATE TABLE #Batch_Records (
        MFG int , 
        MFG2 int ,
        QC int, 
        QC2 INT , 
        [Group] int,
        [Date_Completed] datetime,
        Date_Received datetime)
    
    
    
    
    INSERT INTO #Batch_Records (MFG ,   MFG2 ,  QC ,    QC2  ,  [Group] ,   [Date_Completed] ,  Date_Received )
    VALUES (1,null,null,null,1,'1/4/2011','2/4/2011'),
           (null,null,null,null,1,'2/2/2011','3/4/2011'),
           (1,null,null,null,1,'3/6/2011','4/3/2011'),
           (null,null,null,null,1,NULL,'5/4/2011'),
           (1,null,null,null,1,'5/4/2011','6/6/2011'),
           (1,null,null,null,1,NULL,'7/4/2011')
    
    
    DECLARE @GROUP int
    DECLARE @STARTDATE DateTime
    DECLARE @ENDDATE DateTime
    
    SET @GROUP = 1
    SET @STARTDATE = '1/1/2001'
    SET @ENDDATE = '1/1/2012'
    
    ;WITH CTE AS
    (
        SELECT
            CASE  
                WHEN A.MFG IS NULL AND A.MFG2 IS NULL AND A.QC IS NULL AND A.QC2 IS NULL THEN  
                    1.0  
                ELSE 0.0 END A, 
            CASE WHEN [Date_Completed] IS NOT NULL THEN 
            1.0 ELSE 0.0 END B,
            CASE WHEN A.Date_Received IS NOT NULL THEN 1 ELSE 0 END  NUM_RECEIVED,
            CASE WHEN [Date_Completed] IS NOT NULL THEN 1 ELSE 0 END  NUM_COMPLETE_OF_OPENED,   
            CASE WHEN A.DATE_COMPLETED IS NOT NULL THEN 1 ELSE 0 END  NUM_COMPLETED_IN_MONTH,
            CASE WHEN A.MFG IS NULL AND A.MFG2 IS NULL AND A.QC IS NULL AND A.QC2 IS NULL THEN 1 ELSE 0 END AS NUM_WITHOUT_ERROR
        FROM 
            #Batch_Records a 
        WHERE 
            (a.[Group] = @GROUP or @GROUP = '' OR @GROUP IS NULL) 
            AND A.Date_Received >= @STARTDATE AND A.Date_Received <= @ENDDATE
    )
    
    select 
        round(100 * SUM(A)/SUM(b) , 1) ,
        SUM(NUM_RECEIVED) NUM_RECEIVED,
        SUM(NUM_COMPLETE_OF_OPENED) NUM_COMPLETE_OF_OPENED,
        SUM(NUM_COMPLETED_IN_MONTH) NUM_COMPLETED_IN_MONTH,
        SUM(NUM_WITHOUT_ERROR) NUM_WITHOUT_ERROR
    
    
     FROM CTE
    
     DROP TABLE #Batch_Records
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My scenary: I am trying to develop a service which will query different databases.
I am trying to develop a query to insert unique records but am receiving
I'm trying to develop a little C# application (with MS Visual Express and SQL
I am trying to develop a .NET Web Project using NHibernate and Spring.NET, but
I'm trying to develop an application that will use getImageData in javascript in Firefox
I've been trying to develop a linq query that returns the ItemNumber column of
I am trying to develop a report in SQL Server Reporting Services using parameters.
I'm trying to develop a script that I will be able to run on
We are trying to develop a company specific tracking software but not interested in
I'm trying to develop an ASP SQL connection to a MSSQL 2005 server. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.