how can i set loop in pl/sql query function which the result of each loop out put in different columns and all in one table - plsqldeveloper

how can i set loop in pl/sql query function which the result of each loop out put in different columns and all in one table.
the function is :
select t1.aaa, coalesce(t2.bbb_count, 0) bbb_count,
coalesce(t2.ccc_sum, 0) ccc_sum
from (
select distinct aaa
from nrb
) t1
left join (
select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
from nrb t
where t.vvv IN ('3','4','5','6','D','E','F')
and t.ddd like '50%'
and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
group by t.aaa
) t2 on t1.aaa = t2.aaa
order by t1.aaa;
this function will give me a table with 200 rows and 3 columns. i need to make a loop in
" and t.ddd like '50%'"
line , from 50 to 55. i mean the result will be 200 rows and 15 columns.
Pl/sql 7.0.2 unlimited user lisence
oci : 9.2
oracle db : 11.1.0.6.0 enterprise
os : win xp

select t1.aaa,
coalesce(t2.bbb_count, 0) bbb_50_count, coalesce(t2.ccc_sum, 0) ccc_50_sum,
coalesce(t3.bbb_count, 0) bbb_51_count, coalesce(t3.ccc_sum, 0) ccc_51_sum,
coalesce(t4.bbb_count, 0) bbb_52_count, coalesce(t4.ccc_sum, 0) ccc_52_sum,
coalesce(t5.bbb_count, 0) bbb_53_count, coalesce(t5.ccc_sum, 0) ccc_53_sum,
coalesce(t6.bbb_count, 0) bbb_54_count, coalesce(t6.ccc_sum, 0) ccc_54_sum,
coalesce(t7.bbb_count, 0) bbb_55_count, coalesce(t7.ccc_sum, 0) ccc_55_sum
from (
select distinct aaa
from nrb
) t1
left join (
select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
from nrb t
where t.vvv IN ('3','4','5','6','D','E','F')
and t.ddd like '50%'
and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
group by t.aaa
) t2 on t1.aaa = t2.aaa
left join (
select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
from nrb t
where t.vvv IN ('3','4','5','6','D','E','F')
and t.ddd like '51%'
and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
group by t.aaa
) t3 on t1.aaa = t3.aaa
left join (
select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
from nrb t
where t.vvv IN ('3','4','5','6','D','E','F')
and t.ddd like '52%'
and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
group by t.aaa
) t4 on t1.aaa = t4.aaa
left join (
select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
from nrb t
where t.vvv IN ('3','4','5','6','D','E','F')
and t.ddd like '53%'
and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
group by t.aaa
) t5 on t1.aaa = t5.aaa
left join (
select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
from nrb t
where t.vvv IN ('3','4','5','6','D','E','F')
and t.ddd like '54%'
and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
group by t.aaa
) t6 on t1.aaa = t6.aaa
left join (
select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
from nrb t
where t.vvv IN ('3','4','5','6','D','E','F')
and t.ddd like '55%'
and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
group by t.aaa
) t7 on t1.aaa = t7.aaa
order by t1.aaa;

Related

I am missing an alias for a derived table

