Solved

SQL scripting help

Posted on 2014-01-04
3
146 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
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 48

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

861 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