简体   繁体   English

有没有更好的方法来编写我写的查询

[英]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 让在条件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 或者您可以尝试更简单的方法,使用bool (0或1) count SUM

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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