473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question on scripting

I have a question on coding, just want to see if there is a better way
to do this.

Here is some sample data combinations that would come from 2 different
tables.

Item Method Price Cost Percentage
A-1 L 100 50 10
A-2 D 110 55 15
A-3 U 90 40 65

Ok, here is what I want to do.

If L then 100 x (1+(10/100))
if D then 110 x (1-(15/100))
if U then 40 x (1+(65/100))

To produce 1 number per item.

I can do this obviously using multiple When Then Else statements.
Like

Case when method='L'
then Price * (1+(percentage/100))
else case when method='D'
then Price * (1-(percentage/100))
else Cost * (1+(percentage/100))
end
end
However I was thinking there might just be a better way to do this that
I am not familar with, plus I could easily have 8-10 of these methods
that I need to code.
Any ideas on a better way?
Thanks in advance.

Oct 25 '06 #1
2 1228
On 25 Oct 2006 11:30:11 -0700, mike wrote:
>I have a question on coding, just want to see if there is a better way
to do this.

Here is some sample data combinations that would come from 2 different
tables.

Item Method Price Cost Percentage
A-1 L 100 50 10
A-2 D 110 55 15
A-3 U 90 40 65

Ok, here is what I want to do.

If L then 100 x (1+(10/100))
if D then 110 x (1-(15/100))
if U then 40 x (1+(65/100))

To produce 1 number per item.

I can do this obviously using multiple When Then Else statements.
Like

Case when method='L'
then Price * (1+(percentage/100))
else case when method='D'
then Price * (1-(percentage/100))
else Cost * (1+(percentage/100))
end
end
However I was thinking there might just be a better way to do this that
I am not familar with, plus I could easily have 8-10 of these methods
that I need to code.
Any ideas on a better way?
Thanks in advance.
Hi Mike,

The obvious simplification is to use one case with multiple WHEN clauses
instead of nesting the CASE expressions, like this:

Case when method='L'
then Price * (1+(percentage/100))
when method='D'
then Price * (1-(percentage/100))
else Cost * (1+(percentage/100))
end

Another possible way to simplify this, depending on the nature of the
other methods, would be to use CASE expressions for the variable parts
of the formula. All formula's above start with either Price or Cost,
then multiply this with either the sum or the difference of 1 and
percentage/100. You could rewrite this as

CASE WHEN method IN ('L', 'D')
THEN Price
ELSE Cost
END * (1 + ((percentage / 100)
* CASE WHEN method = 'D' THEN -1 ELSE 1 END))
--
Hugo Kornelis, SQL Server MVP
Oct 25 '06 #2
On 25 Oct 2006 11:30:11 -0700, "mike" <mi********@gmail.comwrote:
>I can do this obviously using multiple When Then Else statements.
Like

Case when method='L'
then Price * (1+(percentage/100))
else case when method='D'
then Price * (1-(percentage/100))
else Cost * (1+(percentage/100))
end
end

However I was thinking there might just be a better way to do this that
I am not familar with, plus I could easily have 8-10 of these methods
that I need to code.
You have overly complicated this CASE expression. It does NOT require
nesting, and either format will handle ten (or fourty) alternative
calculations without becoming awkward.

CASE WHEN method='L'
THEN Price * (1+(percentage/100))
WHEN method='D'
THEN Price * (1-(percentage/100))
WHEN method='U'
THEN Cost * (1+(percentage/100))
END

or:

CASE method
WHEN 'L' THEN Price * (1+(percentage/100))
WHEN 'D' THEN Price * (1-(percentage/100))
WHEN 'U' THEN Cost * (1+(percentage/100))
END

Roy Harvey
Beacon Falls, CT
Oct 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Adonis | last post by:
What I do not understand, or not clear to me I should say, is how can some people regard Python as a scripting language? In particular the JAVA crowd. Unless my understanding is off, and from what...
2
by: John Salerno | last post by:
Ok, like I mentioned before, I'm learning C# for fun. I'm interested in learning Python sort of as a "supplement" (by that, I mean a language with scripting capabilities that can do things maybe...
6
by: David | last post by:
I am running into situtations where confining all forms to just one window (instance of broswer) is becoming overly restrictive. Does ASP.NET provide ways to have multiple windows to interact with...
8
by: Jolly Student | last post by:
Dear Colleagues: I am a systems guy who has been working with hardware, networking and basic scripting for ten years or so. I have been given the opportunity to attend ..Net classes of all...
1
by: tigozilla | last post by:
I am developing a website with ASP.NET /C#. The items will render automatically based on their "catID". So that the URL will look similar to the example: http://www.domain.com/index.aspx?catID=1...
6
by: Joel Byrd | last post by:
I want a website that works in the following way: It has a main content div, and of course menu items. When you click a menu item, it should use AJAX to change the main content div to the content...
21
by: ManningFan | last post by:
I need to use late binding in a project because it's company standard to not include references which aren't MS defaults, so I can't add the scripting runtime. I need to be able to search...
7
by: Brave | last post by:
I have two questions about pulldown menus on forms. 1: Can I have the options of one pulldown menu be dictated by the choice from another pulldown menu (example beow)? 2: Can I have a form be...
0
by: Bill P | last post by:
I'm having a heck of time finding out how to code a some 'set' commands from vbs: objFSO = CreateObject("Scripting.FileSystemObject") objOut = CreateObject("Scripting.FileSystemObject") objOut2...
4
by: Fawzib Rojas | last post by:
I have an app with a WebBrowser component and I'm setting the the ObjectForScripting component so the object can be used from javascript. The object has a Scripting.Dictionary property. My...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.