Solved

SQL - Math Question

Posted on 2014-01-09
7
337 Views
Last Modified: 2014-01-09
I have an old document with the following syntax. This Query will Set a column to "YES" in my Master Hex table if 85% of the Households are included from the Test table.

I would like to change the query to give me summery of all Hex's above 75% and not update my Master Hex Table.
Old Code
SET ARITHABORT OFF
SET ANSI_WARNINGS OFF

update dbo.Master_Hex
set dbo.Master_Hex.[EORN_served] = 'Yes'
from dbo.test, dbo.Master_Hex
where dbo.test.[GISHEXID] = dbo.Master_Hex.[GISHEXID]
and cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) >=0.85
and test.Total_HH is not null
and dbo.Master_Hex.Total_Households is not null 

Open in new window


New Code would be a select Query - I'm Doing this wrong (I'm a novice level in SQL)
select dbo.Master_Hex.GISHEXID, cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) >=0.75
from dbo.test, dbo.Master_Hex
where dbo.test.[GISHEXID] = dbo.Master_Hex.[GISHEXID]
and test.Total_HH is not null
and dbo.Master_Hex.Total_Households is not null

Open in new window

0
Comment
Question by:PtboGiser
[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
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39768156
you where close:
select dbo.Master_Hex.GISHEXID, cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) percentag
from dbo.test, dbo.Master_Hex
where dbo.test.[GISHEXID] = dbo.Master_Hex.[GISHEXID]
and test.Total_HH is not null
and dbo.Master_Hex.Total_Households is not null
and cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) >=0.75 

Open in new window

0
 

Author Comment

by:PtboGiser
ID: 39768160
Msg 8134, Level 16, State 1, Line 3
Divide by zero error encountered.
 I tried that code earlier. Then switched it.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39768173
well, that error :)
select dbo.Master_Hex.GISHEXID, cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) percentag
from dbo.test, dbo.Master_Hex
where dbo.test.[GISHEXID] = dbo.Master_Hex.[GISHEXID]
and test.Total_HH is not null
and dbo.Master_Hex.Total_Households <> 0
and cast((dbo.test.Total_HH)as 
float)/cast((dbo.Master_Hex.Total_Households)as float) >=0.75 
                                            

Open in new window

0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39768180
btw, you may want to read up this article:
http://www.experts-exchange.com/Database/Miscellaneous/A_11135-Why-should-I-use-aliases-in-my-queries.html

your query would become:
select mh.GISHEXID
, cast( t.Total_HH as 
float) / cast( mh.Total_Households as float) percentag
from dbo.test t
join dbo.Master_Hex mh
  on t.[GISHEXID] = mh.[GISHEXID]
and t.Total_HH is not null
and mh.Total_Households <> 0
and cast( t.Total_HH as float)/cast( mh.Total_Households as float) >=0.75 

Open in new window

0
 

Author Comment

by:PtboGiser
ID: 39768195
Thanks
Aliases i'm split on. Since we're  lower lever programmers at the point and our queries are usually pretty simple we have not used them much as it seems to cause more confusion then what's it worth at this point. I totally see there benefit though and have used them on occasion
Thanks


Can I change the Percetage column to a % type?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39768218
there is no "%" type as such, but you can diplay accordingly:

cast( round( 100.00 * ( a / b ) , 1 )  as varchar(20)) + '%'

this should do
0
 

Author Closing Comment

by:PtboGiser
ID: 39768244
Thank you kind sir! Have a good day.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

738 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