如果列的值为零,如何从SQL查询中获取值?

时间:2017-12-29 19:57:37

标签: python sqlite

基本上我有一个训练数据集,但我希望它显示零而不是它根本不显示

以下节目全部不完整且完整,但我希望它显示不完整,并且来自零不完整的部门为0

这是我到目前为止,我需要它来展示所有,我的同事试图帮助我,但不得不离开,所以我问你们这些人

Select distinct Department, Total FROM
(
Select Department, Total FROM

(
SELECT 'Incomplete' AS Status, department, count(*) as Total
FROM MyTable
WHERE CompletedTraining = 'Incomplete'
GROUP BY department
union all
Select 'Complete' AS Status, department, Count(*) as Total
FROM MyTable
WHERE CompletedTraining = ' Complete'
Group By Department
)
WHERE Status = 'Incomplete'
)
union all
SELECT DISTINCT Department, NULL AS Total
FROM MyTable

当我运行我的python脚本时,我得到了这个

Incomplete Training
[6, 8, 2, 3, 6, 4, 4, 5, 2, 4]

但如果有人接受我的培训,我希望它在列表中显示为零 所以我的预期输出将是这样的

Incomplete Training
[6, 8, 2, 3, 6, 4, 4, 5, 2, 4, 0, 0]

示例文档X具有多个用户的1500行

Curriculum Name     Department          Employee Name           Employee Email  Employee Status Date Assigned   Completion Date CompletedTraining
Security Training   OIS John Doe        john.doe@email.org      Active  7/18/2017 21:47 8/2/2017 21:31                          Complete
Support Training    OIS Home Simpson    home.simpson@email.gov  Active  4/20/2017 15:33 5/3/2017 22:18                          Complete
Security Training   ASD bart simpson    bart.simpson@email.gove Active  5/5/2017 20:22  11/30/2017 19:43                        incomplete
Security Training   CO  jack johnson    jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   ECARS    jack johnson   jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   EO  jack johnson    jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   ISD jack johnson    jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   MSCD    jack johnson    jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   RD  jack johnson    jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   TTD jack johnson    jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   DP  jack johnson    jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   MLD jack johnson    jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   OIS jack johnson    jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   TTD jack johnson    jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   TTD jack johnson    jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete
Security Training   AQPSD jack johnson  jack.johnson@email.gov  Active  5/9/2017 21:15  5/10/2017 20:23                         incomplete

1 个答案:

答案 0 :(得分:2)

这应该是你不完整的#。

select distinct 
    Department
    , (select count(*) from MyTable t2 where t2.Department = t1.Department and t2.CompletedTraining='Incomplete') as Total
from MyTable t1

获取不完整并在同一行完成

select distinct 
    Department
    , (select count(*) from MyTable t2 where t2.Department = t1.Department and t2.CompletedTraining='Incomplete') as TotalIncomplete
    , (select count(*) from MyTable t2 where t2.Department = t1.Department and t2.CompletedTraining='Complete') as TotalComplete
from MyTable t1

或类似

select distinct 'Incomplete' as Src
    , Department
    , (select count(*) from MyTable t2 where t2.Department = t1.Department and t2.CompletedTraining='Incomplete') as Total
from MyTable t1
union
select distinct 'Complete' as Src
    , Department
    , (select count(*) from MyTable t2 where t2.Department = t1.Department and t2.CompletedTraining='Completeomplete') as Total
from MyTable t1