I know, this question has been asked very often but I know my error, I know how I could fix it, but I canĀ“t find the point where the error is. In my opinion, all the subqueries have different and unique names, I even gave the columns different names then the subqueries. Any help would be appreciated. Where is the point I am missing an alias?
Whenever I am trying to run this query I get the response "Every derived table must have its alias", which is an understandable error message, but I can't figure out where my error is located.
SELECT
mso.entity_id,
GROUP_CONCAT(msh.comment) AS comment,
msoa.lastname,
base_grand_total,
mso.created_at,
mso.status,
marketplace_order_id AS amazon_order_id,
clvData.recurrenceRate,
clvData.avgRepRate
FROM
mag_sales_flat_order AS mso
LEFT JOIN mag_sales_flat_order_status_history AS msh ON mso.entity_id = msh.parent_id
LEFT JOIN mag_sales_flat_order_address AS msoa ON mso.entity_id = msoa.parent_id
left join (
select
cast(((cet.cec - cnt.cnc) / cst.csc) AS decimal(6, 2)) as recurrenceRate,
avg(repRate.countedOrders) AS avgRepRate
from(
Select
*,
(
select
count(customer_email) AS csc
from
mag_sales_flat_order
where
created_at between '2017-01-01'
and '2017-12-31'
) AS cst,
(
select
count(customer_email) AS cec
from
mag_sales_flat_order
where
created_at between '2017-01-01'
and '2020-12-31'
) AS cet,
(
select
count(mso_new.customer_email) AS cnc
from
(
select
*
from
mag_sales_flat_order
where
created_at between '2018-01-01'
and current_date()
) AS mso_new
left join (
select
*
from
mag_sales_flat_order
where
created_at between '2017-01-01'
and '2017-12-31'
) AS mso_old on mso_new.customer_email = mso_old.customer_email
)) AS cnt
join (
select
customer_email,
count(grand_total) as countedOrders,
sum(grand_total) as summedOrders
from
mag_sales_flat_order
group by
customer_email
) AS repRate on cl.customer_email = repRate.customer_email
) AS clvData on mso.customer_email = clvData.customer_email
WHERE
store_id IN({$store['id']})
AND (
mso.status = 'complete'
OR mso.status = 'closed'
OR mso.status = 'processing'
OR mso.status = 'exported'
OR mso.status LIKE 'pending%'
)
AND (
DATE_FORMAT(mso.created_at, '%Y-%m-%d') >= '$begin_date'
)
AND (
DATE_FORMAT(mso.created_at, '%Y-%m-%d') <= '$end_date'
)
GROUP BY
entity_id;
SELECT
mso.entity_id,
GROUP_CONCAT(msh.comment) AS comment,
msoa.lastname,
base_grand_total,
mso.created_at,
mso.status,
marketplace_order_id AS amazon_order_id,
clvData.recurrenceRate,
clvData.avgRepRate
FROM mag_sales_flat_order AS mso
LEFT JOIN mag_sales_flat_order_status_history AS msh ON mso.entity_id = msh.parent_id
LEFT JOIN mag_sales_flat_order_address AS msoa ON mso.entity_id = msoa.parent_id
LEFT JOIN
(
SELECT cast(((cet.cec - cnt.cnc) / cst.csc) AS decimal(6, 2)) as recurrenceRate, avg(repRate.countedOrders) AS avgRepRate
FROM
(
SELECT *,
(
SELECT count(customer_email) AS csc
FROM mag_sales_flat_order
WHERE created_at BETWEEN '2017-01-01' AND '2017-12-31'
) AS cst,
(
SELECT count(customer_email) AS cec
FROM mag_sales_flat_order
WHERE created_at BETWEEN '2017-01-01' AND '2020-12-31'
) AS cet,
(
SELECT count(mso_new.customer_email) AS cnc
FROM
(
SELECT *
FROM mag_sales_flat_order
WHERE created_at BETWEEN '2018-01-01' AND getdate()
) AS mso_new
LEFT JOIN
(
SELECT *
FROM mag_sales_flat_order
WHERE created_at BETWEEN '2017-01-01' AND '2017-12-31'
) AS mso_old on mso_new.customer_email = mso_old.customer_email
) AS cnt
) as cl
JOIN
(
SELECT customer_email, count(grand_total) as countedOrders, sum(grand_total) as summedOrders
FROM mag_sales_flat_order
GROUP BY customer_email
) AS repRate on cl.customer_email = repRate.customer_email
) AS clvData on mso.customer_email = clvData.customer_email
WHERE store_id IN({ $ store ['id'] })
AND
(
mso.status = 'complete'
OR mso.status = 'closed'
OR mso.status = 'processing'
OR mso.status = 'exported'
OR mso.status LIKE 'pending%'
)
AND
(
DATE_FORMAT(mso.created_at, '%Y-%m-%d') >= '$begin_date'
)
AND
(
DATE_FORMAT(mso.created_at, '%Y-%m-%d') <= '$end_date'
)
GROUP BY entity_id;

Use group by inside subquery

