Update query with multiple conditions

Here is some sample data from my Orders table
Orderid     PersonID         OrderMonth      OrderYear           OrderItem          OrderAmount    FreeShip
1                 2                          Jan                      2015                  Shoes                        300            N
2                 5                          Jan                      2015                  Socks                        200            N
3                 6                          Jan                      2015                  Shirts                        250            N

Here is some sample data from my Person Table  
PersonID            FirstName           LastName            Address1           City                    State        Zip
2                           Joe                      Smith            100 Main st                 Dallas                    Tx           77777
5                           Joe                      Jones            200 Main st                 Chicago            IL           60122
6                          John                     Doe            300 Smith st                 Houston              TX           77555

Here is some sample data from my OrderTotalHistory  
PersonId              OrderHistoryTotal
2                     300
6                      600

I am trying to write an update query that will update the freeship field to Y if all the following conditions are met
          State it is being shipped to is TX
         order amount is over 200
         OrderHistoryTotal is over 500

Here is the query to get the info
select orderid, a.personid, Ordermonth, OrderYear,  Orderitem, orderamount, FreeShipping, b.state, isnull(c.OrderHistoryTotal,0) as OrderHistoryTotal from [dbo].[Orders] a
inner join person b on a.personid = b.personid
left outer join OrderTotalHistory c on a.personid = c.personid


after the update the table should look like
Orderid     PersonID         OrderMonth      OrderYear           OrderItem          OrderAmount    FreeShip
1                2                         Jan                      2015                   Shoes                         300            N
2                5                        Jan                      2015                    Socks                        200            N
3                6                        Jan                      2015                    Shirts                        250            Y

(order id 1 is to TX and order amount is over 200 but the orderhistory total tied to personid 2 is under 500 so ‘N’)

(order id 2 is to IL so ‘N’)

(order id 3 is to TX and order amount is over 200 and  the orderhistory total tied to personid 6 is over 500 so ‘Y’)
johnnyg123Asked:
Who is Participating?
 
Snarf0001Commented:
IIF only came to SQL server as of 2012, so if you're on an earlier version, you're out of luck.
case is the more standard method.

If everything is already set to 'N' and you just need to update the proper 'Y' values, then you can use:

update a set FreeShip = 'Y'
from [dbo].[Orders] a
inner join person b on a.personid = b.personid
left outer join OrderTotalHistory c on a.personid = c.personid
where b.state = 'TX' and a.orderamount > 200 and c.OrderHistoryTotal > 500

Open in new window


If you need to run the update on everything regardless, then try:

update a set FreeShip = case when b.state = 'TX' and a.orderamount > 200 and c.OrderHistoryTotal > 500 then 'Y' else 'N' end
from [dbo].[Orders] a
inner join person b on a.personid = b.personid
left outer join OrderTotalHistory c on a.personid = c.personid

Open in new window

0
 
Mark BullockQA Engineer IIICommented:
The value clause for the FreeShip column is:
IIF (State='Tx' and OrderAmount>200 and OrderHistoryTotal>500, 'Y', 'N')

Open in new window

0
 
johnnyg123Author Commented:
Thanks Snarf0001

I am on SQL Server 2008 so thanks for the case method
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.