Solved

SQL scripting help

Posted on 2014-01-04
3
149 Views
Last Modified: 2014-01-06
I am an expert in a few things but SQL scripting is not one of them.  That being said, here is what I need help with.  There are three fields in a table that I need to manipulate.  These fields are EMP_ID, EARN_ID, and Amount.  There are multiple records in the table for each EMP_ID with different EARN_ID and Amount.  There is one record for each EMP_ID that has an EARN_ID equal to 'W2MEDREV'.  Each EMP_ID record will also have and EARN_ID record for their status (i.e. single, married, etc.).  I need to produce a script that will set the Amount field in the record for each EMP_ID that contains the 'W2MEDREV' field to the negative or inverse of the Amount in the single, married, etc. for the corresponding EMP_ID.

EMP_ID        EARN_ID             Amount
00001           W2MEDREV        100.00
00001           MARRIED           -100.00
00002           W2MEDREV         200.00
00002           SINGLE               -200.00

This is a sample of what I need to end up with.  Can someone help me?  

Thanks.......
0
Comment
Question by:rlewisc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 39756485
this should do it:
UPDATE t
  set Amount = - ( select sum(i.Amount)
               from yourtable i
             where i.emp_id = t.emp_id
                  and i.earn_id <> 'W2MEDREV'
          )
from yourtable t
where t.earn_id = 'W2MEDREV' 

Open in new window

0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 100 total points
ID: 39758515
I would suggest a small change in line 5 above
 and i.earn_id in ('MARRIED','SINGLE')

Open in new window

as we don't know what other values may be stored in the field earn_id
0
 

Author Closing Comment

by:rlewisc
ID: 39759236
I utilized both comments.  It worked perfectly.  Thanks for the help.....
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

726 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