Solved

Data Issue with Aggregate in Sql Update

Posted on 2013-11-11
2
233 Views
Last Modified: 2013-11-11
Hi ,

I am attaching some data for example.  The following is the query I am using.  I don't get the right result on the update value.  I have tried many different ways.  Any input would be greatly appreciated:

 BEGIN TRANSACTION
  UPDATE T1
  SET T1.SPCL_EARNS = T2.SUM_QTY
  FROM PS_PY_CAAT_CALC T1
  INNER JOIN (
 SELECT DISTINCT TMP.EMPLID,
  SUM(EARNS.SPCL_EARNS) AS SUM_QTY
  FROM PS_PY_CAAT_TMP TMP
  INNER JOIN PS_PY_SPCL_EARNS EARNS ON TMP.PAGE_NUM = EARNS.PAGE_NUM
   AND TMP.LINE_NUM = EARNS.LINE_NUM
   AND TMP.OFF_CYCLE = EARNS.OFF_CYCLE
   AND TMP.SEPCHK = EARNS.SEPCHK
   AND TMP.EMPL_RCD = EARNS.EMPL_RCD
   AND TMP.PAYGROUP = EARNS.PAYGROUP
   INNER JOIN PS_PY_CAAT_CALC
   AS CALC ON CALC.EMPLID = TMP.EMPLID
 WHERE EARNS.PAY_END_DT > CASE WHEN CALC.NC_MAX_PENS_EFFDT >= DATEADD(yy,DATEDIFF(yy,0,'2013-03-23' ),0) THEN CALC.NC_MAX_PENS_EFFDT ELSE DATEADD(yy,DATEDIFF(yy,0,'2013-03-23' ),0) END
   AND EARNS.PAY_END_DT <= '2013-03-23'
   AND EARNS.ERNCD_SPCL = 'CP3'
   AND EARNS.PAYGROUP = 'PTS'
  GROUP BY EARNS.PAGE_NUM,TMP.EMPLID ) AS T2
  ON T2.EMPLID = T1.EMPLID

Thank you,
Nigluc
0
Comment
Question by:Lucia
[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
  • 2
2 Comments
 
LVL 11

Accepted Solution

by:
Louis01 earned 500 total points
ID: 39638698
Try this?
UPDATE T1 
  SET T1.SPCL_EARNS = (SELECT SUM(EARNS.SPCL_EARNS) AS SUM_QTY
			  FROM PS_PY_CAAT_TMP TMP 
			  INNER JOIN PS_PY_SPCL_EARNS EARNS ON TMP.PAGE_NUM = EARNS.PAGE_NUM 
			   AND TMP.LINE_NUM = EARNS.LINE_NUM 
			   AND TMP.OFF_CYCLE = EARNS.OFF_CYCLE 
			   AND TMP.SEPCHK = EARNS.SEPCHK 
			   AND TMP.EMPL_RCD = EARNS.EMPL_RCD 
			   AND TMP.PAYGROUP = EARNS.PAYGROUP 
			   INNER JOIN PS_PY_CAAT_CALC 
			   AS CALC ON CALC.EMPLID = TMP.EMPLID 
			 WHERE EARNS.PAY_END_DT > CASE WHEN CALC.NC_MAX_PENS_EFFDT >= DATEADD(yy,DATEDIFF(yy,0,'2013-03-23' ),0) THEN CALC.NC_MAX_PENS_EFFDT ELSE DATEADD(yy,DATEDIFF(yy,0,'2013-03-23' ),0) END 
			   AND EARNS.PAY_END_DT <= '2013-03-23'
			   AND EARNS.ERNCD_SPCL = 'CP3' 
			   AND EARNS.PAYGROUP = 'PTS' 
			   AND TMP.EMPLID = T1.EMPLID
			  GROUP BY EARNS.PAGE_NUM,TMP.EMPLID)
  FROM PS_PY_CAAT_CALC T1

Open in new window

0
 
LVL 11

Expert Comment

by:Louis01
ID: 39640732
Thanks. By the way, I think it was the DISTINCT in your sub-select that caused the problems...
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

730 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