简体   繁体   English

具有百分比明细的MySQL子查询(困难查询)

[英]Mysql sub-query with percentage breakdown (difficult query)

Ok I have two related tables one which contains the main fields called 'opportunities' and one which contains addition fields called 'opportunities_cstm'. 好的,我有两个相关的表,一个包含称为“ opportunities”的主要字段,另一个包含名为“ opportunities_cstm”的附加字段。 For our purposes the opportunities table contains the following fields: id and sales_stage. 就我们而言,机会表包含以下字段:id和sales_stage。 The opportunities_cstm table contains the fields id_c and sales_stage_before_closed_c. careers_cstm表包含字段id_c和sales_stage_before_closed_c。 id_c is what relates the two tables. id_c是将两个表关联起来的内容。

sales_stage contains the values from 1 to 10 and also either 'Closed Lost' or 'Closed Won'. sales_stage包含从1到10的值,还包含“ Closed Lost”或“ Closed Won”。 In the actual application 1 to 10 represent percentage bands from 0-9% to 90-99% and closed lost is 0% and closed won is 100%. 在实际应用中,1到10代表从0-9%到90-99%的百分比区间,平仓亏损为0%,平仓赢率为100%。

sales_stage_before_closed_c is the percentage band that it was at before it was closed. sales_stage_before_closed_c是关闭前的百分比范围。

So in my actual query I need display a percentage for each sales_stage on how many opportunities reached this stage and resulted in a won opportunity and how many reach this stage and resulted in a lost. 因此,在我的实际查询中,我需要为每个sales_stage显示一个百分比,其中有多少机会达到了这一阶段并带来了获胜的机会,有多少机会达到了这一阶段并导致了损失。

Update to new query which is much closer to what I need: 更新到新查询,它更接近我需要的内容:

SELECT opportunities_c_top.sales_stage_before_closed_c AS 'Sales Stage',
COUNT(*) * 100.0 /
( SELECT COUNT(*)
FROM `opportunities_cstm` opportunities_cstm join 
`opportunities` opportunities
on opportunities_cstm.id_c = opportunities.id WHERE opportunities.`sales_stage` =   'Closed Won' AND opportunities_cstm.sales_stage_before_closed_c = opportunities_c_top.sales_stage_before_closed_c ) AS 'Closed Won',

COUNT(*) * 100.0 /
( SELECT COUNT(*)
FROM `opportunities_cstm` opportunities_cstm join 
`opportunities` opportunities
on opportunities_cstm.id_c = opportunities.id WHERE opportunities.`sales_stage` =   'Closed Lost' AND opportunities_cstm.sales_stage_before_closed_c = opportunities_c_top.sales_stage_before_closed_c ) AS 'Closed Lost'

FROM `opportunities_cstm` opportunities_c_top join 
`opportunities` opportunities_top
on opportunities_top.id = opportunities_c_top.id_c
WHERE (opportunities_top.`sales_stage` = 'Closed Won' OR opportunities_top.`sales_stage` = 'Closed Lost')  
GROUP BY opportunities_c_top.sales_stage_before_closed_c

http://sqlfiddle.com/#!2/ac28d/1 http://sqlfiddle.com/#!2/ac28d/1

Its still not 100% correct though, if you run the query it shows 60%-69% as 200 on both instead of 50 each side. 尽管它仍然不是100%正确,但是如果您运行查询,它将显示60%-69%均为200,而不是每侧50。

SQL is not really for presentation. SQL并不是真正用于演示。 I would consider just extracting the raw data then manipulating it in PHP. 我会考虑只提取原始数据,然后在PHP中进行操作。

SELECT 
    opportunities.`sales_stage`,
    opportunities_cstm.`percent_before_closed_c`,
    count(*)
FROM `opportunities` opportunities join 
    `opportunities_cstm` opportunities_cstm
    on opportunities.id = opportunities_cstm.id_c
WHERE opportunities.`sales_stage` in ('Closed Lost', 'Closed Won')
GROUP BY opportunities.`sales_stage`, opportunities_cstm.`percent_before_closed_c`

Unless I am completely missing the point. 除非我完全忽略了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM