5

I am using Jaspersoft's iReport to create a report that will pull data from my Maintenance Assistant CMMS database. The DB is on the localhost, and I am not creating any tables or columns. MA CMMS takes care of that. I only want to pull the data to arrange in a report.

Here is my code:

SELECT * 
FROM   "tblworkordertask" 
WHERE  "dbltimespenthours" > 0 
       AND "dtmdatecompleted" BETWEEN $P{DATE_FROM} AND $P{DATE_TO}
GROUP  BY "intworkorderid" 

and my error:

Caused by: java.sql.SQLSyntaxErrorException: Column reference 'tblWorkOrderTask.id' is invalid, or is part of an invalid expression.  For a SELECT list with a GROUP BY, the columns and expressions being selected may only contain valid grouping expressions and valid aggregate expressions.

I don't know why the error is referring to 'tblWorkOrderTask.id' because I don't have such a column, nor did I ask for that column.

If I take out the group by clause, it works fine, but as you could expect, I get multiple results with the same WorkOrderID. I want to group it by this column, and then count the results. I tried using SELECT DISTINCT, but then I get errors about columns that aren't selected.

3
  • What are the fields in tblWorkOrderTask ?
    – kevro
    Commented Mar 22, 2013 at 18:35
  • I am not at work at the moment, But I'll be sure to look that up for you. I do know that I only care about a few of them for this report. Commented Mar 22, 2013 at 22:58
  • Fields are: "id", "intWorkOrderID", "intOrder", "dblTimeSpentHours", "strDescription", "intAssignedToUserID", "dtmDateCompleted", "intCompletedByUserID" Commented Mar 25, 2013 at 15:26

3 Answers 3

3

You're selecting all columns in the tblWorkOrderTask table. The "id" column is the first column in that table. You are getting an error because you do not have all columns specified in the select list.

This select would work, but I'm not sure what information you need out of your table.

SELECT id, intworkorderid
FROM tblWorkOrderTask 
group by id, intworkorderid

http://www.w3schools.com/sql/sql_groupby.asp

6
  • So, if I understand you correctly, in order to use group by, you need to be specific in the select line? If I need to, I need to individually slect all the fields? Also, why use the ID in the group by? do I need to group by the first column always? even if all the records have a unique value there? Commented Mar 22, 2013 at 22:53
  • I was working with all the information you provided, but you have to include everyone non-aggregate column from the select in the group by. Your group by can have more columns than are in your select, but not less.
    – Vinnie
    Commented Mar 24, 2013 at 3:50
  • Okay, so I tried this: select "id", "intWorkOrderID" from "tblWorkOrderTask" where "dblTimeSpentHours">0 and "dtmDateCompleted" between $P{DATE_FROM} and $P{DATE_TO} group by "id", "intWorkOrderID" Just as you said, the group by contains the exact same columns from the select list. However I am now getting: Error filling print... Unknown column name : intOrder net.sf.jasperreports.engine.JRException: Unknown column name : intOrder That column is the third column, after the workorderid column. It's not in my select list, and no where else in my code/report. So why does it care? Commented Mar 25, 2013 at 15:24
  • see above for column names Commented Mar 25, 2013 at 15:26
  • Also, an interesting note... I tried your suggestion, and I got a Table does not exist error. So I encapsulated everything in quotes, and now I get the name Unknown column error, on intOrder (just like above) Commented Mar 25, 2013 at 15:35
0

Get rid of the GROUP BY clause -- if you're just trying to order the result, then use ORDER BY instead; but otherwise, you don't need either.

EDIT

As the error says, everythign in your SELECT list must be one of two things -- either 1) also listed in your GROUP BY list, or 2) an aggregated value. Here is a sample that will work:

SELECT intworkorderid, COUNT(*) 
FROM   "tblworkordertask" 
WHERE  "dbltimespenthours" > 0 
       AND "dtmdatecompleted" BETWEEN $P{DATE_FROM} AND $P{DATE_TO}
GROUP  BY "intworkorderid" 
5
  • No, sorry... I don't care if it's ordered or not, I just want to group the results by the work order number. The work orders have multiple tasks, and the CMMS stores each task as a record, so there are multiple records each with the same work order number. All I want to know is how many unique work orders have the timespent field completed. Commented Mar 22, 2013 at 22:56
  • @KevinSadowy -- you're missing the point. ORDER BY will group your results. Since you're not using aggregation, and you're not using a HAVING filter, you don't need GROUP BY. GROUP BY has special requirements -- if you're going to use it, you have to follow the rules for it (i.e., listing all of your fields in SELECT, and then listing them all again in GROUP BY). Either way will work, but you can't use it in conjunction with a SELECT * -- that's what the error is telling you.
    – Chains
    Commented Mar 25, 2013 at 15:22
  • @KevinSadowy -- see updated example demonstrating what the error is telling you / how to satisfy the requirement.
    – Chains
    Commented Mar 25, 2013 at 15:27
  • Alright. I tried your sample, adding uppercase and quotes. and I got the unknown column: ID error. I tried changing it to ORDER BY, then I got a message saying if the select has an aggregate, then all entries must be valid aggregates. So I removed the COUNT (*), and again got the unknown column: ID error again. So, I changed the select to SELECT *, and now I got exactly what I expected: multiple entries of the same work order ID, but in numerical order. Commented Mar 25, 2013 at 17:52
  • @KevinSadowy -- the invalid column error means that you used a column name in your SELECT that was not found in the table (presumably "ID"). As for the aggregate function (COUNT(*)), if you use that, then you MUST use GROUP BY. Sorry for the frustration on your part -- this is basic SQL -- there is a quick primer / reference here that can help you understand it a bit better maybe: w3schools.com/sql/sql_groupby.asp
    – Chains
    Commented Mar 26, 2013 at 16:08
0

Yes - in order to use group by, you need to be specific in the select line.

So first, decide which fields you want to display. If you want them all, then include them all.

As soon as you add a COUNT() function to get a count of the selected fields, you will need to add the GROUP BY clause. COUNT() is an AGGREGATE function, like SUM() and AVG().

It's a little counter-intuitive and a bit of a pain to specify so many fields in the GROUP BY clause, but it's necessary. The FIRST GROUP BY field is the most important, since this is usually what you are concerned about. This first field can be any of the SELECTed fields, it is not necessarily the first.

Include EVERY field in your GROUP BY that is not an AGGREGATE function like COUNT().

Also, if you are trying to COUNT a group of orders, you probably don't want or need all of the fields in the SELECT. You probably want to specify just the fields that are unique to the work order ID.

Example: If you want to get a COUNT of these fields, you would specify all of the SELECTED fields EXCEPT the COUNT().

SELECT
    intWorkOrderID,
    COUNT(id),
    strDescription
FROM   tblworkordertask
WHERE  dbltimespenthours > 0 
       AND dtmdatecompleted BETWEEN $P{DATE_FROM} AND $P{DATE_TO}
GROUP BY
    intworkorderid,
    strDescription

Not the answer you're looking for? Browse other questions tagged or ask your own question.