Solved

SQL Update Where Exists If Then Else

Posted on 2010-08-17
3
329 Views
Last Modified: 2012-05-10
I am trying to update contents of a field in a table based on the results of a subquery.  I have been able to get the statement to work for an individual row, but I cannot figure out the syntax to run this update against all the rows in the table.

Basically the query looks to see if there was activity in another table for a given year and month and if sees activity update the base table with that count, and if it finds no activity it should update the field with a zero (0)

******** Single part version THIS WORKS BUT I NEED IT TO RUN AGAINS ALL PARTS IN THE BASE TABLE*********

If(EXISTS
(
select UnitsSold
from
(SELECT  partno,warehouse,YEAR(EntryDate) AS 'WOYear', MONTH(EntryDate) AS 'WOMonth', SUM(qty) AS 'UnitsSold',
count(wono) as "Workorders"
FROM woparts
GROUP BY partno,warehouse,YEAR(EntryDate), MONTH(EntryDate)
) C
where C.partno = '0040596'
and C.warehouse = 'inactive'
and C.WOMonth = month(dateadd(m,-3,getdate()))
and C.WOYear = year(dateadd(m,-3,getdate()))
))
      BEGIN
        UPDATE ab_partsdemand
            set demand4=
                  (
                  select UnitsSold
                  from
                  (SELECT  partno,warehouse,YEAR(EntryDate) AS 'WOYear', MONTH(EntryDate) AS 'WOMonth', SUM(qty) AS 'UnitsSold',
                  count(wono) as "Workorders"
                  FROM woparts
                  GROUP BY partno,warehouse,YEAR(EntryDate), MONTH(EntryDate)
                  ) C
                  where C.partno = '0040596'
                  and C.warehouse = 'inactive'
                  and C.WOMonth = month(dateadd(m,-3,getdate()))
                  and C.WOYear = year(dateadd(m,-3,getdate()))
                  )
                  where partno = '0040596'
                  and warehouse = 'inactive'
      END
else
        UPDATE ab_partsdemand
            set demand4=0
            where partno = '0040596'
            and warehouse = 'inactive'
0
Comment
Question by:josephpmo
3 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 33459061
this subquery:
(
                  select UnitsSold
                  from
                  (SELECT  partno,warehouse,YEAR(EntryDate) AS 'WOYear', MONTH(EntryDate) AS 'WOMonth', SUM(qty) AS 'UnitsSold',
                  count(wono) as "Workorders"
                  FROM woparts
                  GROUP BY partno,warehouse,YEAR(EntryDate), MONTH(EntryDate)
                  ) C
                  where C.partno = '0040596'
                  and C.warehouse = 'inactive'
                  and C.WOMonth = month(dateadd(m,-3,getdate()))
                  and C.WOYear = year(dateadd(m,-3,getdate()))
)

has to return a single value. So my question is: what to do if it returns 2 or more values ? I figure the result you want in that case is the SUM, so put this:

       UPDATE ab_partsdemand
            set demand4=
                  (
                  select SUM(UnitsSold ) -- <<<<<<<<<<<<<<<<  This is the only line I changed
                  from
                  (SELECT  partno,warehouse,YEAR(EntryDate) AS 'WOYear', MONTH(EntryDate) AS 'WOMonth', SUM(qty) AS 'UnitsSold',
                  count(wono) as "Workorders"
                  FROM woparts
                  GROUP BY partno,warehouse,YEAR(EntryDate), MONTH(EntryDate)
                  ) C
                  where C.partno = '0040596'
                  and C.warehouse = 'inactive'
                  and C.WOMonth = month(dateadd(m,-3,getdate()))
                  and C.WOYear = year(dateadd(m,-3,getdate()))
                  )
                  where partno = '0040596'
                  and warehouse = 'inactive'

0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33459106
You should optimize the YEAR(EntryDate) and Month(Entry) expressions to use the index by working out the date range, but if the partno+warehouse is selective enough, it is probably not an issue.
        UPDATE ab_partsdemand

            set demand4=

                  ISNULL((

                  (SELECT  SUM(qty)

                  FROM woparts

                  WHERE woparts.partno = ab_partsdemand.partno

                  AND woparts.warehouse = 'inactive'

                  AND YEAR(EntryDate) = year(dateadd(m,-3,getdate()))

                  AND MONTH(EntryDate) = month(dateadd(m,-3,getdate()))

                  ),0)

                  where warehouse = 'inactive'

Open in new window

0
 

Author Closing Comment

by:josephpmo
ID: 33459321
CyberKiwi,

Thanks a very elegant solution.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

707 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

11 Experts available now in Live!

Get 1:1 Help Now