How to query multiple SUMs of the same item using SQL in iReport

后端 未结 3 1979
甜味超标
甜味超标 2021-01-07 10:40
  • I\'m creating a JasperReport using iReport, and as such, I\'m limited* to one SQL query.

  • I have a table \'statistics\', with a \'name\' (VARCHAR), \

3条回答
  •  爱一瞬间的悲伤
    2021-01-07 11:16

    sum(case when -condition- then count else 0 end)

    SELECT
      SUM(case when $P{oneDayAgo} <= datetime then count else 0 end) as 'today',
      SUM(case when $P{oneWeekAgo} <= datetime then count else 0 end) as 'thisweek',
      SUM(count) as 'thismonth'
    FROM
      statistics
    WHERE
       name = "test"
      AND $P{oneMonthAgo} <= datetime
      AND datetime <= $P{now}
    

    note that if you need averages, be sure to substitute NULL for 0.

提交回复
热议问题