我都使用subquery [Below]来确定百分比。但是我需要一个没有子查询的查询。谁能帮我,如何使用联接来计算百分比?
已使用查询
def unreliable_method(item) do
# Do something with `item`
:ok
rescue
_err ->
Logger.error("Failed for item: #{inspect(item)}")
Logger.error(inspect(__STACKTRACE__))
{:error, :processing_failed}
end
答案 0 :(得分:0)
“我需要一个没有子查询的查询。”
答案:
SELECT Count(*) All_count
, SUM(CASE WHEN N.id_membid_claimid_c IS NULL THEN 1 else 0 end) as Dropped_count
, SUM(CASE WHEN N.id_membid_claimid_c IS NULL THEN 1 else 0 end) * 1.0 / Count(*) as Dropping_Percentage
FROM pfo_bhi_new N
RIGHT JOIN pfo_bhi_old o ON o.id_membid_claimid_c = N.id_membid_claimid_c
说明:
使用有关内容的假设来审查主查询:
SELECT Count(*) Dropped_count, '1' b
FROM pfo_bhi_new N
RIGHT JOIN pfo_bhi_old o ON o.id_membid_claimid_c = N.id_membid_claimid_c
WHERE N.id_membid_claimid_c IS NULL
应为您提供pfo_bhi_old中未出现在pfo_bhi_new中的记录数。这里的假设是您需要基于现有的右连接进行总计。所有匹配和不匹配的记录。
因此,可以通过删除where子句来计算所有现有记录, COUNT(*)会给您总计。
接下来,您要计算没有匹配项的数目,这将使您使用where子句获得的值“滴落计数”,即id_membid_claimid_c为null的那些位置,即 SUM(CASE当N.id_membid_claimid_c为NULL时,则返回1,否则0结束)。换句话说,只有在id_membid_claimid_c字段为null时,才会将1加到总和上,否则它将加零(0)。
我将分子乘以1.0,以强制SQL Server使用十进制值,并使查询更易于阅读。
如果需要使用小数(16,9)作为结果,则应该是这样。
SELECT Count(*) All_count
, SUM(CASE WHEN N.id_membid_claimid_c IS NULL THEN 1 else 0 end) as Dropped_count
, SUM(CASE WHEN N.id_membid_claimid_c IS NULL THEN 1 else 0 end) * 1.0 / Count(*) as Dropping_Percentage
, CAST(SUM(CASE WHEN N.id_membid_claimid_c IS NULL THEN 1 else 0 end) AS DECIMAL(16,9)) / CAST(Count(*) AS DECIMAL(16,9)) as Dropping_Pct_16_9
FROM pfo_bhi_new N
RIGHT JOIN pfo_bhi_old o ON o.id_membid_claimid_c = N.id_membid_claimid_c