Goetz Markgraf
08/23/2023, 6:33 PMjob
and a table part
where every job has one or more parts.
Requirement: I want to query data from a specific part – and I also want to know how many parts are there for this job.
The result should look something like this:
jobId, partId, some_data_from_this_part, count(parts_for_this_job)
2736, 1, 'data', 2
I have found this stakeoverflow post that looked very much like what we want to do. However, it uses a SubQueryExpression
that does not seem to be present anymore.
The documentation for exposed (https://github.com/JetBrains/Exposed/wiki/DSL) is also very short for this topic.
Any help would be appreciated.Arjan van Wieringen
08/23/2023, 6:45 PMGoetz Markgraf
08/23/2023, 6:47 PMArjan van Wieringen
08/23/2023, 6:47 PMGoetz Markgraf
08/23/2023, 6:47 PMpart
Goetz Markgraf
08/23/2023, 6:48 PMpart
and the number of parts in this job. There is one-to-many from job
to part
Arjan van Wieringen
08/23/2023, 6:48 PMGoetz Markgraf
08/23/2023, 6:51 PMwhere
jobid (FK to job
) and partId to get the data. For the count
, i wanted some kind of sub query the is like SELECT count(partid) from part where jobid = <some job id>Goetz Markgraf
08/23/2023, 6:51 PMArjan van Wieringen
08/23/2023, 6:52 PMArjan van Wieringen
08/23/2023, 6:53 PMGoetz Markgraf
08/23/2023, 6:56 PMpart
because it is enough.
We have these parts:
jobid | partid | part_data
------|--------|-----------
123 | 1 | data1
123 | 2 | data2
123 | 3 | data3
Now I need a query that, if I insert the jobid 123
and partid 2
return:
123 | 2 | data2 | 3
And the last 3
is the number of all parts for jobid 123
Goetz Markgraf
08/23/2023, 7:04 PMSELECT t1.jobid, t1.partid, t1.part_data, COUNT(t2.partid) AS part_count
FROM your_table t1
LEFT JOIN your_table t2 ON t1.jobid = t2.jobid
WHERE t1.jobid = 123 AND t1.partid = 2
GROUP BY t1.jobid, t1.partid, t1.part_data;
It really seems to work without a subquery. I have never tried to join one table to itself.
Does that work? How do I write this in exposed?Arjan van Wieringen
08/23/2023, 7:05 PMGoetz Markgraf
08/23/2023, 7:06 PMGoetz Markgraf
08/23/2023, 7:10 PM