If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Adding Info

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-24-12, 08:18
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
Adding Info

Hi guys,

So this is my question. I have a table where I am adding data, based on two other tables. The problem is that when I make a normal join I get an error saying that the subquery returned more than 1 value. This is because in the list I have there are some companies that are repeated several times so the code that returns this error is this one

Code:
UPDATE CC_SPLIT SET CC_ACCOUNT_AMOUNT =
(SELECT AMOUNT FROM EXP_DATA
WHERE PERIOD = '1111'
AND EXP_DATA.ACCOUNT = CC_SPLIT.CC_ACCOUNT
AND EXP_DATA.ACCOUNT = CC_SPLIT.CC_COMPANY)
So I came with this as a solution but the problem is that the following only insert data in those companies that are repeated and have different ammounts


Code:
UPDATE CC_SPLIT SET CC_ACCOUNT_AMOUNT =
(SELECT SUM (AMOUNT) FROM EXP_DATA
WHERE PERIOD = '1111'
AND EXP_DATA.ACCOUNT = CC_SPLIT.CC_ACCOUNT
AND EXP_DATA.ACCOUNT = CC_SPLIT.CC_COMPANY)
So my question is how can I add into CC_ACCOUNT_AMOUNT the other data left?

Any ideas?
Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 01-24-12, 09:05
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
Is something like this possible?

Code:
IF CC_ACCOUNT_AMOUNT_NOV11 IS NOT NULL 

BEGIN

UPDATE CC_SPLIT SET CC_ACCOUNT_AMOUNT =
(SELECT AMOUNT FROM EXP_DATA
WHERE PERIOD = '1111'
AND EXP_DATA.ACCOUNT = CC_SPLIT.CC_ACCOUNT
AND EXP_DATA.ACCOUNT = CC_SPLIT.CC_COMPANY)

END

??? Any ideas how to fix this?
Reply With Quote
  #3 (permalink)  
Old 01-24-12, 09:29
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,280
I think there is a bug in your code
AND EXP_DATA.ACCOUNT = CC_SPLIT.CC_COMPANY)

Try this:
Code:
UPDATE U 
SET U.CC_ACCOUNT_AMOUNT = T.AMOUNT
FROM CC_SPLIT AS U
	INNER JOIN (SELECT SUM(AMOUNT) as Amount, 
			ACCOUNT, CC_COMPANY
		FROM EXP_DATA 
		WHERE PERIOD = '1111'
		GROUP BY ACCOUNT, CC_COMPANY
		) AS T ON
		T.ACCOUNT = U.CC_ACCOUNT
		AND T.CC_COMPANY = U.CC_COMPANY
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #4 (permalink)  
Old 01-24-12, 10:35
buzmay buzmay is offline
Registered User
 
Join Date: Jan 2012
Posts: 44
Thanks for the help Wim I really appreciate it.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On