Solved

query ...

Posted on 2006-07-21
5
223 Views
Last Modified: 2012-06-27
Hello
This is a stripped down example of a query

Select
(case when (iSite_Cart.RuleID = 0 or iSite_Cart.intQty < iSite_Rules.db1) then sum(iSite_Cart.intQty * iSite_Cart.Cost)
else                        
((case when iSite_Rules.RuleType = 0 then sum((iSite_Cart.intQty * iSite_Cart.Cost) - iSite_Rules.db2)
when iSite_Rules.RuleType = 1 then      
sum((iSite_Cart.intQty * iSite_Cart.Cost) - (iSite_Cart.intQty * iSite_Cart.Cost) * (iSite_Rules.db2/100))
when iSite_Rules.RuleType = 2 then
sum((iSite_Cart.intQty * iSite_Cart.Cost) - (iSite_Rules.db2 * iSite_Cart.Cost)) end))
end)
as Total

iSite_Cart.RuleID is what rule is applied - rule 0 has zero value
iSite_Cart.intQty is the quantity in cart
iSite_Cart.Cost is the dollar amount
iSite_Rules.RuleType is the type of rule (0 = dollar amount, 1 = percent, 2 = subtract)
iSite_Rules.db1 is the value the quantity is checked against to see if rule will apply
iSite_Rules.db2 is the value to subtract or multiply percent

My question is how do I code when for ex
a person has an item that has a rule of "buy 5 get 1 free"
in this case db1 would be 5 and db2 would be 1
so when they buy 10 they would expect 2 free, but my code just gives the 1 free
I need to look at multiples I guess but don't know how ...

How to?

0
Comment
Question by:dgrafx
  • 3
  • 2
5 Comments
 
LVL 26

Expert Comment

by:DireOrbAnt
Comment Utility
Try:
FLOOR(iSite_Cart.intQty / iSite_Rules.db1)

As in:
0
 
LVL 26

Accepted Solution

by:
DireOrbAnt earned 500 total points
Comment Utility
As in:
sum((iSite_Cart.intQty * iSite_Cart.Cost) - (iSite_Rules.db2 * iSite_Cart.Cost) * FLOOR(iSite_Cart.intQty / iSite_Rules.db1))

The other issue with your code was that it was always giving 1 free, even if less than 5 were purchased. This should work at all times :)

You could also simplify with:
sum( (iSite_Cart.intQty - iSite_Rules.db2 * FLOOR(iSite_Cart.intQty / iSite_Rules.db1)) * iSite_Cart.Cost )

0
 
LVL 24

Author Comment

by:dgrafx
Comment Utility
thanks - seems to work good
what would be the syntax for
(case when iSite_Rules.RuleType = 0 then sum((iSite_Cart.intQty * iSite_Cart.Cost) - iSite_Rules.db2)
if I ever wanted to do this for type 0 which is subtracting an amount for every x number purchased
0
 
LVL 26

Expert Comment

by:DireOrbAnt
Comment Utility
To remove $db2 for evey db1 purchased as in, buy 5 and get $20 off:
case when iSite_Rules.RuleType = 0 then sum( iSite_Cart.intQty * iSite_Cart.Cost - iSite_Rules.db2 * FLOOR(iSite_Cart.intQty / iSite_Rules.db1) )
0
 
LVL 24

Author Comment

by:dgrafx
Comment Utility
thanks a lot - works good
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now