I am trying to do an sql query that will help me to acheive the following result:
----------------------------------------------------------------------------
| RowNum | email | point_1 | point_2 | total_point |
----------------------------------------------------------------------------
| 1 | abc@gmail.com | 120 | 70 | 190 |
----------------------------------------------------------------------------
Sql query statement 1 (to get the value of RowNum, email and point_1):
$sql = "
select *
from
(
select ROW_NUMBER() OVER (ORDER BY m.first_name) as **RowNum**,
ltrim(rtrim(m.email_addr)) AS **email**,
CAST(isnull(p.points_accumulated,'0') AS INT) AS **point_1**
FROM (select * from crm_member_list where coy_id='HSG' and mbr_id not in (select mbr_id from o2o_tmp_mbr_issues_exclude) ) m
left join (select * from crm_member_points where coy_id='HSG') p
on p.mbr_id = m.mbr_id
where m.email_addr = 'abc@gmail.com'
and m.date BETWEEN '2016-08-01 00:00:00' AND '2016-08-31 23:59:00'
)sub where RowNum>? and RowNum<? order by RowNum";
Sql query statement 2 (to get the value of point_2):
$sql = "
select CAST(isnull(p.points_accumulated,'0') AS INT) AS **point_2**
FROM (select * from crm_member_list where coy_id='HSG' and mbr_id not in (select mbr_id from o2o_tmp_mbr_issues_exclude) ) m
left join (select * from crm_member_points where coy_id='HSG') p
on p.mbr_id = m.mbr_id
where m.email_addr = 'abc@gmail.com'
and m.date BETWEEN '2016-09-01 00:00:00' AND '2016-09-30 23:59:00'";
I tried to combine the 2 statements shown above to get the result, but I am getting the error of
"execute sql directly, no cursor".
Combined code:
$sql = "
select *
from
(
(select ROW_NUMBER() OVER (ORDER BY m.first_name) as **RowNum**,
ltrim(rtrim(m.email_addr)) AS **email**,
CAST(isnull(p.points_accumulated,'0') AS INT) AS **point_1**
FROM (select * from crm_member_list where coy_id='HSG' and mbr_id not in (select mbr_id from o2o_tmp_mbr_issues_exclude) ) m
left join (select * from crm_member_points where coy_id='HSG') p
on p.mbr_id = m.mbr_id
where m.email_addr = 'abc@gmail.com'
and m.date BETWEEN '2016-08-01 00:00:00' AND '2016-08-31 23:59:00'),
(select CAST(isnull(p.points_accumulated,'0') AS INT) AS **point_2**
FROM (select * from crm_member_list where coy_id='HSG' and mbr_id not in (select mbr_id from o2o_tmp_mbr_issues_exclude) ) m
left join (select * from crm_member_points where coy_id='HSG') p
on p.mbr_id = m.mbr_id
where m.email_addr = 'abc@gmail.com'
and m.date BETWEEN '2016-09-01 00:00:00' AND '2016-09-30 23:59:00'
)
)sub where RowNum>? and RowNum<? order by RowNum";
How should I go about combining both of the query statements to generate the result shown above? And how do I add the column of point_1 and point_2 to get the total_point column?
Thanks in advance
If I'm understanding your question correctly (and assuming you aren't using mysql
as it doesn't support row_number
), one approach would be to use conditional aggregation
. I also think you are actually looking to sum
the points for each month versus selecting 1 from each month:
select *, row_number() over (order by first_name) rn
from (
select
m.first_name,
ltrim(rtrim(m.email_addr)) AS email,
sum(case when m.date >= '2016-08-01' AND m.Date < '2016-09-01'
then CAST(isnull(p.points_accumulated,'0') AS INT)
end
) as point_1,
sum(case when m.date >= '2016-09-01' AND m.Date < '2016-10-01'
then CAST(isnull(p.points_accumulated,'0') AS INT)
end
) as point_2,
sum(CAST(isnull(p.points_accumulated,'0') AS INT))
from crm_member_list m
left join crm_member_points p on m.coy_id = p.coy_id
and p.mbr_id = m.mbr_id
where m.coy_id = 'HSG'
and m.mbr_id not in (select mbr_id from o2o_tmp_mbr_issues_exclude)
and m.date >= '2016-08-01' AND m.date < '2016-10-01'
group by 1, 2
) t
Please note I've changed your between
statement to use <
and >=
. Less chance for issues with this approach.
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.