select ID,(select count(*) from 表A where ID='A') 货物总数,
(select count(*) from 表B where ID='A' and 是否抽查 is not null) 抽查数,
((select cast(count(*) as float) from 表B where ID='A' and 是否抽查 is not null)/(select cast(count(*) as float) from 表A where ID='A')) 抽查率
from 表A group by id;
WITH OK AS(SELECT ID,货物流水号,0 AS 是否抽查
FROM 表A
UNION ALL
SELECT ID,货物流水号, 是否抽查
FROM 表B)
SELECT ID,COUNT(DISTINCT 货物流水号) AS 货物总数,STR(SUM(是否抽查)*100/COUNT(DISTINCT 货物流水号),4,0)+'%'
FROM OK
GROUP BY ID
完全可以!~~~