不知道你是什么数据库,用sql server举例说明类似这样
写两个子查询
SELECT
s.ProductName,
s.Amount AS CurrentAmount,
COALESCE(
(SELECT Amount
FROM Sales s2
WHERE s2.ProductName = s.ProductName
AND s2.SaleDate >= '${DATEINMONTH(MONTHDELTA(CONCATENATE(date_month,"-01"),-1),1)}'/*环比*/
AND s2.SaleDate >='${DATEINMONTH(MONTHDELTA(CONCATENATE(date_month,"-01"),-1),-1)}'
) AS PreviousPeriodAmount,
COALESCE(
(SELECT Amount
FROM Sales s3
WHERE s3.ProductName = s.ProductName
AND s3.SaleDate >= '${YEARDELTA(CONCATENATE(date_month,"-01"),-1)}'
AND s3.SaleDate >= '${DATEINMONTH(YEARDELTA(CONCATENATE(date_month,"-01"),-1),-1)}'
) AS SamePeriodLastYearAmount
FROM
Sales s
WHERE
s.SaleDate = '${date_month}'/*SaleDate 是年月字段,你可以自己处理成年月*/