?
Solved

SQL Server 2008 - Query Calculation Issue

Posted on 2013-12-18
10
Medium Priority
?
325 Views
Last Modified: 2013-12-18
I am running this query below on SQL Server 2008:

Select Distinct
      EI.FIRSTNAME + ' ' + EI.LASTNAME as 'Employee Name',
      EI.NUM as 'Employee Id',
      EI.SHIFT as 'Shift',
      Count(Distinct CONVERT(VARCHAR(10),Date_Time,111)) as 'Days Worked',
      Count(Distinct TR.Serial_Number) as 'Total Coils Processed',
      PC.PassCoilCount as 'Chamber Coils Good',      
      PC.PassCoilCount / Count(Distinct TR.Serial_Number) * 100 as 'Chamber Coils Pass %',
      Count(Distinct TR.Serial_Number) / Count(Distinct CONVERT(VARCHAR(10),TR.Date_Time,111)) as 'Chamber Avg Coils/Day'
FROM
      AIO_Test_Results.dbo.Test_Results TR JOIN AIO_Test_Results.dbo.EmployeeInfo EI
      ON TR.Op_ID = EI.FIRSTNAME + ' ' + EI.LASTNAME
      JOIN @PassedCoils PC ON  PC.EmployeeName = TR.Op_ID
Where
      (Date_Time BETWEEN '2013-11-10 16:32:35.757' AND '2013-12-17 16:32:35.757') AND
      Len(Serial_Number) >=15 AND (System_ID LIKE '%Chamber%')
      AND EI.NUM IN (SELECT * FROM dbo.fn_SplitDelimiterString(@EmployeeList, ','))
Group By
      Shift, EI.FIRSTNAME + ' ' + EI.LASTNAME, EI.NUM  ,PC.PassCoilCount
Order By
      Shift, EI.FIRSTNAME + ' ' + EI.LASTNAME


-----------------------------------------------

Chamber Coils Pass % - is returning  0

any idea why it is not showing actual value..

Thanks
0
Comment
Question by:mani_sai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 11

Expert Comment

by:Simone B
ID: 39727041
It's possible that it's a value that rounds to 0. Try this:

CAST(PC.PassCoilCount / Count(Distinct TR.Serial_Number) * 100 as DECIMAL(10,2)) as 'Chamber Coils Pass %'
0
 
LVL 11

Expert Comment

by:Simone B
ID: 39727045
If both columns are integers, which they appear to be as they are counts, then the result will be an integer.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39727056
Buttercup1 is correct.

integer / integer is 'integer math', and SQL ass-u-me's that the resulting value will also be an integer.

3 / 4 = 0, 5 / 2 = 2, you get the idea.

The way around this is to case one of the values as a data type that allows decimal values.

CAST(3 as decimal(5,2))/4 = .05, CAST(5 as decimal(5,2))/2 = 2.5
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 8

Author Comment

by:mani_sai
ID: 39727068
Thanks

CAST(PC.PassCoilCount / Count(Distinct TR.Serial_Number) * 100 as DECIMAL(10,2)) as 'Chamber Coils Pass %'

is returning 0.00

Any idea?
0
 
LVL 11

Accepted Solution

by:
Simone B earned 2000 total points
ID: 39727075
One other thing. Move your *100 like this:

CAST(PC.PassCoilCount  * 100 / Count(Distinct TR.Serial_Number) as DECIMAL(10,2)) as 'Chamber Coils Pass %'

For * and /, if no brackets specify order, they will be calculated left to right. Try these 3 scenarios, and choose the one that works best for you. I recommend the last one.

SELECT CAST(1/8*100 AS DECIMAL(10,2))

SELECT CAST(1*100/8 AS DECIMAL(10,2))

SELECT CAST(CAST(1 AS DECIMAL(10,2))*100/CAST(8 AS DECIMAL(10,2)) AS DECIMAL(10,2))

Open in new window

0
 
LVL 8

Author Comment

by:mani_sai
ID: 39727077
Here is my Full Code:

DECLARE @PassedCoils TABLE(EmployeeId Varchar(30) NOT NULL, EmployeeName Varchar(50) NOT NULL, PassCoilCount int NOT NULL);


INSERT INTO @PassedCoils
Select Distinct
      EI.NUM,
      EI.FIRSTNAME + ' ' + EI.LASTNAME ,
      Count(Distinct Serial_Number)
FROM
      AIO_Test_Results.dbo.Test_Results TR JOIN AIO_Test_Results.dbo.EmployeeInfo EI
ON
      TR.Op_ID = (EI.FIRSTNAME + ' ' + EI.LASTNAME)
Where
      (TR.Date_Time BETWEEN '2013-11-10 16:32:35.757' AND '2013-12-17 16:32:35.757') AND
      Len(TR.Serial_Number) >=15 AND (TR.System_ID LIKE '%Chamber%') AND (TR.Test_Result LIKE '%PASS%')
      AND EI.NUM IN (SELECT * FROM dbo.fn_SplitDelimiterString(@EmployeeList, ','))
Group By
      EI.FIRSTNAME + ' ' + EI.LASTNAME, Shift, EI.NUM
      
 

--Final results for Chamber
Select Distinct
      EI.FIRSTNAME + ' ' + EI.LASTNAME as 'Employee Name',
      EI.NUM as 'Employee Id',
      EI.SHIFT as 'Shift',
      Count(Distinct CONVERT(VARCHAR(10),Date_Time,111)) as 'Days Worked',
      Count(Distinct TR.Serial_Number) as 'Total Coils Processed',
      PC.PassCoilCount as 'Chamber Coils Good',      
      CAST(PC.PassCoilCount / Count(Distinct TR.Serial_Number) * 100 as DECIMAL(10,2)) as 'Chamber Coils Pass %' ,
      Count(Distinct TR.Serial_Number) / Count(Distinct CONVERT(VARCHAR(10),TR.Date_Time,111)) as 'Chamber Avg Coils/Day'
FROM
      AIO_Test_Results.dbo.Test_Results TR JOIN AIO_Test_Results.dbo.EmployeeInfo EI
      ON TR.Op_ID = EI.FIRSTNAME + ' ' + EI.LASTNAME
      JOIN @PassedCoils PC ON  PC.EmployeeName = TR.Op_ID
Where
      (Date_Time BETWEEN '2013-11-10 16:32:35.757' AND '2013-12-17 16:32:35.757') AND
      Len(Serial_Number) >=15 AND (System_ID LIKE '%Chamber%')
      AND EI.NUM IN (SELECT * FROM dbo.fn_SplitDelimiterString(@EmployeeList, ','))
Group By
      Shift, EI.FIRSTNAME + ' ' + EI.LASTNAME, EI.NUM, PC.PassCoilCount
Order By
      Shift, EI.FIRSTNAME + ' ' + EI.LASTNAME
0
 
LVL 11

Expert Comment

by:Simone B
ID: 39727093
See my last post.
0
 
LVL 8

Author Closing Comment

by:mani_sai
ID: 39727097
Thanks. It worked.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39727101
<Slight correction to the T-SQL in the first comment>
CAST one of the two amounts in the division, not the entire amount.
-- These returns the decimal places
SELECT CAST(3 as decimal(5,2))/4
SELECT 3/CAST(4 as decimal(5,2))

-- This doesn't
SELECT CAST(3/4 as decimal(5,2))

Open in new window

0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 39727106
Have you tried:

CAST(PC.PassCoilCount as DECIMAL(10,2)) / Count(Distinct TR.Serial_Number) * 100  as 'Chamber Coils Pass %'
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up 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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

800 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