简体   繁体   中英

“Query does not include the specified expression as part of an aggregate function” when looking for top 3 values in group

This is a sample of some of my data:

样本数据

I am trying to get the top 3 values of L when grouping by ID_stop and Nn .

I tried the following SQL:

SELECT TOP 3 Sclerometrica_equotip.ID_stop, Sclerometrica_equotip.Nn, Sclerometrica_equotip.L
FROM Sclerometrica_equotip
GROUP BY Sclerometrica_equotip.ID_stop, Sclerometrica_equotip.Nn
ORDER BY Sclerometrica_equotip.ID_stop, Sclerometrica_equotip.Nn;

but got the following error:

Query does not include the specified expression as part of an aggregate function.

  1. I don't understand the error message. Can someone explain it to me?
  2. How can I accomplish what I am trying to do?

The error means that there are fields in your query which are not part of the grouping and also not aggregated together using an aggregate function.

When you group, you are condensing a set of records based on the grouped fields (in this case ID_stop and Nn ). That means that when before you had 100 records, now you have 3. But from which of those 100 records should the value of L be taken? You either have to include L in the fields to group by, or you have to specify some operation that will act on all the values of L . The possible operations you can use are:

  • first value -- First(L)
  • last value -- Last(L)
  • average -- Avg(L)
  • smallest -- Min(L)
  • largest -- Max(L)
  • sum -- Sum(L)
  • count of records in the group -- Count(*)
  • the standard deviation -- StDev(L) or StDevP(L)
  • variance -- Var(L) or VarP(L)

References

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM