0
SELECT HCIS,ServiceDate,PrimeInsMnemonic,TIN,Amount,SUM(Amount) AS Total
FROM dbo.PbrChargeTransactions
WHERE HCIS = 'ASL'
AND ServiceDate Between'01/01/16' and '12/31/16'
AND PrimeInsMnemonic = 'MED'
AND TIN in ('460224598','46-0224598')
GROUP By HCIS
ORDER BY SUM(Amount) 

Error I Get:

Msg 8120, Level 16, State 1, Line 1 Column 'dbo.PbrChargeTransactions.ServiceDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

I was hoping someone would be kind enough to explain to me why this is not working?

Thank you in advance for any help!

anothermh
  • 9,815
  • 3
  • 33
  • 52
SkysLastChance
  • 211
  • 1
  • 4
  • 14
  • Possible duplicate of [GROUP BY without aggregate function](http://stackoverflow.com/questions/20074562/group-by-without-aggregate-function) – Joe C Jan 19 '17 at 21:25

3 Answers3

1

I think the message is pretty clear. All unaggregated columns should be in the GROUP BY. So, remove the columns you aren't using:

SELECT HCIS, SUM(Amount) AS Total
FROM dbo.PbrChargeTransactions
WHERE HCIS = 'ASL' AND
      ServiceDate Between '2016-01-01' AND '2016-12-31' AND
      PrimeInsMnemonic = 'MED' AND
      TIN in ('460224598','46-0224598')
GROUP By HCIS
ORDER BY SUM(Amount) 
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

You should include every column in Select statement to repeat in Group By if they are not in aggregate function

SELECT HCIS,ServiceDate,PrimeInsMnemonic,TIN,Amount,SUM(Amount) AS Total FROM dbo.PbrChargeTransactions WHERE HCIS = 'ASL' AND ServiceDate Between'01/01/16' and '12/31/16' AND PrimeInsMnemonic = 'MED' AND TIN in ('460224598','46-0224598') GROUP By HCIS,ServiceDate,PrimeInsMnemonic,TIN,Amount ORDER BY SUM(Amount)

Quan Le
  • 11
  • 3
0

it is as they say, you have to enter in the group by columns in the select.

SELECT HCIS,ServiceDate,PrimeInsMnemonic,TIN,Amount,SUM(Amount) AS Total
FROM dbo.PbrChargeTransactions
WHERE HCIS = 'ASL'
AND ServiceDate Between'01/01/16' and '12/31/16'
AND PrimeInsMnemonic = 'MED'
AND TIN in ('460224598','46-0224598')
GROUP By HCIS,ServiceDate,PrimeInsMnemonic,TIN,Amount
ORDER BY SUM(Amount)
mircoso
  • 11
  • 3