Keep Column which is not in aggregate function in group by statement

user3241778

I would like to modify an existing select so it outputs the row with the MAX(amount) per customer and all of its values. At the moment the result is like this.

   AMOUNT CUSTOMERID     ITEMID USERNAME                        USERID SUMMARYDAY

    60    198507        205 luk                                   12 03.10.18
   300    198526        207 max                                   12 03.10.18
 20000    198507        126 luk                                   12 03.10.18
  6000    198526        158 max                                   12 03.10.18
  1200    198526        206 max                                   12 03.10.18

But I want this:

   AMOUNT CUSTOMERID     ITEMID USERNAME                        USERID SUMMARYDAY

  20000    198507        126 luk                                   12 03.10.18
  6000     198526        158 max                                   12 03.10.18

The query at the moment:

SELECT max(totalamount) as amount, cg.customerId, g.itemid,
       (select c.nickname from customer c where c.customerId=cg.customerid) as nickname,
       12 as clientId, sysdate as summaryDate
FROM ItemBuy cg,
     ItemToSell gf,
     Item g
WHERE cg.itemSellId = gf.itemSellId and gf.itemId = g.itemId
  and cg.type = 0 and cg.shopId = 12
  and cg.starttime >= sysdate-100 and cg.starttime < sysdate+100
group by cg.customerId
having max(totalamount) > 0

I anonymized the query a bit but I my main question is:

How can I keep specific colums with an group by statement and tell sql to just keep it after group by and max () have "choosen" a row.

Thank you so much in advance!

Peter

I would suggest this straight forward way and translate your description "outputs the row with the MAX(amount) per customer and all of its values" to SQL:

select * from a_table t
where (t.customerid,t.amount) in (
   select customerid,max(amount) from a_table group by customerid);

さらに入力が必要な場合はお知らせください。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Postgres SQL: column must appear in the GROUP BY clause or be used in an aggregate function

分類Dev

MySQL aggregate function against different groups which should be computed from an outermost GROUP BY?

分類Dev

Array intersection as aggregate function for group by

分類Dev

#1054 - Unknown column in 'group statement'

分類Dev

exclude a column from group by statement

分類Dev

How can I include a PERCENTILE_CONT column within a select statement without generating an error about the ORDER BY clause or aggregate function?

分類Dev

Pandas Group then Shift Column and keep last row

分類Dev

Rails: must appear in the GROUP BY clause or be used in an aggregate function

分類Dev

SOLVED Sequelize get data of another column while using Aggregate Function

分類Dev

create new column in a dataframe using custom aggregate function in pandas

分類Dev

PostgreSQL. Select a column that correlates with value in the aggregate function

分類Dev

Extract comments from R source files, keep function in which they occurs

分類Dev

How to group on a column, array aggregate on another and create a single JSON object keyed by the grouped column

分類Dev

Mongodb Aggregate Nested Group

分類Dev

Mongodb Aggregate Nested Group

分類Dev

Group by / Aggregate Pandas Dataframe

分類Dev

SQL GROUP BY Aggregate

分類Dev

How I can group timestamp column to hourly and aggregate the rows in pandas dataframe

分類Dev

R: how to use the aggregate()-function to sum data from one column if another column has a distinct value?

分類Dev

Aggregate function, group by clause error when summing difference between two columns

分類Dev

keep only those columns in the first file which their column numbers match to numbers in the second file

分類Dev

Groupby on a column and apply function on another column but keep first element of all other columns of dataframe

分類Dev

R Looping aggregate by group count

分類Dev

Mongo multiple $lookup and $group in aggregate

分類Dev

SQL Group By + Aggregate functions with joins

分類Dev

MongoDB: aggregate and group by splitting the id

分類Dev

Group by and Aggregate dataset using Python

分類Dev

Subtract group aggregate values in MongoDB

分類Dev

Using aggregate in a function

Related 関連記事

  1. 1

    Postgres SQL: column must appear in the GROUP BY clause or be used in an aggregate function

  2. 2

    MySQL aggregate function against different groups which should be computed from an outermost GROUP BY?

  3. 3

    Array intersection as aggregate function for group by

  4. 4

    #1054 - Unknown column in 'group statement'

  5. 5

    exclude a column from group by statement

  6. 6

    How can I include a PERCENTILE_CONT column within a select statement without generating an error about the ORDER BY clause or aggregate function?

  7. 7

    Pandas Group then Shift Column and keep last row

  8. 8

    Rails: must appear in the GROUP BY clause or be used in an aggregate function

  9. 9

    SOLVED Sequelize get data of another column while using Aggregate Function

  10. 10

    create new column in a dataframe using custom aggregate function in pandas

  11. 11

    PostgreSQL. Select a column that correlates with value in the aggregate function

  12. 12

    Extract comments from R source files, keep function in which they occurs

  13. 13

    How to group on a column, array aggregate on another and create a single JSON object keyed by the grouped column

  14. 14

    Mongodb Aggregate Nested Group

  15. 15

    Mongodb Aggregate Nested Group

  16. 16

    Group by / Aggregate Pandas Dataframe

  17. 17

    SQL GROUP BY Aggregate

  18. 18

    How I can group timestamp column to hourly and aggregate the rows in pandas dataframe

  19. 19

    R: how to use the aggregate()-function to sum data from one column if another column has a distinct value?

  20. 20

    Aggregate function, group by clause error when summing difference between two columns

  21. 21

    keep only those columns in the first file which their column numbers match to numbers in the second file

  22. 22

    Groupby on a column and apply function on another column but keep first element of all other columns of dataframe

  23. 23

    R Looping aggregate by group count

  24. 24

    Mongo multiple $lookup and $group in aggregate

  25. 25

    SQL Group By + Aggregate functions with joins

  26. 26

    MongoDB: aggregate and group by splitting the id

  27. 27

    Group by and Aggregate dataset using Python

  28. 28

    Subtract group aggregate values in MongoDB

  29. 29

    Using aggregate in a function

ホットタグ

アーカイブ