We help IT Professionals succeed at work.

update amount field in a table with sum of amount fields from a linked server.

billkrieger
billkrieger asked
on
273 Views
Last Modified: 2008-02-01
I created the Following table and inserted the following DISTINCT records (There are multiple line items for each invoice but I only wanted one record in the table I created) from a linked JDE database in SQL Server, which works fine:

CREATE TABLE [sysop].TMP_APAudit (
      BatchNo varchar (25),
      BatchDate datetime,
      InvNo varchar (25),
      InvDate datetime,
      DocType varchar (2),
      InvAmt varchar (14),
      BranchPlant varchar (20),
      SupplierNo varchar (25),
      )

INSERT INTO TMP_APAudit
    SELECT a.RPICU, a.RPDICJ, a.RPVINV, a.RPDIVJ, a.RPDCT, '', a.RPMCU,a.RPAN8 FROM
        OPENQUERY(MMM_JDEVIEW, 'Select DISTINCT RPICU, RPDICJ, RPVINV, RPDIVJ, RPDCT,
        RPMCU,RPAN8 FROM TESTDTA.Docimag4  ') a
              LEFT OUTER JOIN ae_dt3 b ON RTRIM(LTRIM(a.RPICU))  = RTRIM(LTRIM(b.field5))
                       AND RTRIM(LTRIM(a.RPVINV)) = RTRIM(LTRIM(b.field9))
                       AND RTRIM(LTRIM(a.RPAn8)) = RTRIM(LTRIM(b.field2))
                              WHERE b.field5 is NULL AND a.RPDCT IN(SELECT DocTypeID FROM APAudit_DocTypes)
                             AND RPDICJ >= (SELECT Startdate FROM APAudit_StartDate)

How do I update the InvAmt field in the table I created with the sum of the RPAC field in the docimag4 table where the RPICU = BatchNo and RPVINV = InvNo And RPAn8 = SupplierNo

Thanks in Advance for you help.
                               





            
            
            
            
             
                  
Comment
Watch Question

Commented:
Seems like you might be able to do it with one OPENQUERY, but I don't know your data well enough to advance that solution.  Try this:

INSERT INTO TMP_APAudit
    SELECT a.RPICU, a.RPDICJ, a.RPVINV, a.RPDIVJ, a.RPDCT, b.total, a.RPMCU,a.RPAN8 FROM
        OPENQUERY(MMM_JDEVIEW, 'Select DISTINCT RPICU, RPDICJ, RPVINV, RPDIVJ, RPDCT,
        RPMCU,RPAN8 FROM TESTDTA.Docimag4  ') a
              LEFT OUTER JOIN ae_dt3 b ON RTRIM(LTRIM(a.RPICU))  = RTRIM(LTRIM(b.field5))
                       AND RTRIM(LTRIM(a.RPVINV)) = RTRIM(LTRIM(b.field9))
                       AND RTRIM(LTRIM(a.RPAn8)) = RTRIM(LTRIM(b.field2))
--this is what I added
LEFT OUTER JOIN
 OPENQUERY(MMM_JDEVIEW, 'Select  RPICU, RPVINV, RPAN8, SUM(RPAC) total FROM TESTDTA.Docimag4  group by RPICU, RPVINV, RPAN8') b
on a.RPICU=b.RPICU and a.RPVINV=b.RPVINV and a.RPAN8=b.RPAN8
--
                              WHERE b.field5 is NULL AND a.RPDCT IN(SELECT DocTypeID FROM APAudit_DocTypes)
                             AND RPDICJ >= (SELECT Startdate FROM APAudit_StartDate)

Author

Commented:
The statement is running now, but takes a long time. It now has doubled with the 2nd open query.

How would you do it with one open query. I played around with it but, kept getting an error on the group by.

Also, is it possible to put the where clauses inside of the open query to reduce the # of transactions returned.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.