-1

How do I get unique values of one column based on another column using the query?

I tried using

(double)selectFrom(tasks).where(tasks.tasks_type.eq()).uniqueResult(tasks.task_cycle_time_hr);

I want to automate this and make sure that all the values of task_type are being read and a unique value for each of the tasks_type is being returned!

For all the values in the column task_type, I require a unique value from the column task_cycle_time_hr.

enter image description here

Jaco-Ben Vosloo
  • 3,770
  • 2
  • 16
  • 33
  • 1
    best share a screen of the data in question and clarify what exactly you need, not quite clear to me. – Benjamin Dec 16 '21 at 07:28

2 Answers2

0

I don't really understand why you're trying to do this in one query.

If you want to get the cycle time (task_cycle_time_hr column) for each task type (tasks_type column), just do queries in a loop for each possible tasks_type value. If you don't know those a priori, do queries for each value returned by a query of the task type values, which would look something like

for (String taskType : selectFrom(tasks).list(tasks.tasks_type)) {  
    double cycleTime = (double) selectFrom(tasks)
                      .where(db_table.tasks_type.eq(taskType))
                      .firstResult(tasks.task_cycle_time_hr);
    
    traceln("Task type " + taskType + ", cycle time " + cycleTime);
}

But this just amounts to querying all rows and reading the task type and cycle time values from each, so you wouldn't normally do it like this: you'd just have a single query looping through all the full rows instead...

List<Tuple> rows = selectFrom(tasks).list();

for (Tuple row : rows) {
    traceln("Task type " +
        row.get(tasks.tasks_type) + ", cycle time " + 
        row.get(tasks.task_cycle_time_hr));
}

NB: I assume you don't have any rows with duplicate task types because then the whole exercise doesn't make sense unless you want only the first row for each task type value, or want some kind of aggregate (e.g., sum) of the cycle time values for each given task type. You were trying to use uniqueResult, which may mean you want to get a value if there is exactly one row (for a given task type) and 'no result otherwise', but uniqueResult throws an exception (errors) if there isn't exactly one row (so you can't use that directly like that). In that case one way (there are others, some probably slightly better) would be to do a count first to check; e.g. something like

for (String taskType : selectFrom(tasks).list(tasks.tasks_type)) {

    int rowCount = (int) selectFrom(tasks)
              .where(db_table.task.eq(taskType))
              .count();

    if (rowCount == 1) {
       double cycleTime = (double) selectFrom(tasks)
                      .where(db_table.tasks_type.eq(taskType))
                      .firstResult(tasks.task_cycle_time_hr);
    
       traceln("Task type " + taskType + ", unique cycle time " + cycleTime);
    }
}
Stuart Rossiter
  • 2,432
  • 1
  • 17
  • 20
-1

Import your excel sheet into the AnyLogi internal DB and then make use of the DB wizard that will take you step by step to write the code to retrieve the data you want

enter image description here

(double) selectFrom(data)
    .where(data.tasks.eq("T1"))
    .firstResult(data.task_cycle_time_hr)
Jaco-Ben Vosloo
  • 3,770
  • 2
  • 16
  • 33