简体   繁体   English

选择每个月的列总和

[英]select sum of a column for each month

i'm trying to get a sum of a column for each month. 我试图每个月获得一列的总和。 this is what i've done. 这就是我所做的。

My SQL Query : 我的SQL查询:

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

this is the result : 这是结果:

  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

and this is the result i want : 这是我想要的结果:

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

note: 27.20 in Total column is a sum of PilotWaitingTime in January and 12.00 is a sum of PilotWaitingTime in February 注意:“总计”列中的27.20是一月份的PilotWaitingTime的总和,而12.00是二月份的PilotWaitingTime的总和

Thank you for your help 谢谢您的帮助

This query will return the sum for each month: 此查询将返回每个月的总和:

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

then you just can join back this query with the pilot table: 那么您只需将这个查询与试验表连接起来即可:

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

Your pilot table data would be helpful to provide the perfect query. 您的试验表数据将有助于提供理想的查询。 But you can try these two queries. 但是您可以尝试这两个查询。

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;

OR 要么

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

Here's the query you could try out (Example http://sqlfiddle.com/#!9/96667/1 ) 这是您可以尝试的查询(示例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
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() is a mysql function that returns the last day of the month. last_day()是一个mysql函数,它返回该月的最后一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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