任何人都可以建议如何编写SQL查询以包含多个行中多个字段的总和。我正在使用下面的查询,但它一直说“在选择行中的字段无效,因为它不包含在聚合函数或GROUP BY子句中。”
Select ClaimId,InternalICN,BilledAmt,
Sum(PayAmt) as TotPayAmt,Sum(COBAmt) as TotCOBAmt,Sum(PrePayAmt) as
TotPrePayAmt
from CAIDEnc.IntEncTracking.EncounterList
where BypassFlag = 0 and
BypassReason = 0
group by ClaimId, InternalICN
任何建议都将不胜感激。谢谢!
答案 0 :(得分:1)
BilledAmt不在group by子句中。你必须把它放在那里,或用总和,平均值或其他函数汇总它。
答案 1 :(得分:1)
BilledAmt不在聚合查询中。当您使用group by时,您只能选择聚合或group by /
组中的任何字段答案 2 :(得分:1)
根据您的真实需要,您有两种选择:
选项#1:从BilledAmt
SELECT
Select ClaimId,InternalICN,
Sum(PayAmt) as TotPayAmt,Sum(COBAmt) as TotCOBAmt,Sum(PrePayAmt) as
TotPrePayAmt
from CAIDEnc.IntEncTracking.EncounterList
where BypassFlag = 0 and
BypassReason = 0
group by ClaimId, InternalICN
或
选项#2:将BilledAmt
纳入GROUP BY
Select ClaimId,InternalICN,BilledAmt,
Sum(PayAmt) as TotPayAmt,Sum(COBAmt) as TotCOBAmt,Sum(PrePayAmt) as
TotPrePayAmt
from CAIDEnc.IntEncTracking.EncounterList
where BypassFlag = 0 and
BypassReason = 0
group by ClaimId, InternalICN,BilledAmt