0

Can anyone please advise on how to go about writing a SQL query to include the sum for multiple fields across multiple rows by group. I'm using the below query, but it keeps saying that the fields "in the select line are invalid because it is not contained in either an aggregate function or the GROUP BY clause."

    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

Any advice would be greatly appreciated. Thanks!

4

3 回答 3

1

BilledAmt 不在 group by 子句中。你必须把它放在那里,或者用总和、平均值或其他函数聚合它。

于 2013-07-11T18:45:35.580 回答
1

BilledAmt 不在聚合查询中。当您使用 group by 时,您只能选择聚合或 group by 子句中的任何字段/

于 2013-07-11T18:46:21.260 回答
1

根据您真正想要的,您有两种选择:

选项 #1:BilledAmtSELECT

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:BilledAmtGROUP 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
于 2013-07-11T18:48:06.233 回答