我应该编写一个查询,以2010年每个月的降序显示总利润。

到目前为止,我有一个查询以降序显示总利润的查询,并且我有一个可以每月提取的查询,但是我似乎无法将两者联系起来。

select (SalesPrice - AcquisitionPrice) profit, datesold
from Transaction
where DateSold >= '10-jan-01' and DateSold <= '10-dec-31';


和:

select to_char(datesold, 'mon')
from transaction
group by to_char(datesold, 'mon')

最佳答案

Oracle查询:

SELECT TRUNC( datesold, 'MM' ) AS "Month",
       SUM( SalesPrice - AcquisitionPrice ) AS profit
FROM   Transaction
WHERE  EXTRACT( YEAR FROM datesold ) = 2010
GROUP BY TRUNC( datesold, 'MM' )
ORDER BY TRUNC( datesold, 'MM' ) DESC


MySQL和Oracle查询:

SELECT EXTRACT( MONTH FROM datesold ) AS mnth,
       SUM( SalesPrice - AcquisitionPrice ) AS profit
FROM   Transaction
WHERE  EXTRACT( YEAR FROM datesold ) = 2010
GROUP BY EXTRACT( MONTH FROM datesold )
ORDER BY EXTRACT( MONTH FROM datesold ) DESC

08-06 22:05