how to write following SQL using jOOQ?
SELECT *
FROM food_db_schema.tblCategory AS t1
LEFT OUTER JOIN food_db_schema.tblCategory AS t2 ON t1.category_id = t2.parent_id
WHERE t2.parent_id IS NULL
AND t1.heartbeat = "ALIVE";
database is mySQL
flesk's answer depicts nicely how this can be done with jOOQ 1.x. A self-join using aliasing is more or less equivalent to a regular join using aliasing as described in the manual:
https://www.jooq.org/doc/latest/manual/sql-building/table-expressions/aliased-tables/
In the upcoming version 2.0, aliasing will be made less verbose and more type-safe. Hence flesk's solution could be simplified as such:
// Type-safe table aliasing:
TblCategory t1 = TBLCATEGORY.as("t1");
TblCategory t2 = TBLCATEGORY.as("t2");
Record record = create.select()
.from(t1)
// t1 and t2 give access to aliased fields:
.leftOuterJoin(t2).on(t1.CATEGORY_ID.equal(t2.PARENT_ID))
.where(t2.PARENT_ID.isNull())
.and(t1.HEARTBEAT.equal("ALIVE"));
I have also described a more complex example for a self-join here:
http://blog.jooq.org/jooq-meta-a-hard-core-sql-proof-of-concept/
Maybe
SELECT *
FROM food_db_schema.tblCategory AS t1
WHERE t1.category_id IS NULL
AND t1.heartbeat = "ALIVE";
, but are you sure t2.parent_id is both supposed to be NULL and equal to t1.category_id?
EDIT:
Then something like
Table<TblCategoryRecord> t1 = TBLCATEGORY.as("t1");
Table<TblCategoryRecord> t2 = TBLCATEGORY.as("t2");
Field<Integer> t1CategoryId = t1.getField(TblCategory.CATEGORY_ID);
Field<String> t1Heartbeat = t1.getField(TblCategory.HEARTBEAT);
Field<Integer> t2ParentId = t2.getField(TblCategory.PARENT_ID);
Record record = create.select().from(t1)
.leftOuterJoin(t2).on(t1CategoryId.equal(t2ParentId))
.where(t2ParentId.isNull())
.and(t1Heartbeat.equal("ALIVE"));
depending on what the generated classes, properties and meta-model objects are called.
Related
I'm having a hard time trying to inner join 4 tables to determine the region of a warehouse based of the warehouse_id. I'm almost sure my logic is correct but I get an operator error while running the code (error operator does not exist : integer = character).
So the issue is that t1.location_id is an integer while t3.country_id is a character (for example 'US'). FYI all other id columns are integers. Does anyone have any suggestions? Thanks in advance!
Here's my query :
SELECT warehouse_name
FROM warehouses t1
INNER JOIN locations t2
ON t1.location_id = t2.location_id
INNER JOIN countries t3
ON t1.location_id = t3.country_id
INNER JOIN regions t4
ON t1.location_id = t4.region_id
WHERE t4.region_id = 3;
I am really confused with this codes. I have query like this
CREATE ALGORITHM=UNDEFINED DEFINER=`root`#`localhost` SQL SECURITY DEFINER VIEW `aIPK` AS select
`ipbmst_fakultas`.`Kode` AS `Fakultas`,
`ipbmst_departemen`.`Kode` AS `Departemen`,
`akdmst_mahasiswamagister`.`NIM` AS `NIM`,
`akdmst_mahasiswamagister`.`TahunMasuk` AS `TahunMasuk`,
`akdhis_kelanjutanstudi`.`IPK` AS `IPK`
from (((((`akdmst_mahasiswamagister` left join `akdmst_mayor` on((`akdmst_mahasiswamagister`.`MayorID` = `akdmst_mayor`.`ID`)))
left join `ipbmst_departemen` on((`akdmst_mayor`.`DepartemenID` = `ipbmst_departemen`.`ID`)))
left join `ipbmst_fakultas` on((`ipbmst_departemen`.`FakultasID` = `ipbmst_fakultas`.`ID`)))
left join `ipbmst_orang` on((`akdmst_mahasiswamagister`.`NIM` = `ipbmst_orang`.`NIMS2Key`)))
left join `akdhis_kelanjutanstudi` on((`akdhis_kelanjutanstudi`.`NIM` = `ipbmst_orang`.`NIMS2Key`)))
WHERE `akdhis_kelanjutanstudi`.`IPK` IS NOT NULL
order by NIM
LIMIT 100;
but "IPK" result is NULL, actually IPK has its value. What's wrong with those codes?
Here is my table structure:
table 1: akdmst_mahasiswamagister (ID, MahasiswaID, NIM, MayorID, TahunMasuk)
table 2: akdmst_mayor(ID, DepartemenID)
table 3: ipbmst_departemen(ID, FakultasID, DepartmenName)
table 4: ipbmst_fakultas(ID, FacultyName)
table 5: ipbmst_orang(ID, Name, NIMS2Key)
table 6: akdhis_kelanjutanstudi(ID, NIM, IPK)
I also have the other problem about this query. It took too much time to query the view. I thought maybe it's because table akdhis_kelanjutanstudi that consists of more than 300K data records. I have used "LIMIT 100" but still the same. could you please help me to solve that problem?
I don't know what the exact problem(s) is, but your WHERE clause has a problem:
WHERE IPK IS NOT NULL
It is not allowed to refer to a column alias in the WHERE clause, because its value may not be determined yet. Instead, you should use this:
WHERE akdhis_kelanjutanstudi.IPK IS NOT NULL
Update:
The parentheses you used in your original view look strange, unnecessary, and possibly wrong. Try using the following:
CREATE ALGORITHM=UNDEFINED DEFINER=`root`#`localhost`
SQL SECURITY DEFINER VIEW aIPK AS
SELECT t4.Kode AS Fakultas,
t3.Kode AS Departemen,
t1.NIM AS NIM,
t1.TahunMasuk AS TahunMasuk,
t6.IPK AS IPK
FROM akdmst_mahasiswamagister t1
LEFT JOIN akdmst_mayor t2
ON t1.MayorID = t2.ID
LEFT JOIN ipbmst_departemen t3
ON t2.DepartemenID = t3.ID
LEFT JOIN ipbmst_fakultason t4
ON t3.FakultasID = t4.ID
LEFT JOIN ipbmst_orang t5
ON t1.NIM = t5.NIMS2Key
LEFT JOIN akdhis_kelanjutanstudi t6
ON t6.NIM = t5.NIMS2Key
WHERE t6.IPK IS NOT NULL
ORDER BY NIM
LIMIT 100;
Running sql instead of creating algorithm is easier for testing.
In your case check the conditions in join
on((`akdhis_kelanjutanstudi`.`NIM` = `ipbmst_orang`.`NIMS2Key`)))
also you need to change
WHERE `akdhis_kelanjutanstudi`.`IPK` IS NOT NULL
To
WHERE `IPK` IS NOT NULL
you have mistake in your description,
your tables are
table 1: akdhis_mahasiswamagister (ID, MahasiswaID, NIM, MayorID, TahunMasuk)
table 2: akdmst_mayor(ID, DepartemenID)
table 3: ipbmst_departemen(ID, FakultasID, DepartmenName)
table 4: ipbmst_fakultas(ID, FacultyName)
table 5: ipbmst_orang(ID, Name, NIMS2Key)
table 6: akdhis_kelanjutanstudi(ID, NIM, IPK)
but i dont see "akdhis_mahasiswamagister" table in your select
CREATE ALGORITHM=UNDEFINED DEFINER=root#localhost SQL SECURITY DEFINER VIEW aIPK AS select
ipbmst_fakultas.Kode AS Fakultas,
ipbmst_departemen.Kode AS Departemen,
akdmst_mahasiswamagister.NIM AS NIM,
akdmst_mahasiswamagister.TahunMasuk AS TahunMasuk,
akdhis_kelanjutanstudi.IPK AS IPK
from (((((akdmst_mahasiswamagister inner join akdmst_mayor on((akdmst_mahasiswamagister.MayorID = akdmst_mayor.ID)))
inner join ipbmst_departemen on((akdmst_mayor.DepartemenID = ipbmst_departemen.ID)))
inner join ipbmst_fakultas on((ipbmst_departemen.FakultasID = ipbmst_fakultas.ID)))
inner join ipbmst_orang on((akdmst_mahasiswamagister.NIM = ipbmst_orang.NIMS2Key)))
inner join akdhis_kelanjutanstudi on((akdhis_kelanjutanstudi.NIM = ipbmst_orang.NIMS2Key)))
WHERE IPK IS NOT NULL
order by NIM
LIMIT 100;
akdhis_mahasiswamagister and akdmst_mahasiswamagister are different? may you are using wrong table, and so you have incorrect information
Hi and sorry if I don't do the question the correct way
I have this query :
SELECT
t4.tar_nombre as nombre_tarea,
t2.rdi_fechacti as fecha_actividad,
t3.rea_hrstarea as horas_trabajadas
FROM
act_usuario t1
INNER JOIN
act_regisdiario t2
ON
(t2.usu_id = t1.usu_id)
INNER JOIN
act_registtarea t3
ON
(t3.rdi_id = t2.rdi_id)
INNER JOIN
act_tarea t4
ON
(t4.tar_id = t3.tar_id)
WHERE
t4.usu_id = 4
GROUP BY
t3.rea_id
ORDER BY
t2.rdi_fechacti
And the query print this :
So, I need when in "fecha_Actividad" exist the same "nombre_actividad" , thats only show me one "fecha_actividad" and the sum of "horas_trabajadas".
For example the query show this:
But I need this (because the same "nombre_tarea" is in the same date):
The reference:
Sorry my english.
You can simply use SUM(field) syntax after grouping.
SELECT
t4.tar_nombre as nombre_tarea,
t2.rdi_fechacti as fecha_actividad,
t3.rea_hrstarea as horas_trabajadas,
SUM(t3.rea_hrstarea)
FROM
act_usuario t1
INNER JOIN
act_regisdiario t2
ON
(t2.usu_id = t1.usu_id)
INNER JOIN
act_registtarea t3
ON
(t3.rdi_id = t2.rdi_id)
INNER JOIN
act_tarea t4
ON
(t4.tar_id = t3.tar_id)
WHERE
t4.usu_id = 4
GROUP BY
t4.tar_nombre
ORDER BY
t2.rdi_fechacti
There are also other functions you can use on grouped results. You can find them here.
Is there a way for me to also print out values not equal to each other?
I used a command:
WHERE displayname = personnel
I have 800+ data not shown, how can I show these data?
Note personnel and data are from two different tables.
My whole script looks like this:
SELECT a.something, a.somethins2, b.something1, b.something2
FROM a, b
WHERE a.displayname = b.personnel
!= is the "not equals" operator, so you could have a WHERE displayname != personnel clause. A full query would probably look like this:
SELECT displayname, personnel
FROM some_table t1
JOIN some_other_table t2 ON t1.id = t2.id
WHERE displayname != personnel
Try with this
select * from table_name1 t1,table_name2 t2 where
column_name!='".$data."'
and t2.id=t1.id
if you are trying to get the record that matched a.displayname = b.personnel and not matched from table a:
SELECT a.something, a.somethins2, b.something1, b.something2
FROM a LEFT JOIN b
ON a.displayname = b.personnel
I'm working on creating a report for the open source tool Validation Manager. I managed to pull out a report of all requirements covered after lots of work. I'm trying to make a report to list the ones not covered, but using not in gets the query into a never ending processing state.
Here's the DD diagram:
The involved tables are in the bottom right side of the diagram. Sample data can be obtained here. What would be the correct way to get uncovered requirements?
FYI: Uncovered requirements are those that doesn't have a related step and/or their children requirements are covered.
The main issue is that there are, in theory, infinite number of levels of requirement relationships and the SQL I have only works for 2 levels. Trying to figure out a way to look as deep as necessary.
As reference, see the query for the covered requirements below (which is the opposite of what I need):
select
c.covered, t.total
from
(SELECT DISTINCT
count(distinct unique_id) as covered
FROM
requirement
WHERE
requirement.id IN (select
requirement.id
from
`requirement` requirement
INNER JOIN `step_has_requirement` step_has_requirement ON requirement.`id` = step_has_requirement.`requirement_id`
INNER JOIN `step` step ON step_has_requirement.`step_id` = step.`id`
AND step.`test_case_id` = step_has_requirement.`step_test_case_id`
AND step.`test_case_test_id` = step_has_requirement.`step_test_case_test_id`
INNER JOIN `test_case` test_case ON step.`test_case_id` = test_case.`id`
AND test_case.`test_id` = step.`test_case_test_id`
INNER JOIN `test` test ON test_case.`test_id` = test.`id`
INNER JOIN `test_plan_has_test` test_plan_has_test ON test.`id` = test_plan_has_test.`test_id`
INNER JOIN `test_plan` test_plan ON test_plan_has_test.`test_plan_id` = test_plan.`id`
AND test_plan.`test_project_id` = test_plan_has_test.`test_plan_test_project_id`
INNER JOIN `test_project` test_project ON test_plan.`test_project_id` = test_project.`id`
INNER JOIN `requirement_status` requirement_status ON requirement.`requirement_status_id` = requirement_status.`id`
INNER JOIN `requirement_spec_node` requirement_spec_node ON requirement.`requirement_spec_node_id` = requirement_spec_node.`id`
AND requirement_spec_node.`requirement_spec_project_id` = requirement.`requirement_spec_node_requirement_spec_project_id`
AND requirement_spec_node.`requirement_spec_spec_level_id` = requirement.`requirement_spec_node_requirement_spec_spec_level_id`
AND requirement_spec_node.`requirement_spec_id` = requirement.`requirement_spec_node_requirement_spec_id`
INNER JOIN `requirement_spec` requirement_spec ON requirement_spec_node.`requirement_spec_id` = requirement_spec.`id`
AND requirement_spec.`project_id` = requirement_spec_node.`requirement_spec_project_id`
AND requirement_spec.`spec_level_id` = requirement_spec_node.`requirement_spec_spec_level_id`
INNER JOIN `project` project ON requirement_spec.`project_id` = project.`id`
WHERE
requirement_status.status = 'general.approved'
and (project.id = $P{target_project_id}
or project.parent_project_id = $P{target_project_id}))
or requirement.id IN (select parent_requirement_id from requirement_has_requirement where parent_requirement_id = requirement.id
and requirement_id in (select
requirement.id
from
`requirement` requirement
INNER JOIN `step_has_requirement` step_has_requirement ON requirement.`id` = step_has_requirement.`requirement_id`
INNER JOIN `step` step ON step_has_requirement.`step_id` = step.`id`
AND step.`test_case_id` = step_has_requirement.`step_test_case_id`
AND step.`test_case_test_id` = step_has_requirement.`step_test_case_test_id`
INNER JOIN `test_case` test_case ON step.`test_case_id` = test_case.`id`
AND test_case.`test_id` = step.`test_case_test_id`
INNER JOIN `test` test ON test_case.`test_id` = test.`id`
INNER JOIN `test_plan_has_test` test_plan_has_test ON test.`id` = test_plan_has_test.`test_id`
INNER JOIN `test_plan` test_plan ON test_plan_has_test.`test_plan_id` = test_plan.`id`
AND test_plan.`test_project_id` = test_plan_has_test.`test_plan_test_project_id`
INNER JOIN `test_project` test_project ON test_plan.`test_project_id` = test_project.`id`
INNER JOIN `requirement_status` requirement_status ON requirement.`requirement_status_id` = requirement_status.`id`
INNER JOIN `requirement_spec_node` requirement_spec_node ON requirement.`requirement_spec_node_id` = requirement_spec_node.`id`
AND requirement_spec_node.`requirement_spec_project_id` = requirement.`requirement_spec_node_requirement_spec_project_id`
AND requirement_spec_node.`requirement_spec_spec_level_id` = requirement.`requirement_spec_node_requirement_spec_spec_level_id`
AND requirement_spec_node.`requirement_spec_id` = requirement.`requirement_spec_node_requirement_spec_id`
INNER JOIN `requirement_spec` requirement_spec ON requirement_spec_node.`requirement_spec_id` = requirement_spec.`id`
AND requirement_spec.`project_id` = requirement_spec_node.`requirement_spec_project_id`
AND requirement_spec.`spec_level_id` = requirement_spec_node.`requirement_spec_spec_level_id`
INNER JOIN `project` project ON requirement_spec.`project_id` = project.`id`
WHERE
requirement_status.status = 'general.approved'
and (project.id = $P{target_project_id}
or project.parent_project_id = $P{target_project_id})))
order by unique_id) c,
(SELECT
count(distinct unique_id) AS total
FROM
`requirement_status` requirement_status
INNER JOIN `requirement` requirement ON requirement_status.`id` = requirement.`requirement_status_id`
INNER JOIN `requirement_spec_node` requirement_spec_node ON requirement.`requirement_spec_node_id` = requirement_spec_node.`id`
AND requirement_spec_node.`requirement_spec_id` = requirement.`requirement_spec_node_requirement_spec_id`
INNER JOIN `requirement_spec` requirement_spec ON requirement_spec_node.`requirement_spec_id` = requirement_spec.`id`
AND requirement_spec.`spec_level_id` = requirement_spec_node.`requirement_spec_spec_level_id`
AND requirement_spec.`project_id` = requirement_spec_node.`requirement_spec_project_id`
INNER JOIN `project` project ON requirement_spec.`project_id` = project.`id`
INNER JOIN `spec_level` spec_level ON requirement_spec.`spec_level_id` = spec_level.`id`
WHERE
requirement_status.status = 'general.approved'
and (project.id = $P{target_project_id}
or project.parent_project_id = $P{target_project_id})) t
Note: The SQL there is an example on how I managed to do the opposite, get the covered requirements. I would like to get the ones not covered. That SQl query is working properly.
What I am looking for is same for the parts which are not covered now(in the above)!
First thing's first
Your schema is your biggest enemy. I have attached an SQLFiddle with your step table. It is a very impressive table, but impressive isn't always workable in SQL. You would do very well to refactor your tables and make them normalized. There are very few cases where it makes sense to put multiple integers into one text file with commas separating them. If the only thing you are going to be putting in that column, ever, is integers separated by columns, you are failing to make your SQL schema even First Normal Form. While it may seem easier to design your schema this way, it is only on the face that you are doing so. In reality, you are creating a mess of epic proportions for yourself - as you may now be discovering. By failing to even meet First Normal Form, you are failing to take advantage of any of SQL's inherent relational power.
Edit This is a fairly large schema to take in. My reaction to being not 1NF was the text part of step. I don't really know what it is aiming to do so it is hard to say for sure, but it put up a big red flag when I saw multiple rows with the same integer columns in a text box separated by commas. That usually is a sign that several columns have been concatenated into one column. After examining other parts of the schema, it is clear that other parts are normalized. It doesn't appear to be an integral part of your schema, so, my mistake. That being said this is still a very complex application with a large number of cross referencing tables. Having both requirement_has_requirement and step_has_requirement as a table may have benefits but it also has serious drawbacks.
I question whether you need to differentiate steps from requirements in the way you currently are. Is a requirement not just another step? I understand you need different columns, but that could be solved by having a requirement_addendum and a step_addendum table, which can be called on at need and ignored at need as well. I note that you have versioning in requirement. Do you not anticipate that you will need versions in step? Do you anticipate that some of your requirements will be versioned at different times than others? Could a version table be created to cover the three version columns in order to have just one version_id in your requirement table and related tables?
In any case, assuming you can't do anything substantial to this table and you just need to pull a query...
My Suggestion
You have not actually defined what "as deep as necessary" is, so you need to determine what "as deep as necessary" means. If you have a formulaic way of discovering this, you can accomplish this relatively easily by creating a recursive procedure like #RandomSeed suggested. If you do not have a particularly formulaic way of determining what "as deep as necessary" is, are you going to determine by a client-defined number of levels? If so you can still rely on a stored procedure.
If you want to make these determinations on a more case-by-case basis, or if you suspect these requirements might change, you can also consider hard-coding the information into a MySQL table which stores metadata for your schema. This can allow you to determine how many steps by running an SQL SELECT query on this metadata table to receive your answer. You can easily assign each step or each scenario to have a number of recursions you want to accomplish. You would be able to use this information in a Stored Procedure.
If you can alter your table at all...
The benefit to altering your table even slightly is that you can create a terminal to step_has_requirement and requirement_has_requirement. Each record would then have a "yes" (1) or a "no" (0) to determine if there was a requirement involved. You could then run a query on any record which was a 1 or a 0 at your leisure.
requirement_has_requirement
id | status | major_version | mid_version | minor_version
.. | 0 | NULL | NULL | NULL
.. | 1 | .. | .. | ..
step_has_requirement
id | status | ...
Etc.
Query now becomes as simple as toggling between status 0 and status 1 to find your records. You can query all elements of your hierarchy at the same time.
Please note that this becomes much easier and contains less repetition if step and requirement are in one table with additional columns on separate tables based on whether it was a requirement or a step. There are no doubt drawbacks to this but there are also benefits. If you have the ability to make these changes prior to Volume 1 it could be beneficial.
Conclusion
You clearly have a very sophisticated application in development. Unfortunately the sophistication is outgrowing your schema. There is no "best" way to accomplish what you are looking for without making some minor changes to your schema. If changing your schema is not currently an option then I strongly advise you to make a quick hack so as not to waste more time and make a request to adapt the schema soon, before this becomes an even larger headache for you.
It's a mess, but I took a crack at it. By breaking it up into chunks with CTEs, you can troubleshoot smaller pieces.
;WITH
cteRequirement (id) AS
( SELECT distinct requirement.id
FROM requirement
INNER JOIN step_has_requirement
ON requirement.id = step_has_requirement.requirement_id
INNER JOIN step
ON step.id = step_has_requirement.step_id
AND step.test_case_id = step_has_requirement.step_test_case_id
AND step.test_case_test_id = step_has_requirement.step_test_case_test_id
INNER JOIN test_case
ON test_case.id = step.test_case_id
AND test_case.test_id = step.test_case_test_id
INNER JOIN test
ON test.id = test_case.test_id
INNER JOIN test_plan_has_test
ON test_plan_has_test.test_id = test.id
INNER JOIN test_plan
ON test_plan.id = test_plan_has_test.test_plan_id
AND test_plan.test_project_id = test_plan_has_test.test_plan_test_project_id
INNER JOIN test_project
ON test_project.id = test_plan.test_project_id
INNER JOIN requirement_status
ON requirement_status.id = requirement.requirement_status_id
INNER JOIN requirement_spec_node
ON requirement_spec_node.id = requirement.requirement_spec_node_id
AND requirement_spec_node.requirement_spec_project_id = requirement.requirement_spec_node_requirement_spec_project_id
AND requirement_spec_node.requirement_spec_spec_level_id = requirement.requirement_spec_node_requirement_spec_spec_level_id
AND requirement_spec_node.requirement_spec_id = requirement.requirement_spec_node_requirement_spec_id
INNER JOIN requirement_spec
ON requirement_spec.id = requirement_spec_node.requirement_spec_id
AND requirement_spec.project_id = requirement_spec_node.requirement_spec_project_id
AND requirement_spec.spec_level_id = requirement_spec_node.requirement_spec_spec_level_id
INNER JOIN project
ON project.id = requirement_spec.project_id
WHERE requirement_status.[status] = 'general.approved'
and (project.id = $P{target_project_id} or project.parent_project_id = $P{target_project_id})
),
cteParent (id) AS
( SELECT DISTINCT parent_requirement_id
FROM requirement_has_requirement
INNER JOIN cteRequirement
ON cteRequirement.id = requirement_has_requirement.requirement_id
WHERE parent_requirement_id = requirement.id
),
ListOfRequirementIDs (requirement_id) AS
( SELECT id FROM cteRequirement UNION
SELECT id FROM cteParents
),
ListOfUniqueIDs (unique_id) AS
( SELECT DISTINCT unique_id
FROM requirement_status
INNER JOIN requirement
ON requirement.requirement_status_id = requirement_status.id
INNER JOIN requirement_spec_node
ON requirement_spec_node.id = requirement.requirement_spec_node_id
AND requirement_spec_node.requirement_spec_id = requirement.requirement_spec_node_requirement_spec_id
INNER JOIN requirement_spec
ON requirement_spec_node.requirement_spec_id = requirement_spec.id
AND requirement_spec.spec_level_id = requirement_spec_node.requirement_spec_spec_level_id
AND requirement_spec.project_id = requirement_spec_node.requirement_spec_project_id
INNER JOIN project
ON project.id = requirement_spec.project_id
INNER JOIN spec_level
ON spec_level.id = requirement_spec.spec_level_id
WHERE requirement_status.status = 'general.approved'
and (project.id = $P{target_project_id} or project.parent_project_id = $P{target_project_id})
)
SELECT 'Covered' AS [Type], COUNT(ListOfRequirementIDs.requirement_id) AS Cnt
FROM ListOfRequirementIDs
UNION ALL
SELECT 'Total' AS [Type], COUNT(ListOfUniqueIDs.unique_id) AS Cnt
FROM ListOfUniqueIDs