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!
BilledAmt is not in the group by clause. You must put it there, or aggregate it with a sum, average or other function.
BilledAmt is not in a aggregate query. When you use group by you can only select aggregates or any field in the group by clause/
Depending on what you really want, you have two options:
OPTION #1: Remove the BilledAmt
from the 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
or
OPTION #2: Include the BilledAmt
in the 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.