Solved

SQL Update Where Exists If Then Else

Posted on 2010-08-17
3
330 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

914 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

20 Experts available now in Live!

Get 1:1 Help Now