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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window

0
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
johnnyg123Author Commented:
Thanks Snarf0001

I am on SQL Server 2008 so thanks for the case method
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.