Solved

SQL Server 2008 - Query Calculation Issue

Posted on 2013-12-18
10
323 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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 500 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 (Access MVP)
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

707 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