MySQL:每周和每月SUM

时间:2017-07-17 11:37:03

标签: mysql select

以下是 TOUR 表:

    tour_id | tour_date | amount
    ------------------------------
    ABC     | 2016-03-07   | 100
    ABC     | 2016-03-15   | 200
    DEF     | 2016-03-07   | 300
    DEF     | 2016-03-15   | 100
    GHI     | 2016-03-07   | 30

我想得到以下内容:

  • 每周总和
  • 每月总和。

此最终数据必须如下所示:

  tour_id| weekly sum. | monthly sum.
    --------------------------------
    ABC  | 100         | 300       
    DEF  | 300         | 400   
    GHI  | 30          | 30  

2 个答案:

答案 0 :(得分:0)

    SELECT 
    tour_id, WEEKLY, MONTHLY
FROM
    TOUR T1
        INNER JOIN
    (SELECT 
        tour_id, SUM(amount) WEEKLY
    FROM
        TOUR
    GROUP BY tour_id , WEEK(tour_date)) WEEK ON WEEK.tour_id = T1.tour_id
        INNER JOIN
    (SELECT 
        tour_id, SUM(amount) MONTHLY
    FROM
        TOUR
    GROUP BY tour_id , DATE_FORMAT(tour_date, '%m')) MONTH ON WEEK.tour_id = MONTH.tour_id

尝试以上查询。

答案 1 :(得分:0)

import pandas as pd

# Read csv
df = pd.read_csv("pathtocsv")

# Filter column N
df= df[df["N"] > "Y"]

# Write to csv again
df.to_csv("newcsvpath")