简体   繁体   中英

is there a better way to write the query that I wrote

I have the following query which gives me the overall status of a project. I was wondering if there was a better way to write the same query.

an example schema for the table:

CREATE TABLE `test_status`
(
      `test_id` int
(11) NOT NULL,
      `group_id` int
(11) NOT NULL,
      `sub_test_id` int
(11) NOT NULL,
      `project_id` int
(11) NOT NULL,
      `collection_status` varchar
(20) DEFAULT NULL,
      `labeling_status` varchar
(20) DEFAULT NULL,
      `upload_status` varchar
(20) DEFAULT NULL,
      `upload_date` date DEFAULT NULL,
      `disk_number` int
(11) DEFAULT NULL,
      `subject_id` varchar
(10) DEFAULT NULL,
      `collection_id` varchar
(25) DEFAULT NULL,
      `assigned_to` int
(11) DEFAULT NULL,
      `assigned_on` date DEFAULT NULL,
      `turned_in_date` date DEFAULT NULL,
      PRIMARY KEY
(`test_id`,`group_id`,`sub_test_id`,`project_id`));

My Query:

select
    (select count(*)
    from test_status
    where project_id = 7 and collection_status = 'COLLECTED') as collected,
    (select count(*)
    from test_status
    where project_id = 7 and collection_status = 'SCHEDULED') as scheduled,
    (select count(*)
    from test_status
    where project_id = 7 and collection_status = 'CORRUPTED') as corrupted,
    (select count(*)
    from test_status
    where project_id = 7 and collection_status is NULL) as 'not collected',
    (select count(*)
    from test_status
    where project_id = 7 and ((labeling_status = 'GOOD' and (upload_status != 'UPLOADED' or upload_status != 'QUEUED')) or (labeling_status = 'Labeled'))) as 'Labeled',
    (select count(*)
    from test_status
    where project_id = 7 and (labeling_status = 'RAW') or (labeling_status = 'ASSIGNED') ) as 'to be labeled',
    (select count(*)
    from test_status
    where project_id = 7 and labeling_status = 'RE-LABEL') as 'Re-label',
    (select count(*)
    from test_status
    where project_id = 7 and (upload_status = 'UPLOADED' or upload_status = 'QUEUED')) as 'Uploaded',
    (select count(*)
    from test_status
    where project_id = 7 and labeling_status = 'GOOD' and upload_status is null) as 'ready to be uploaded';

You can try to use condition aggregate function will be better.

let the condition in CASE WHEN

SELECT 
    COUNT(CASE WHEN collection_status = 'COLLECTED' THEN 1 END),
    COUNT(CASE WHEN collection_status = 'SCHEDULED' THEN 1 END),
    COUNT(CASE WHEN collection_status = 'CORRUPTED' THEN 1 END),
    COUNT(CASE WHEN collection_status is NULL THEN 1 END),
    COUNT(CASE WHEN  ((labeling_status = 'GOOD' and (upload_status != 'UPLOADED' or upload_status != 'QUEUED')) or (labeling_status = 'Labeled'))THEN 1 END),
    COUNT(CASE WHEN (labeling_status = 'RAW') or (labeling_status = 'ASSIGNED') THEN 1 END),
    COUNT(CASE WHEN labeling_status = 'RE-LABEL' THEN 1 END),
    COUNT(CASE WHEN (upload_status = 'UPLOADED' or upload_status = 'QUEUED') THEN 1 END),
    COUNT(CASE WHEN labeling_status = 'GOOD' and upload_status is null THEN 1 END)
FROM test_status
WHERE project_id = 7

Or you can try a simpler way, SUM with bool (0 or 1) to count

SELECT 
    SUM(collection_status = 'COLLECTED'),
    SUM(collection_status = 'SCHEDULED'),
    SUM(collection_status = 'CORRUPTED' ),
    SUM(collection_status is NULL ),
    SUM(((labeling_status = 'GOOD' and (upload_status != 'UPLOADED' or upload_status != 'QUEUED')) or (labeling_status = 'Labeled'))),
    SUM((labeling_status = 'RAW') or (labeling_status = 'ASSIGNED') ),
    SUM(labeling_status = 'RE-LABEL' ),
    SUM((upload_status = 'UPLOADED' or upload_status = 'QUEUED')),
    SUM(labeling_status = 'GOOD' and upload_status is null )
FROM test_status
WHERE project_id = 7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM