获取组内每个条件的单独计数
Get separate count for each condition within group
我正在尝试查看来自 Oracle 10g table 的 table 信息,该信息在各自的列中列出了列的特定值的计数,每行都是组值。
例如:
第一个 select 是:
SELECT processed_by, count(priority) as P2
FROM agreement_activity
WHERE priority = '2'
GROUP BY processed_by
输出:
PROCESSED_BY P2
------------------------------ ----------
Alicia 2
Christine 2
第二个select是:
SELECT processed_by, count(priority) as P1
FROM agreement_activity
WHERE priority = '1'
GROUP BY processed_by
输出:
PROCESSED_BY P1
------------------------------ ----------
Bonita 2
Alicia 6
Christine 2
我正在寻找的是输出这些值如下:
PROCESSED_BY P1 P2
------------------------------ ---------- ----------
Bonita 2
Alicia 6 2
Christine 2 2
这可能吗?
您可以将 sum
与 case
表达式一起使用以获得条件计数:
select processed_by
, sum(case when priority = 1 then 1 else 0 end) as P1
, sum(case when priority = 2 then 1 else 0 end) as P2
from agreement_activity
group by processed_by
P.S。如果您不关心 P1
或 P2
也许 null
而不是 0
您可以在两个表达式中省略 else
。
这就是我实现 sql 的方式。我正在使用 firebird 代码,但我认为您可以将代码转换为 sql
SELECT
a.equipmentid,
a.name equipname,
w1.countwarranty1 ,
w2.countwarranty2
FROM TBL_EQUIPMENTMST a
inner JOIN
(select c.equipmentid, count(c.WARRANTYID) countwarranty1 from tbl_equipwarranty c where c.serviceproduct='1' group by c.equipmentid) w1
ON w1.equipmentid = a.equipmentid
inner JOIN
(select d.equipmentid, count(d.WARRANTYID) countwarranty2 from tbl_equipwarranty d where d.serviceproduct='2' group by d.equipmentid) w2
ON w2.equipmentid = a.equipmentid
inner JOIN
(select e.equipmentid, count(e.equiplocationid) countlocation from tbl_equiplocation e group by e.equipmentid) w3
ON w3.equipmentid = a.equipmentid
这是输出
我的保修 table 每个设备只有 2 个保修,这就是为什么它只显示 2 个。
您还可以使用不同的计数内部连接到相同的 table。
如您所见,我的保修有多个保修,每个保修的服务产品都不同 table
如果我编辑你的代码,它会像这样
SELECT a.processed_by, b.priority as p2, c.priority as p1
FROM agreement_activity a
inner join
(
SELECT w1.processed_by, count(w1.priority) as P2 FROM agreement_activity w1
WHERE w1.priority = '2' GROUP BY w1.processed_by
) b
on a.processed_by = b.processed_by
inner join
(
SELECT w2.processed_by, count(w2.priority) as P2 FROM w2.agreement_activity
WHERE w2.priority = '1' GROUP BY w2.processed_by
) c
on a.processed_by = c.processed_by
测试一下
我正在尝试查看来自 Oracle 10g table 的 table 信息,该信息在各自的列中列出了列的特定值的计数,每行都是组值。
例如: 第一个 select 是:
SELECT processed_by, count(priority) as P2
FROM agreement_activity
WHERE priority = '2'
GROUP BY processed_by
输出:
PROCESSED_BY P2
------------------------------ ----------
Alicia 2
Christine 2
第二个select是:
SELECT processed_by, count(priority) as P1
FROM agreement_activity
WHERE priority = '1'
GROUP BY processed_by
输出:
PROCESSED_BY P1
------------------------------ ----------
Bonita 2
Alicia 6
Christine 2
我正在寻找的是输出这些值如下:
PROCESSED_BY P1 P2
------------------------------ ---------- ----------
Bonita 2
Alicia 6 2
Christine 2 2
这可能吗?
您可以将 sum
与 case
表达式一起使用以获得条件计数:
select processed_by
, sum(case when priority = 1 then 1 else 0 end) as P1
, sum(case when priority = 2 then 1 else 0 end) as P2
from agreement_activity
group by processed_by
P.S。如果您不关心 P1
或 P2
也许 null
而不是 0
您可以在两个表达式中省略 else
。
这就是我实现 sql 的方式。我正在使用 firebird 代码,但我认为您可以将代码转换为 sql
SELECT
a.equipmentid,
a.name equipname,
w1.countwarranty1 ,
w2.countwarranty2
FROM TBL_EQUIPMENTMST a
inner JOIN
(select c.equipmentid, count(c.WARRANTYID) countwarranty1 from tbl_equipwarranty c where c.serviceproduct='1' group by c.equipmentid) w1
ON w1.equipmentid = a.equipmentid
inner JOIN
(select d.equipmentid, count(d.WARRANTYID) countwarranty2 from tbl_equipwarranty d where d.serviceproduct='2' group by d.equipmentid) w2
ON w2.equipmentid = a.equipmentid
inner JOIN
(select e.equipmentid, count(e.equiplocationid) countlocation from tbl_equiplocation e group by e.equipmentid) w3
ON w3.equipmentid = a.equipmentid
这是输出
我的保修 table 每个设备只有 2 个保修,这就是为什么它只显示 2 个。
您还可以使用不同的计数内部连接到相同的 table。 如您所见,我的保修有多个保修,每个保修的服务产品都不同 table
如果我编辑你的代码,它会像这样
SELECT a.processed_by, b.priority as p2, c.priority as p1
FROM agreement_activity a
inner join
(
SELECT w1.processed_by, count(w1.priority) as P2 FROM agreement_activity w1
WHERE w1.priority = '2' GROUP BY w1.processed_by
) b
on a.processed_by = b.processed_by
inner join
(
SELECT w2.processed_by, count(w2.priority) as P2 FROM w2.agreement_activity
WHERE w2.priority = '1' GROUP BY w2.processed_by
) c
on a.processed_by = c.processed_by
测试一下