A: 

UNION'd queries should produce the same columns (name, type). Set unused columns to NULL or use a differenciation column:

SELECT
  SUM(count) as `total`,
  'today' as `when`
FROM
  statistics
WHERE
  name = "test"
 AND $P{oneDayAgo} <= datetime
 AND datetime <= $P{now}
UNION
SELECT
  SUM(count) as `total`,
  'thisWeek' as `when`
FROM
  statistics
WHERE
  name = "test"
 AND $P{oneWeekAgo} <= datetime
 AND datetime <= $P{now}
UNION
SELECT
  SUM(count) as `total`,
  'thisMonth' as `when`
FROM
  statistics
WHERE
  name = "test"
 AND $P{oneMonthAgo} <= datetime
 AND datetime <= $P{now}
streetpc
So how do I differentiate the 'day' from the 'week' from the 'month' rows?
barryred
added sample code for that
streetpc
You could also use CASE/WHEN to compute the 'when' column, instead of UNION.
streetpc
This solution will give you 3 rows of 2 cols (3 "totals" and 3 "when"). Hafthor's will give you one row with 3 columns, maybe preferable.
streetpc
Thank you, that worked a treat.
barryred
+3  A: 

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.

Hafthor
The other ways will work, but this a better approach...
eschneider
$P{} - Is this Jasper magic, or some part of the SQL language I've never seen before?
Chris Kaminski
$P{} is Jasper magic! (Just a parameter specified in the report)
barryred
Cool, thanks Hafthor, I have no idea if this is 'faster' or 'better' but it is definitely cleaner and clearer.
barryred
A: 

You have 2 options:

1) Remove the 'as' part of each query and then it will come as 1 column that won't have a name

2) Create a temp table and insert those rows into a temp table and then query the temp table

Avitus