Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何添加两列但保持行不相等_Sql_Ms Access - Fatal编程技术网

Sql 如何添加两列但保持行不相等

Sql 如何添加两列但保持行不相等,sql,ms-access,Sql,Ms Access,如果使用UPDATE语句在多个列Account、Account2中具有相同的值,如何添加两列amount、amount2。我想保留所有行,即使是帐户2。另外,我想把它们分为两列,只有帐户和金额。我正在使用MS Access sql视图 例如:我只想在Account、Account 2列匹配时添加Amount和Amount 2列 我希望结果端只有两列,账户和合计的金额 Account Amount Account2 Amount2 1234576 4 1234576 4 4444

如果使用UPDATE语句在多个列Account、Account2中具有相同的值,如何添加两列amount、amount2。我想保留所有行,即使是帐户2。另外,我想把它们分为两列,只有帐户和金额。我正在使用MS Access sql视图

例如:我只想在Account、Account 2列匹配时添加Amount和Amount 2列

我希望结果端只有两列,账户和合计的金额

Account Amount Account2 Amount2
1234576  4      1234576    4
4444444  10     4564888    11
456789   2      456789      2
000000   1      1111111     6 
这里有一个使用iif语句的选项:

select account, 
       amount + iif(account = account2, amount2, 0) as amount
from yourtable

如何确定金额值?当账号相等时,将其相加。如果它们不相等,就保留它们。基本上,任何重复的帐号都应该添加,并且仍然保留其余的。update tablename set Amount2=Amount where account=Account2?Hi Jarlh,它可以工作,但只添加相等的值,不相等的行不会显示在更新的表上。如果您告诉我们有关此要求的上下文,可能会有所帮助。老实说,这听起来像是一个面试或家庭作业问题。提供操作前后的数据示例也可能有帮助,也就是说,显示它确实是什么样子,以及它应该是什么样子。如果你只得到了一半答案,那么另一半必须是相反的。加入他们,加入工会谢谢你,吉弗洛。如果我想使用UPDATE,这会起作用吗?嗯,从未尝试使用UNION更新。您可能认为需要更新到辅助表以保留输入表。
(SELECT t1.Account as Acct, Sum(t1.Amount) as Amt1, first(t1.Account2) as Acct2, sum(t1.amount) as Amt2 
FROM test as t1
WHERE t1.Amount=t1.Amount2
GROUP BY t1.Account
ORDER BY t1.Account)

UNION

(SELECT t2.Account as Acct, sum(t2.Amount) as Amt1, first(t2.Account2) as Acct2, sum(t2.amount2) as Amt2
FROM test as t2
WHERE t2.Amount <> t2.Amount2
GROUP BY t2.account
ORDER BY t2.account)