I am trying to display progressive_total and cumulative_sum group by the subincome field in house_details table using mysql. I built my schema in this link
working query:
SELECT *,
COALESCE(
(SELECT SUM(x.rupees)
FROM house_details x
WHERE MONTH(x.date) < t1.month), '-') AS progressive_total,
(SELECT SUM(x.rupees)
FROM house_details x
WHERE MONTH(x.date) <= t1.month) AS cumulative_sum
FROM
(SELECT MONTHNAME(t.date) AS `monthname`,
MONTH(t.date) `month`,
YEAR(t.date) AS YEAR,
t.income,
t.subincome,
t.ssubincome,
SUM(rupees) AS amount,
GROUP_CONCAT(receipt_id) AS receipt_ids
FROM house_details t
WHERE YEAR(t.date) = YEAR(CURRENT_DATE())
GROUP BY month(t.date),
t.subincome
ORDER BY t.date) t1
but this gives irrelevant cumulative_sum in the field.
I tried to use group by inside the subquery like this:
query:
SELECT *,
COALESCE(
(SELECT SUM(x.rupees)
FROM house_details x
WHERE MONTH(x.date) < t1.month
GROUP BY x.subincome), '-') AS progressive_total,
(SELECT SUM(x.rupees)
FROM house_details x
WHERE MONTH(x.date) <= t1.month
GROUP BY x.subincome) AS cumulative_sum
FROM
(SELECT MONTHNAME(t.date) AS `monthname`,
MONTH(t.date) `month`,
YEAR(t.date) AS YEAR,
t.income,
t.subincome,
t.ssubincome,
SUM(rupees) AS amount,
GROUP_CONCAT(receipt_id) AS receipt_ids
FROM house_details t
WHERE YEAR(t.date) = YEAR(CURRENT_DATE())
GROUP BY month(t.date),
t.subincome
ORDER BY t.date) t1;
but it shows error sub query returns more than one row.
You can use below query for your expected result set
SELECT *,
COALESCE(
(SELECT SUM(pt.rupees) FROM (
SELECT MONTH(`date`) `month`,
MAX(id) id,
SUM(rupees) rupees
FROM house_details
GROUP BY `month`,subincome
) pt
WHERE CASE WHEN pt.month = t1.month THEN pt.id < t1.id ELSE pt.month < t1.month END
), 0) AS progressive_total,
(SELECT SUM(rupees) FROM(
SELECT MONTH(`date`) `month`,
MAX(id) id,
SUM(rupees) rupees
FROM house_details
GROUP BY `month`,subincome
) cs
WHERE CASE WHEN cs.month = t1.month THEN cs.id <= t1.id ELSE cs.month <= t1.month END
) AS cumulative_sum
FROM (
SELECT MONTHNAME(t.date) AS `monthname`,
MAX(id) id,
MONTH(t.date) `month`,
YEAR(t.date) AS `year`,
GROUP_CONCAT(t.income) income,
t.subincome,
GROUP_CONCAT(t.ssubincome) ssubincome,
SUM(rupees) AS amount,
GROUP_CONCAT(receipt_id) AS receipt_ids
FROM house_details t
WHERE YEAR(t.date) = YEAR(CURRENT_DATE())
GROUP BY `monthname`,`month`, t.subincome
ORDER BY `month`
) t1
Demo
Subqueries that written in the previous part of "FROM" must return only one row. Obviously your query turns more than one line here.
Also the query seems a bit complicated.
You can easily get the progressive sum like this:
set #csum := 0;
select id, date, rupees ,(#csum := #csum + rupees) as proggressive_sum
from house_details
order by date;
and you can group on it what you want like this:
Monthly groupped
set #csum := 0;
select month,sum_rupees, (#csum := #csum + sum_rupees) as progressive_sum_monthly
from (
select DATE_FORMAT(date,'%Y-%m') month, sum(rupees) sum_rupees
from house_details
GROUP BY DATE_FORMAT(date,'%Y-%m')
) gg
order by 1;
Year and subincome groupped :
set #csum := 0;
select subincome, year,sum_rupees, (#csum := #csum + sum_rupees) as progressive_sum_subincome
from (
select year(date) year, subincome, sum(rupees) sum_rupees
from house_details
GROUP BY subincome,year(date)
) gg

Error Code: 1242. Subquery returns more than 1 row (for all records)

Here i need to get all the records group by AgentID but it showing error:Error Code: 1242. Subquery returns more than 1 row
table :
customerID amountreceived date_time area agentID paymentmode
1 2000 5/13/2014 hyd 1 cash
enter code here
DELIMITER $$
CREATE DEFINER=`ntc`#`%` PROCEDURE `spforallAgents`()
BEGIN
select
(select
#DayAmount:=sum(AmountRecevied) as Totoalamountperday
from
collection_master
where day(Date_Time) = day(CURRENT_DATE()) group by AgentID),
(select
#MonthAmount:=sum(AmountRecevied) as Totoalamountperday
from
collection_master
where date_time between DATE_FORMAT(NOW(), '%Y-%m-01') and LAST_DAY(now() - interval 0 month) group by AgentID),
(select
#YearAmount:=sum(AmountRecevied) as Totoalamountpermonth
from
collection_master where year(Date_Time) = YEAR(CURRENT_DATE()) group by AgentID),
(select
#Position:=#Position + 1 AS Rank
from
collection_master,
(SELECT #Position:=0) r group by AgentID) as position;
END
Using joins, you could do it something like this (untested):-
SELECT AgentID,
current_date_amount,
date_range_amount,
month_amount,
#Position:=#Position + 1 AS `Rank`
FROM
(
SELECT just_agent.AgentID,
total_current_date.Totoalamountperday AS current_date_amount,
total_date_range.Totoalamountperday AS date_range_amount,
total_month.Totoalamountpermonth AS month_amount
FROM
(
SELECT DISTINCT AgentID
FROM collection_master
) just_agent
LEFT OUTER JOIN
(
select AgentID, SUM(AmountRecevied) as Totoalamountperday
from collection_master
where day(Date_Time) = day(CURRENT_DATE())
group by AgentID
) total_current_date
ON just_agent.AgentID = total_current_date.AgentID
LEFT OUTER JOIN
(
select AgentID, sum(AmountRecevied) as Totoalamountperday
from collection_master
where date_time between DATE_FORMAT(NOW(), '%Y-%m-01') and LAST_DAY(now() - interval 0 month)
group by AgentID
) total_date_range
ON just_agent.AgentID = total_date_range.AgentID
LEFT OUTER JOIN
(
select AgentID, sum(AmountRecevied) as Totoalamountpermonth
from collection_master
where year(Date_Time) = YEAR(CURRENT_DATE())
group by AgentID
) total_month
ON just_agent.AgentID = total_month.AgentID
ORDER BY total_month.Totoalamountpermonth DESC
) Sub1
CROSS JOIN (SELECT #Position:=0) Sub2
Note that this is making a few assumptions. For example your original query doesn't make it clear what order you want to use to assign the rank (I have assumed descending Totoalamountpermonth). Also it would be simpler if there were another table giving the agent IDs, rather than using an extra sub query to get the distinct AgentID
SQL fiddle for it:-
http://www.sqlfiddle.com/#!2/549232/2
EDIT
Joining the query against itself, using the code from the other thread (and M. Massias deserves the credit).
SELECT t2.*
FROM
(
SELECT AgentID,
current_date_amount,
date_range_amount,
month_amount,
#Position1:=#Position1 + 1 AS `Rank`
FROM
(
SELECT just_agent.AgentID,
total_current_date.Totoalamountperday AS current_date_amount,
total_date_range.Totoalamountperday AS date_range_amount,
total_month.Totoalamountpermonth AS month_amount
FROM
(
SELECT DISTINCT AgentID
FROM collection_master
) just_agent
LEFT OUTER JOIN
(
select AgentID, SUM(AmountRecevied) as Totoalamountperday
from collection_master
where day(Date_Time) = day(CURRENT_DATE())
group by AgentID
) total_current_date
ON just_agent.AgentID = total_current_date.AgentID
LEFT OUTER JOIN
(
select AgentID, sum(AmountRecevied) as Totoalamountperday
from collection_master
where date_time between DATE_FORMAT(NOW(), '%Y-%m-01') and LAST_DAY(now() - interval 0 month)
group by AgentID
) total_date_range
ON just_agent.AgentID = total_date_range.AgentID
LEFT OUTER JOIN
(
select AgentID, sum(AmountRecevied) as Totoalamountpermonth
from collection_master
where year(Date_Time) = YEAR(CURRENT_DATE())
group by AgentID
) total_month
ON just_agent.AgentID = total_month.AgentID
ORDER BY total_month.Totoalamountpermonth DESC
) Sub1
CROSS JOIN (SELECT #Position1:=0) Sub2
) t1
JOIN
(
SELECT AgentID,
current_date_amount,
date_range_amount,
month_amount,
#Position2:=#Position2 + 1 AS `Rank`
FROM
(
SELECT just_agent.AgentID,
total_current_date.Totoalamountperday AS current_date_amount,
total_date_range.Totoalamountperday AS date_range_amount,
total_month.Totoalamountpermonth AS month_amount
FROM
(
SELECT DISTINCT AgentID
FROM collection_master
) just_agent
LEFT OUTER JOIN
(
select AgentID, SUM(AmountRecevied) as Totoalamountperday
from collection_master
where day(Date_Time) = day(CURRENT_DATE())
group by AgentID
) total_current_date
ON just_agent.AgentID = total_current_date.AgentID
LEFT OUTER JOIN
(
select AgentID, sum(AmountRecevied) as Totoalamountperday
from collection_master
where date_time between DATE_FORMAT(NOW(), '%Y-%m-01') and LAST_DAY(now() - interval 0 month)
group by AgentID
) total_date_range
ON just_agent.AgentID = total_date_range.AgentID
LEFT OUTER JOIN
(
select AgentID, sum(AmountRecevied) as Totoalamountpermonth
from collection_master
where year(Date_Time) = YEAR(CURRENT_DATE())
group by AgentID
) total_month
ON just_agent.AgentID = total_month.AgentID
ORDER BY total_month.Totoalamountpermonth DESC
) Sub1
CROSS JOIN (SELECT #Position2:=0) Sub2
) t2
ON ABS(t1.`Rank` - t2.`Rank`) <= 1.5
WHERE t1.AgentID = 2

Select in select - Every derived table must have its own alias error

I'm trying to get from database information about orders grouped by date.
I have table sales_flat_order, where I have it's id, order creation date, total_paid for order, and order item count. And I have table sales_flat_order_item where are orders items with it prices.
I created script to get order information by day:
SELECT
DATE( sales_flat_order.created_at ) AS date,
SUM( sales_flat_order.total_paid ) AS sales,
SUM( sales_flat_order.total_item_count ) AS items
FROM
sales_flat_order,
sales_flat_order_payment
WHERE
sales_flat_order.status = 'complete'
AND sales_flat_order.entity_id = sales_flat_order_payment.parent_id
AND sales_flat_order_payment.method = 'checkmo'
GROUP BY DATE( sales_flat_order.created_at )
WITH ROLLUP
I get:
DATE SALES ITEMS
2013-03-05 72 3
2013-03-06 100 5
And I have script to count median price:
SELECT
avg(t1.price) as median_val
FROM
(
SELECT
#rownum:=#rownum+1 as `row_number`,
d.price
FROM
sales_flat_order_item d,
(SELECT #rownum:=0) r
WHERE 1
ORDER BY d.price
) as t1,
(
SELECT
count(*) as total_rows
FROM
sales_flat_order_item d
WHERE 1
) as t2
WHERE 1
AND t1.row_number>=total_rows/2
and t1.row_number<=total_rows/2+1;
Now I'm trying to combine this two script to get:
DATE SALES ITEMS median_item_price
2013-03-05 72 3 19
2013-03-06 100 5 10.5
Combined script:
SELECT
DATE( sales_flat_order.created_at ) AS date,
SUM( sales_flat_order.total_paid ) AS sales,
SUM( sales_flat_order.total_item_count ) AS items,
sales_flat_order_item.price as median_item_price
FROM
sales_flat_order,
sales_flat_order_payment,
(
SELECT
avg(t1.price) as median_val
FROM
(
SELECT
#rownum:=#rownum+1 as `row_number`,
d.price
FROM
sales_flat_order_item d,
(SELECT #rownum:=0) r
WHERE 1
ORDER BY d.price
) as t1,
(
SELECT
count(*) as total_rows
FROM
sales_flat_order_item d
WHERE 1
) as t2
WHERE 1
AND t1.row_number>=total_rows/2
and t1.row_number<=total_rows/2+1
) as sales_flat_order_item
WHERE
sales_flat_order.status = 'complete'
AND sales_flat_order.entity_id = sales_flat_order_payment.parent_id
AND sales_flat_order_payment.method = 'checkmo'
AND DATE(sales_flat_order_item.created_at) = DATE(sales_flat_order.created_at)
GROUP BY DATE( sales_flat_order.created_at )
WITH ROLLUP
and get error: #1248 - Every derived table must have its own alias
here is database: http://sqlfiddle.com/#!2/7dfec
Can anyone help?
Solution:
SELECT
DATE( sales_flat_order.created_at ) AS date,
SUM( sales_flat_order.total_paid ) AS sales,
SUM( sales_flat_order.total_item_count ) AS items,
MAX( median.median_val ) as median_item_price
FROM
sales_flat_order,
sales_flat_order_payment,
(
SELECT DATE(sq.created_at) as median_date, avg(sq.price) as median_val FROM (
SELECT t1.row_number, t1.price, t1.created_at FROM(
SELECT IF(#prev!=d.created_at, #rownum:=1, #rownum:=#rownum+1) as `row_number`, d.price, #prev:=d.created_at AS created_at
FROM sales_flat_order_item d, (SELECT #rownum:=0, #prev:=NULL) r
ORDER BY d.price
) as t1 INNER JOIN
(
SELECT count(*) as total_rows, created_at
FROM sales_flat_order_item d
GROUP BY created_at
) as t2
ON t1.created_at = t2.created_at
WHERE 1=1
AND t1.row_number>=t2.total_rows/2 and t1.row_number<=t2.total_rows/2+1
)sq
group by DATE(sq.created_at)
) as median
WHERE
sales_flat_order.status = 'complete'
AND sales_flat_order.entity_id = sales_flat_order_payment.parent_id
AND sales_flat_order_payment.method = 'checkmo'
AND median.median_date = DATE( sales_flat_order.created_at )
GROUP BY DATE( sales_flat_order.created_at )
WITH ROLLUP

MYSQL Double Nest SELECT

I have a 'double' nested SELECT statement. The deeper statement pulls the values I need for the per month total I am trying to insert into an existing row. However each "Segment' has differing percentages so it results in multiple rows. To get around this I have an outer select that then does a SUM the result.
More problem is I am unable to match the result to the month as the when I try to link to the columns name 'Enrol_month_short' I get the following message.
"Unknown column 'Enrol_month_short' in 'where clause'"
SELECT Date_format(tbl_client_details.cd_enrol_date, '%b %y')AS
Enrol_month_short, Sum(tbl_revenue.rev_value) AS
Enrol_Cum_Rev, (SELECT Sum(tbl_revenue.rev_value)
FROM tbl_client_details
LEFT OUTER JOIN gridlock.tbl_revenue
ON tbl_client_details.cd_zbi_no = tbl_revenue.rev_zbi_no
AND tbl_client_details.cd_ucn = tbl_revenue.rev_ucn
WHERE ( Date_format(tbl_client_details.cd_enrol_date, '%b %y') =
enrol_month_short )
AND ( Date_format(rev_month, '%b %y') = enrol_month_short )
GROUP BY Date_format(tbl_client_details.cd_enrol_date, '%b %y')) AS
New_client_rev,
(SELECT Count(tbl_client_details.cd_id)
FROM tbl_client_details
WHERE ( tbl_client_details.cd_enrol_date >= '2012-04-01'
AND tbl_client_details.cd_enrol_date <= '2012-07-31' )
AND Date_format(tbl_client_details.cd_enrol_date, '%b %y') =
enrol_month_short
GROUP BY Date_format(tbl_client_details.cd_enrol_date, '%b %y')) AS
Actual_Enrols,
(SELECT ( ( Sum(tbl_revenue.rev_value) *
( tbl_segment.seg_growth_2012 / 100 ) ) / 12 ) AS YEAR_NEW_BUS_BUDGET
FROM tbl_client_details
LEFT OUTER JOIN tbl_revenue
ON tbl_client_details.cd_zbi_no = tbl_revenue.rev_zbi_no
AND tbl_client_details.cd_ucn = tbl_revenue.rev_ucn
LEFT OUTER JOIN tbl_segment
ON tbl_client_details.cd_segment = tbl_segment.seg_name
WHERE ( rev_month >= '2012-04-01' AND rev_month <= '2012-07-31' )
GROUP BY enrol_month_short) AS
YEAR_NEW_BUS_BUDGET
FROM tbl_client_details
LEFT OUTER JOIN gridlock.tbl_revenue
ON tbl_client_details.cd_zbi_no = tbl_revenue.rev_zbi_no
AND tbl_client_details.cd_ucn = tbl_revenue.rev_ucn
WHERE ( tbl_client_details.cd_enrol_date >= '2012-04-01'
AND tbl_client_details.cd_enrol_date <= '2012-07-31' )
AND ( rev_month >= '2012-04-01'
AND rev_month <= '2012-07-31'
OR rev_month = NULL )
GROUP BY enrol_month_short
ORDER BY tbl_client_details.cd_enrol_date;