Dav*_*vid 2 sql db2 ibm-midrange
我有两张桌子。
表 1 包含订单和客户代码。表 2 包含带有问题代码的订单。
我需要能够从表 1 中返回客户的不同订单计数,以及表 2 中问题代码为“F”的客户的不同订单计数。然后最终字段将是两者的比率。发行数/订单数。我正在使用 AS400/DB2 SQL。任何帮助将不胜感激。
Customer ORcnt IScnt IssueRatio
cust1 450 37 0.082
cust2 255 12 0.047
cust3 1024 236 0.230
cust4 450 37 0.082
您可以使用outer join问题表和countwith distinct。类似这样的事情取决于您的表定义:
select o.customercode,
count(distinct o.orderid),
count(distinct i.orderid),
count(distinct i.orderid)/count(distinct o.orderid) ratio
from table1 o
left join table2 i on o.orderid = i.orderid and i.issuecode = 'F'
group by o.customercode
Run Code Online (Sandbox Code Playgroud)
有些数据库需要将比率转换为小数——我不确定db2。如果需要,一种方法是将结果乘以 1.0:
1.0*count(distinct i.orderid)/count(distinct o.orderid)
另外,您可能不需要distinct-count取决于您的数据......
| 归档时间: |
|
| 查看次数: |
12433 次 |
| 最近记录: |