Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Finding the percent SQL syntax

Posted on 2004-09-30
6
Medium Priority
?
429 Views
Last Modified: 2008-03-10
Hello,

My last columns are trying to find the percent of items that contain a L or an R, you get the idea.  However, the only percent im coming up with is 0, any suggestions?

Thank You

SELECT
      LINNBR As "Number",
      COD As "Code",
      Convert(varchar, DAT, 101) As "Date",
      Left(CAGE, 2) As House,
      DateDiff("d", DAT, VAL) As "Age",
      Sum( Case When NBR <> 0 Then 1 End) As "Total_Count",
      Avg(BW) AS "Weight",
      Avg([BW]/DateDiff("d", DAT, VAL)) As "ADG",
      Avg(BC) As AvgBC,
      Avg(FE) As AvgFe,
      Sum( Case When Stat1 = 'E' Then 1 End) As "Elites",
      Avg( Case When Stat1 = 'E' Then (BWT1) End) As "Avg_Weight_E",
      (Sum( Case When Defects1 like '%L%' Then 1 else 0 End)) As "%L",
      100*(Sum( Case When Def like '%T%' Then 1 else 0 End)/Sum( Case When NBR <> 0 Then 1 End)) As "%T",
      100*(Sum( Case When Def like '%K%' Then 1 else 0 End)/Sum( Case When NBR <> 0 Then 1 End)) As "%K",
      100*(Sum( Case When Def like '%R%' Then 1 else 0 End)/Sum( Case When NBR <> 0 Then 1 End)) As "%R",
      100*(Sum( Case When Def like '%V%' Then 1 else 0 End)/Sum( Case When NBR <> 0 Then 1 End)) As "%V",
      100*(Sum( Case When Def like '%D%' Then 1 else 0 End)/Sum( Case When NBR <> 0 Then 1 End)) As "%D",
      100*(Sum( Case When Def like '%S%' Then 1 else 0 End)/Sum( Case When NBR <> 0 Then 1 End)) As "%S"
FROM
      PSBRLEVLP
WHERE
      VAL <> ''
GROUP BY
      LINNBR,
      COD,
      DAT,
      Left(CAGE, 2),
      DateDiff("d", DAT, VAL)
0
Comment
Question by:Navicerts
[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
6 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12191741
You probably have some nulls, which sets it all null. Try substituting:

NULLIF(NBR, 0)

for

NBR
0
 
LVL 7

Author Comment

by:Navicerts
ID: 12191752
NBR is a keyed field that wont allow Null's
0
 
LVL 7

Author Comment

by:Navicerts
ID: 12191767
sorry,  you did mean i probally have null's in the NBR field right?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Accepted Solution

by:
BillAn1 earned 2000 total points
ID: 12191916
the SUM returns int, so SLQ treats the calculation as integers. You are dividng numerator by denom then multiplying by 100
e.g. 100 * ( 1234 / 1500) will give 100 * ( 0) = 0
try casting as floats (you can get away with only the numerator)

.....
   100.0 *( cast( Sum( Case When Def like '%T%' Then 1 else 0 End) as float) /Sum( Case When NBR <> 0 Then 1 End)) As "%T",
.....
0
 
LVL 34

Expert Comment

by:arbert
ID: 12191950
Possibly SQL Server's implicit conversion on the data type--try this:

100*(Sum( Case When Def like '%T%' Then 1 else 0 End)/Sum( Case When NBR <> 0 Then 1.00  End)) As "%T"
0
 
LVL 7

Author Comment

by:Navicerts
ID: 12193579
Cast worked great, thank you all for your input
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

618 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