选择每个月的列总和

时间:2015-09-10 20:01:48

标签: mysql select

我试图获得每个月的一列总和。这就是我所做的。

我的SQL查询:

SELECT `ID_Pilot` ,`PilotStationDate`  ,`PilotWaitingTime`, ( SELECT FORMAT(SUM( `PilotWaitingTime` ),2) FROM pilot ) AS Total
FROM pilot

这是结果:

  ID_Pilot  PilotStationDate    PilotWaitingTime    Total
  P001      2013-01-01                       0.2    39.20
  P002      2013-01-02                      19.2    39.20
  P003      2013-01-03                       7.8    39.20
  P004      2013-02-04                       6.4    39.20
  P005      2013-02-06                       5.6    39.20

这是我想要的结果:

ID_Pilot    PilotStationDate    PilotWaitingTime    Total
  P001      2013-01-01                       0.2    27.20
  P002      2013-01-02                      19.2    27.20
  P003      2013-01-03                       7.8    27.20
  P004      2013-02-04                       6.4    12.00
  P005      2013-02-06                       5.6    12.00

注意:总计列中的27.20是1月份PilotWaitingTime的总和,而12.00是2月份PilotWaitingTime的总和

感谢您的帮助

4 个答案:

答案 0 :(得分:0)

此查询将返回每个月的总和:

SELECT
  DATE_FORMAT(PilotStationDate, '%Y-%m') as year_month,
  FORMAT(SUM( `PilotWaitingTime` ),2) as total
FROM pilot
GROUP BY
  DATE_FORMAT(PilotStationDate, '%Y-%m')

然后您可以将此查询与试用表联接起来:

SELECT
  p.`ID_Pilot`,
  p.`PilotStationDate`,
  p.`PilotWaitingTime`,
  t.total
FROM
  pilot p INNER JOIN (
    SELECT
      DATE_FORMAT(PilotStationDate, '%Y-%m') as year_month,
      FORMAT(SUM( `PilotWaitingTime` ),2) as total
    FROM pilot
    GROUP BY
      DATE_FORMAT(PilotStationDate, '%Y-%m')
  ) t ON DATE_FORMAT(p.PilotStationDate, '%Y-%m') = t.year_month

答案 1 :(得分:0)

您的试用表数据有助于提供完美的查询。但你可以试试这两个问题。

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:src="@drawable/logo"
    android:id="@+id/logo"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

<Button
    android:padding="@dimen/default_margin"
    android:text="Login using Facebook"
    android:textColor="@android:color/white"
    android:background="@drawable/com_facebook_button_background"
    android:id="@+id/authButton"
    android:layout_below="@+id/logo"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

OR

SELECT `ID_Pilot` ,`PilotStationDate`  ,`PilotWaitingTime`, ( SELECT FORMAT(SUM( `PilotWaitingTime` ),2) FROM pilot p2 where p2.ID_Pilot = p1.ID_Pilot ) AS Total
FROM pilot p1;

答案 2 :(得分:0)

以下是您可以尝试的查询(示例http://sqlfiddle.com/#!9/96667/1

select pilot.*, totals.tot
from pilot
inner join
(
  select 
    year(pilotstationdate) as yr,
    month(pilotstationdate) as mth,
    sum(pilotwaitingtime) as tot
  from pilot
  group by 
    year(pilotstationdate),
    month(pilotstationdate)
) totals
  on year(pilot.pilotstationdate) = totals.yr
  and month(pilot.pilotstationdate) = totals.mth

答案 3 :(得分:0)

select p.id_pilot, p.pilotstationdate, p.pilotwaitingtime,
(select sum(pilotwaitingtime) 
  from pilot 
  where last_day(pilotstationdate)=last_day(p.pilotstationdate)) total
from pilot p

last_day()是一个返回该月最后一天的mysql函数。