Solved

update query Sql server 2005

Posted on 2011-09-06
3
188 Views
Last Modified: 2012-06-21
I have the following table named SalesData

Shop  CurrSales  PriorSales    Sales Diff   SalesMiss  SalesNeg
1201   0                20000          -20000                
1202   10000        0                  10000
1203    25000      24000           1000
1204    32000      32200            -200

I am trying to write an update query that for each shop will do the following

      update SalesMiss with a Y if either    CurrSales or PriorSales    = 0.  Otherwise, should be set to  N
     
      update SalesNeg with A Y if  salesdiff is < 0.  Otherwise should be set to N  



Here is the result I am looking for


Shop  CurrSales  PriorSales    Sales Diff   SalesMiss  SalesNeg
1201   0                20000          -20000           Y              Y
1202   10000        0                  10000            Y               N    
1203    25000      24000           1000              N               N
1204    32000      32200            -200              N               Y



0
Comment
Question by:johnnyg123
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 500 total points
Comment Utility
Update SalesData
SET SalesMiss = CASE WHEN CurrSales = 0 OR PriorSales = 0 THEN 'Y' ELSE 'N' END,
SalesNeg = CASE WHEN SalesDiff < 0 THEN 'Y' ELSE 'N' END
0
 
LVL 16

Expert Comment

by:brad2575
Comment Utility
update sales data
set SalesMiss = CASE when currsales = 0 or priorsales = 0 then 'Y' ELSE 'N' END
SET SalesNeg = CASE WHEN salesdiff <0 THEN 'Y' ELSE 'N' END
0
 
LVL 16

Expert Comment

by:brad2575
Comment Utility
lludden beat me by a little but same code :)
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

Title # Comments Views Activity
Remove extra commas from beginning of string 5 43
T-SQL To Select PiD In Two Hobbies 3 39
SQL Question 1 28
sql help 5 48
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

13 Experts available now in Live!

Get 1:1 Help Now