RDB

MySQL] Group By 이후 SUM

2023. 6. 15. 20:05
목차
  1. 1. ChatGPT 검색 결과
  2. 2. 내가 작성한 코드
반응형

1. ChatGPT 검색 결과

  • 질문한 내용: sum after group by in mysql
  • chatgpt의 답변에서 아이디어를 얻음(subquery 사용)
  • 아래는 chatgpt의 답변

To calculate the sum of a column after using the GROUP BY clause in MySQL, you can make use of the GROUP BY clause along with the SUM() function in a subquery. Here's an example:

SELECT group_column, SUM(column_to_sum) AS sum_result
FROM (
    SELECT group_column, column_to_sum
    FROM your_table
    GROUP BY group_column, column_to_sum
) AS subquery
GROUP BY group_column;

In the above query, replace your_table with the actual name of your table. group_column represents the column(s) by which you want to group the data, and column_to_sum is the column for which you want to calculate the sum.

 

For example, if you have a table named "orders" with columns "customer_id", "product_id", and "quantity", and you want to calculate the total quantity of products ordered by each customer, you can use the following query:

SELECT customer_id, SUM(quantity) AS total_quantity_ordered
FROM (
    SELECT customer_id, product_id, SUM(quantity) AS quantity
    FROM orders
    GROUP BY customer_id, product_id
) AS subquery
GROUP BY customer_id;

This query will calculate the sum of the "quantity" column after grouping the rows by "customer_id" and "product_id" in the subquery. Then, in the outer query, it will group the result by "customer_id" to provide the total quantity ordered by each customer.

 

 

2. 내가 작성한 코드

select sum(sum1), sum(sum2) from
( 
    select G.file_id, G.runtime_gitReset as sum1, G.runtime_editScript as sum2
    from commits as c
    inner join files f
    on c.commit_id = f.commit_id
    inner join changes_GT_3_runtime G on f.file_id = G.file_id
    where c.project_name = 'neo4j'
    group by G.file_id, G.runtime_gitReset, G.runtime_editScript
    order by G.file_id
) as subquery

 

반응형

'RDB' 카테고리의 다른 글

MySQL] 유용한 문법 정리(추가)  (0) 2023.06.09
MySQL 백업 및 복원  (0) 2023.06.09
MySQL : 조인(JOIN)  (0) 2021.11.11
MySQL 입문  (0) 2021.11.11
데이터베이스 개요  (0) 2021.11.11
  1. 1. ChatGPT 검색 결과
  2. 2. 내가 작성한 코드
'RDB' 카테고리의 다른 글
  • MySQL] 유용한 문법 정리(추가)
  • MySQL 백업 및 복원
  • MySQL : 조인(JOIN)
  • MySQL 입문
나른한 찰리
나른한 찰리
반응형
나른한 찰리
Moojun's Storage
나른한 찰리
전체
오늘
어제
  • 분류 전체보기 (83)
    • 포트폴리오 스프링 부트 프로젝트 (1)
    • 멘질멘질] 2023 졸업 프로젝트 (16)
    • 학부연구생 (5)
    • Spring Boot (17)
      • Test Code (10)
    • Amazon Web Services(AWS) (3)
    • Ubuntu Server (1)
    • RDB (6)
    • git (1)
    • 오류 (9)
    • MacOS (4)
    • python library (5)
    • 기타 (8)
    • 프로그래밍 도서 (4)
      • 클린 코드(로버트 마틴) (4)
    • 코딩 테스트 (3)
      • 프로그래머스 (3)
    • 회고 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • pandas#dataframe
  • 우분투#ubuntu#mysql#mysql설치#mysql오류

최근 댓글

최근 글

hELLO · Designed By 정상우.
내 관리 화면으로 이동
나른한 찰리
MySQL] Group By 이후 SUM
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.