简体   繁体   中英

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'. For our purposes the opportunities table contains the following fields: id and sales_stage. The opportunities_cstm table contains the fields id_c and sales_stage_before_closed_c. id_c is what relates the two tables.

sales_stage contains the values from 1 to 10 and also either 'Closed Lost' or '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%.

sales_stage_before_closed_c is the percentage band that it was at before it was closed.

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.

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

Its still not 100% correct though, if you run the query it shows 60%-69% as 200 on both instead of 50 each side.

SQL is not really for presentation. I would consider just extracting the raw data then manipulating it in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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