sql two order by give different result - mysql

I have this sql query below that has 2 order by my dates are
02/01/2016 - 03/01/2017
02/03/2011 - 07/07/2016
12/22/2010 - 07/07/2016
02/01/2016 - 02/01/2016
12/22/2010 - 07/07/2013
This is the result when i use the query below. But the problem is it is not in order in what i expect for. I want to order first the DateEnd in desc order and then the DateStarted.
Select top 5
Case When dbo.VoluntaryWork.Organization + ' - ' + dbo.VoluntaryWork.OrganizationAddress = ' - '
Then 'N/A'
Else dbo.VoluntaryWork.Organization + ' - ' + dbo.VoluntaryWork.OrganizationAddress end AS OrgAddress,
Case When isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') = '' or isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') = '01/01/1900' then 'N/A' else isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') end AS DateStarted,
Case When isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') = '' or isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') = '01/01/1900' then 'N/A' else isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') end AS DateEnded
From dbo.PersonVoluntaryWork
Inner Join dbo.VoluntaryWork ON dbo.PersonVoluntaryWork.VoluntaryWorksId = dbo.VoluntaryWork.VoluntaryWorksId
Where (dbo.PersonVoluntaryWork.PersonId = #PersonId)
Order By dbo.VoluntaryWork.DateEnded desc ,dbo.VoluntaryWork.DateStarted desc

Hi use another select clause from outside your main query and then apply order by clause like this.. I hope this will work.
SELECT * from (
SELECT top 5 case when dbo.VoluntaryWork.Organization + ' - ' + dbo.VoluntaryWork.OrganizationAddress = ' - ' then 'N/A'
else dbo.VoluntaryWork.Organization + ' - ' + dbo.VoluntaryWork.OrganizationAddress end AS OrgAddress,
case when isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') = '' or isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') = '01/01/1900' then 'N/A' else isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateStarted, 101),'') end AS DateStarted,
case when isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') = '' or isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') = '01/01/1900' then 'N/A' else isnull(CONVERT(varchar(10), dbo.VoluntaryWork.DateEnded, 101),'') end AS DateEnded,
FROM dbo.PersonVoluntaryWork INNER JOIN
dbo.VoluntaryWork ON dbo.PersonVoluntaryWork.VoluntaryWorksId = dbo.VoluntaryWork.VoluntaryWorksId
WHERE (dbo.PersonVoluntaryWork.PersonId = #PersonId)
ORDER BY dbo.VoluntaryWork.DateEnded DESC
) A ORDER BY A.DateStarted

Related

EMBEDDED DATEDIFF EXCLUD WEEKENDS + CASE STATEMENT

I have the below query that utilizes a case statement. I would like to datediff two dates but exclude weekend days.
I have the below that excutes but now I would like to exclude Sat and Sunday from this... AND DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) <= 2
CASE WHEN
S.Name IN ('Assessment','Survey')
AND A.ALERT_DESC = 'ER'
AND CAST(A.ALERTS_CREATE_DT AS DATE) <= CAST(S.CreatedDate AS DATE)
AND DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) <= 2 /*EXCLUDE Sat and Sunday from the calculation*/
Full Query
SELECT
CASE WHEN
S.Name IN ('Assessment','Survey')
AND A.ALERT_DESC = 'ER'
AND CAST(A.ALERTS_CREATE_DT AS DATE) <= CAST(S.CreatedDate AS DATE)
AND
( DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) <= 2 /*Business Days*/
--DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate) + 1
---(DATEDIFF(WK,A.ALERTS_CREATE_DT,S.CreatedDate) * 2)
---(CASE WHEN DATENAME(DW,A.ALERTS_CREATE_DT) = 'SUNDAY' THEN 1 ELSE 0 END)
---(CASE WHEN DATENAME(DW,S.CreatedDate) = 'SATURADAY' THEN 1 ELSE 0 END)
)
THEN 'Y'
WHEN A.ALERT_DESC = 'model' OR S.CreatedDate IS NULL OR S.Name = 'ER'
THEN ''
ELSE 'N'
END 'Count towards Alerts'
FROM A
FULL S ON A.id= S.id
WHERE 1=1
This should give you the required result by excluding the Saturdays and Sundays.
SELECT A.ALERT_DESC,A.ALERTS_CREATE_DT,S.Name,S.CreatedDate,
CASE WHEN
S.Name IN ('Assessment','Survey') AND A.ALERT_DESC = 'ER'
AND CAST(A.ALERTS_CREATE_DT AS DATE) <= CAST(S.CreatedDate AS DATE)
AND
(( DATEDIFF(DD,A.ALERTS_CREATE_DT,S.CreatedDate)+1
- (datediff(wk,A.ALERTS_CREATE_DT,S.CreatedDate)*2)
- case when datepart(dw,A.ALERTS_CREATE_DT)=1 then 1 else 0 end
- case when datepart(dw,S.CreatedDate)=7 then 1 else 0 end
)) <=2 THEN 'Y'
WHEN A.ALERT_DESC = 'model' OR S.CreatedDate IS NULL OR S.Name = 'ER' THEN ''
ELSE 'N' END 'Count towards Alerts'
FROM A,S

CASE statement not updating Temp Table

In the UPDATE statement below, the InspectionChg, MileageChg, FuelChg and FreightChg columns are not updating to temp table. Goal is to aggregate charges. I have tried several variations and I cannot get Temp Table Columns to update.
UPDATE #TTable
SET ChargeCode = id.cht_itemcode,
InspectionChg = CASE WHEN LEFT(id.cht_itemcode, 3) = 'INS' THEN
InspectionChg+id.ivd_charge ELSE InspectionChg+0 END,
MileageChg = CASE WHEN LEFT(id.cht_itemcode, 4) = 'MILE' THEN MileageChg+id.ivd_charge
ELSE MileageChg+0 END,
FuelChg = CASE WHEN LEFT(id.cht_itemcode, 4) = 'FUEL' THEN
FuelChg+id.ivd_charge ELSE FuelChg+0 END,
FreightChg = CASE WHEN LEFT(id.cht_itemcode, 2) = 'LH' THEN FreightChg+id.ivd_charge
ELSE FreightChg+0 END,
Rate = 1
FROM #TTable
INNER JOIN invoicedetail as id
on #TTable.OrderNumber = id.Ord_hdrnumber
What is wrong?
UPDATE TT
SET TT.ChargeCode = id.cht_itemcode ,
TT.InspectionChg = ( CASE WHEN LEFT(id.cht_itemcode, 3) = 'INS'
THEN InspectionChg + id.ivd_charge
ELSE InspectionChg + 0
END ) ,
TT.MileageChg = ( CASE WHEN LEFT(id.cht_itemcode, 4) = 'MILE'
THEN MileageChg + id.ivd_charge
ELSE MileageChg + 0
END ) ,
TT.FuelChg = ( CASE WHEN LEFT(id.cht_itemcode, 4) = 'FUEL'
THEN FuelChg + id.ivd_charge
ELSE FuelChg + 0
END ) ,
TT.FreightChg = ( CASE WHEN LEFT(id.cht_itemcode, 2) = 'LH'
THEN FreightChg + id.ivd_charge
ELSE FreightChg + 0
END ) ,
TT.Rate = 1
FROM #TTable AS TT
INNER JOIN invoicedetail AS id ON TT.OrderNumber = id.Ord_hdrnumber

Subquery returns more than 1 row sql query

I had such an error can explain what I did wrong?
I only add this sql query:
(? = (select travel_region_id from relationships where travel_id = travels.id))
error
ActiveRecord::StatementInvalid (Mysql::Error: Subquery returns more than 1 row: select count(*) from travel_start_days, cars, travels
where travels.id = travel_start_days.travel_id and travels.id = travel.car_id and travel_start_days.day > adddate(curdate(), interval '2' day) and (7 = (select travel_region_id from relationships where travel_id = travels.id)) and '2013-08-16' <= travel_start_days.day):
Update Whis is method create query
def conditions
where = ''
param = []
if #region && #region != ''
where += 'travels.region_id = ?'
param += [#region.to_i]
end
if #car && #car != ''
where += ' and ' if where != ''
where += 'cars.id = ?'
param += [#car.to_i]
end
if #relation && #relation != ''
where += ' and ' if where != ''
where += '(? = (select travel_region_id from relationships where travel_id = travels.id))'
param += [#relation.to_i]
end
if #start_port && #start_port != ''
where += ' and ' if where != ''
where += '(? = (select location_id from travel_days where travel_id = travel_start_days.travel_id and day_no = 1 order by arrival asc limit 1))'
param += [#start_port.to_i]
end
return where == '' ? nil : ["travel_start_days.day > adddate(curdate(), interval ? day) and " + where] + [#criteria[5]] + param
end
The issue in this part of conditions:
(7 = (select travel_region_id from relationships where travel_id = travels.id))
Obviously this subquery returns more than one travel_region_id just replace = with IN
(7 IN (select travel_region_id from relationships where travel_id = travels.id))
If you are getting more than 1 row from subquery, then add group by clause to your subquery
SELECT travel_region_id FROM relationships
WHERE travel_id = travels.id
GROUP BY travel_region_id

nested mysql case when

SELECT ( CASE
WHEN 1944.0000 >= country_from_price
AND 1944.0000 <= country_to_price
THEN ((1944.0000 + ((1944.0000 * country_percentage)/100)))
ELSE 1944.0000
END
)
FROM `country_markup`
WHERE estatus = '1'
AND country_id REGEXP '[[:<:]]138[[:>:]]'
Above query return value increased by percent but if condition is not full fill than it return NULL. What i want is if query return NULL value than it should return 1944.0000
and For this i have tried below code but did not get any success
SELECT IF( (SELECT (CASE
WHEN 1944.0000 >= country_from_price
AND 1944.0000 <= country_to_price
THEN ((1944.0000 + ((1944.0000 * country_percentage)/100)))
ELSE 1944.0000
END
) as f1
FROM `country_markup`
WHERE estatus = '1'
AND country_id REGEXP '[[:<:]]223[[:>:]]') IS NOT NULL, f1 ,1944.0000) as final_price
Thanks in advance
This appears to work with COALESCE (assuming your original query can only return a single record):
SELECT COALESCE((
SELECT
CASE WHEN 1944.0000 >= country_from_price AND 1944.0000 <= country_to_price
THEN ((1944.0000 + ((1944.0000 * country_percentage)/100)))
ELSE 1944.0000
END
FROM `country_markup`
WHERE estatus = '1'
AND country_id REGEXP '[[:<:]]138[[:>:]]'), 1944.0000) final_price
SQL Fiddle Demo
You can add LIMIT 1 to your subquery to make sure it only returns a single record as well if needed.
Here's an alternative using the MAX aggregate with COALESCE:
SELECT
COALESCE(MAX( CASE WHEN 1944.0000 >= country_from_price AND 1944.0000 <= country_to_price
THEN ((1944.0000 + ((1944.0000 * country_percentage)/100)))
ELSE 1944.0000
END ),1944.0000) final_price
FROM `country_markup`
WHERE estatus = '1'
AND country_id REGEXP '[[:<:]]138[[:>:]]'
More Fiddle
You just need to test that the country_percentage is not null in your expression:
SELECT (CASE WHEN 1944.0000 between country_from_price AND country_to_price and
country_percentage is not null
THEN ((1944.0000 + ((1944.0000 * country_percentage)/100)))
ELSE 1944.0000
END)
FROM `country_markup`
WHERE estatus = '1' AND country_id REGEXP '[[:<:]]138[[:>:]]';
I also changed the comparison logic to use between.

Mysql Stored Proc Call Error

I have a stored procedure that is throwing an error.
Here is the call:
CALL campaign_strat_placement_test ('10541','2013-1-1','2013-1-7',2,0,2,0,0,32)
I have bolded the part that is causing the error because if I change that option to a 0 or 1 the procedure runs.
Here is the entire procedure:
DELIMITER $$
USE `reporting`$$
DROP PROCEDURE IF EXISTS `campaign_strat_placement_test`$$
CREATE DEFINER=`username`#`%` PROCEDURE `campaign_strat_placement_test`(strat_id VARCHAR(255),rpt_start_date DATE, rpt_end_date DATE,mrt_opt INT, mrt_rev_opt INT, mro_opt INT, mro_rev_opt INT, media_cost_opt INT, metrics_opt INT)
COMMENT 'returns client report'
BEGIN
SELECT report_title FROM reporting.campaign_meta_cron_daily WHERE strategy_id = strat_id;
SELECT CONCAT('Data Updated Through ',DATE_FORMAT(ADDDATE(CURDATE(),-1),'%M %d, %Y'));
SET #report_code:=CONCAT('
select date(date) as `Date`
, placement_id
, placement_name
, strategy_name as strategy
, concept
, size
, exchange
, targeting_strat
, ifnull(IMPS_3P,0) as Imps
, ifnull(CLICKS_3P,0) as Clicks
',CASE WHEN mrt_opt = 1 THEN
' ,ifnull(MRT_PC_3P,0) as MRT_PC'
WHEN mrt_opt = 2 THEN
' ,ifnull(MRT_PC_3P,0) as MRT_PC
,ifnull(MRT_PV_3P*goal1_pv_discount,0) as MRT_PV'
WHEN mrt_opt = 3 THEN
' ,ifnull(MRT_PC_3P+(MRT_PV_3P*goal1_pv_discount),0) as MRT_Tot'
ELSE '' END
,CASE WHEN mrt_rev_opt = 1 THEN
' ,ifnull(round(sum(MRT_PC_REV_3P),2),0) as MRT_PC_Rev'
WHEN mrt_rev_opt = 2 THEN
' ,ifnull(MRT_PC_REV_3P,0) as MRT_PC_Rev
,ifnull(MRT_PV_REV_3P*goal1_pv_discount,0) as MRT_PV_Rev'
WHEN mrt_rev_opt = 3 THEN
' ,ifnull(MRT_PC_REV_3P+(MRT_PV_REV_3P*goal1_pv_discount),0) as MRT_Rev'
ELSE '' END
,CASE WHEN mro_opt = 1 THEN
' ,ifnull(MRO_PC_3P,0) as MRO_PC'
WHEN mro_opt = 2 THEN
' ,ifnull(MRO_PC_3P,0) as MRO_PC
,ifnull(MRO_PV_3P*goal1_pv_discount,0) as MRO_PV'
WHEN mro_opt = 3 THEN
' ,ifnull((MRO_PC_3P+(MRO_PV_3P*goal1_pv_discount)),0) as MRO_Tot'
ELSE '' END
,CASE WHEN mro_rev_opt = 1 THEN
' ,ifnull(round(MRO_PC_REV_3P,2),0) as MRO_PC_Rev'
WHEN mro_rev_opt = 2 THEN
' ,ifnull(round(MRO_PC_REV_3P,2),0) as MRO_PC_Rev
,ifnull(round(MRO_PV_REV_3P,2),0) as MRO_PV_Rev'
WHEN mro_rev_opt = 3 THEN
' ,ifnull(round(MRO_PC_REV_3P+sum(MRO_PV_REV_3P*goal1_pv_discount),2),0) as MRO_Rev'
ELSE '' END
,CASE WHEN media_cost_opt = 1 THEN
' ,ifnull(round(gross_media_cost,2),0) as Media_Cost'
WHEN media_cost_opt = 2 THEN
' ,ifnull(round(net_media_cost,2),0) as Net_Media_Cost
,ifnull(round(gross_media_cost,2),0) as Gross_Media_Cost'
ELSE ' ,ifnull(round(net_media_cost,2),0) as Media_Cost' END
,CASE WHEN metrics_opt%2 >= 1 THEN
' ,round(ifnull(CLICKS_3P/IMPS_3P,0),4) as CTR' ELSE '' END
,CASE WHEN metrics_opt%4 >= 2 THEN
' ,round(ifnull((MRT_PC_3P+(sum(MRT_PV_3P)*goal1_pv_discount))/IMPS_3P*1000,0),4) as RR_per_M' ELSE '' END
,CASE WHEN metrics_opt%8 >= 4 THEN
' ,round(ifnull((net_media_cost/IMPS_3P)*1000,0),2) as Net_CPM' ELSE '' END
,CASE WHEN metrics_opt%16 >= 8 THEN
' ,round(ifnull(net_media_cost/CLICKS_3P,0),2) as Net_CPC' ELSE '' END
,CASE WHEN metrics_opt%32 >= 16 THEN
' ,round(ifnull(net_media_cost/(MRT_PC_3P+(MRT_PV_3P*goal1_pv_discount)),0),2) as Net_CPA' ELSE '' END
,CASE WHEN metrics_opt%64 >= 32 THEN
' ,round(ifnull((gross_media_cost/IMPS_3P)*1000,0),2) as Gross_CPM' ELSE '' END
,CASE WHEN metrics_opt%128 >= 64 THEN
' ,round(ifnull(gross_media_cost/CLICKS_3P,0),2) as Gross_CPC' ELSE '' END
,CASE WHEN metrics_opt%256 >= 128 THEN
' ,round(ifnull(gross_media_cost/(MRT_PC_3P+(MRT_PV_3P*goal1_pv_discount)),0),2) as Gross_CPA' ELSE '' END
,CASE WHEN metrics_opt%512 >= 256 THEN
' ,round(ifnull(((MRT_PC_REV_3P+(MRT_PV_REV_3P*goal1_pv_discount))/net_media_cost)-1,0),2) as Net_ROI' ELSE '' END
,CASE WHEN metrics_opt%1024 >= 512 THEN
' ,round(ifnull(((MRT_PC_REV_3P+(MRT_PV_REV_3P*goal1_pv_discount))/gross_media_cost)-1,0),2) as Gross_ROI' ELSE '' END
,CASE WHEN metrics_opt%2048 >= 1024 THEN
' ,round(ifnull(net_media_cost/(MRT_PC_3P+(MRT_PV_3P*goal1_pv_discount)),0),2) as Alt_Net_CPA' ELSE '' END
,CASE WHEN metrics_opt%4096 >= 2048 THEN
' ,round(ifnull(gross_media_cost/(MRT_PC_3P+(MRT_PV_3P*goal1_pv_discount)),0),2) as Alt_Gross_CPA' ELSE '' END
,CASE WHEN metrics_opt%8192 >= 4096 THEN
' ,round(ifnull(((MRO_PC_REV_3P+(MRO_PV_REV_3P*goal1_pv_discount))/net_media_cost)-1,0),2) as Alt_Net_ROI' ELSE '' END
,CASE WHEN metrics_opt%16384 >= 8192 THEN
' ,round(ifnull(((MRO_PC_REV_3P+(MRO_PV_REV_3P*goal1_pv_discount))/gross_media_cost)-1,0),2) as Alt_Gross_ROI' ELSE '' END
,CASE WHEN metrics_opt%32768 >= 16384 THEN
' ,round(ifnull((MRO_PC_3P+(sum(MRO_PV_3P)*goal1_pv_discount))/IMPS_3P*1000,0),4) as Alt_RR_per_M' ELSE '' END
,' from external_02.creative
where strategy_id in (',strat_id,')
and date >= ''',rpt_start_date,'''
and date <= ''',rpt_end_date,'''
group by date(date)
,advsym
,strategy_name
,placement_id
,placement_name
,concept
,size
,exchange
,targeting_strat
UNION ALL
select ''Total'' as `Date`
, '''' as placement_id
, '''' as placement_name
, '''' as strategy
, '''' as concept
, '''' as size
, '''' as exchange
, '''' as targeting_strat
, ifnull(sum(IMPS_3P),0) as Imps
, ifnull(sum(CLICKS_3P),0) as Clicks
',CASE WHEN mrt_opt = 1 THEN
' ,ifnull(sum(MRT_PC_3P),0) as MRT_PC'
WHEN mrt_opt = 2 THEN
' ,ifnull(sum(MRT_PC_3P),0) as MRT_PC
,ifnull(sum(MRT_PV_3P*goal1_pv_discount),0) as MRT_PV'
WHEN mrt_opt = 3 THEN
' ,ifnull(sum(MRT_PC_3P)+sum(MRT_PV_3P*goal1_pv_discount),0) as MRT_Tot'
ELSE '' END
,CASE WHEN mrt_rev_opt = 1 THEN
' ,ifnull(sum(MRT_PC_REV_3P),0) as MRT_PC_Rev'
WHEN mrt_rev_opt = 2 THEN
' ,ifnull(sum(MRT_PC_REV_3P),0) as MRT_PC_Rev
,ifnull(sum(MRT_PV_REV_3P*goal1_pv_discount),0) as MRT_PV_Rev'
WHEN mrt_rev_opt = 3 THEN
' ,ifnull(sum(MRT_PC_REV_3P)+sum(MRT_PV_REV_3P*goal1_pv_discount),0) as MRT_Rev'
ELSE '' END
,CASE WHEN mro_opt = 1 THEN
' ,ifnull(sum(MRO_PC_3P),0) as MRO_PC'
WHEN mro_opt = 2 THEN
' ,ifnull(sum(MRO_PC_3P),0),0) as MRO_PC
,ifnull(sum(MRO_PV_3P*goal1_pv_discount),0) as MRO_PV'
WHEN mro_opt = 3 THEN
' ,ifnull(sum(MRO_PC_3P)+sum(MRO_PV_3P*goal1_pv_discount),0) as MRO_Tot'
ELSE '' END
,CASE WHEN mro_rev_opt = 1 THEN
' ,ifnull(round(sum(MRO_PC_REV_3P),2),0) as MRO_PC_Rev'
WHEN mro_rev_opt = 2 THEN
' ,ifnull(sum(MRO_PC_REV_3P),0) as MRO_PC_Rev
,ifnull(sum(MRO_PV_REV_3P*goal1_pv_discount),0) as MRO_PV_Rev'
WHEN mro_rev_opt = 3 THEN
' ,ifnull(round(sum(MRO_PC_REV_3P)+sum(MRO_PV_REV_3P*goal1_pv_discount),2),0) as MRO_Rev'
ELSE '' END
,CASE WHEN media_cost_opt = 1 THEN
' ,ifnull(round(sum(gross_media_cost),2),0) as Media_Cost'
WHEN media_cost_opt = 2 THEN
' ,ifnull(round(sum(net_media_cost),2),0) as Net_Media_Cost
,ifnull(round(sum(gross_media_cost),2),0) as Gross_Media_Cost'
ELSE ' ,ifnull(round(sum(net_media_cost),2),0) as Media_Cost' END
,CASE WHEN metrics_opt%2 >= 1 THEN
' ,round(ifnull(sum(CLICKS_3P)/sum(IMPS_3P),0),4) as CTR' ELSE '' END
,CASE WHEN metrics_opt%4 >= 2 THEN
' ,round(ifnull((sum(MRT_PC_3P)+(sum(MRT_PV_3P)*goal1_pv_discount))/sum(IMPS_3P)*1000,0),4) as RR_per_M' ELSE '' END
,CASE WHEN metrics_opt%8 >= 4 THEN
' ,round(ifnull((sum(net_media_cost)/sum(IMPS_3P))*1000,0),2) as Net_CPM' ELSE '' END
,CASE WHEN metrics_opt%16 >= 8 THEN
' ,round(ifnull(sum(net_media_cost)/sum(CLICKS_3P),0),2) as Net_CPC' ELSE '' END
,CASE WHEN metrics_opt%32 >= 16 THEN
' ,round(ifnull((sum(net_media_cost))/((sum(MRT_PC_3P))+(sum(MRT_PV_3P)*goal1_pv_discount)),0),2) as Net_CPA' ELSE '' END
,CASE WHEN metrics_opt%64 >= 32 THEN
' ,round(ifnull((sum(gross_media_cost)/sum(IMPS_3P))*1000,0),2) as Gross_CPM' ELSE '' END
,CASE WHEN metrics_opt%128 >= 64 THEN
' ,round(ifnull(sum(gross_media_cost)/sum(CLICKS_3P),0),2) as Gross_CPC' ELSE '' END
,CASE WHEN metrics_opt%256 >= 128 THEN
' ,round(ifnull((sum(gross_media_cost))/((sum(MRT_PC_3P))+(sum(MRT_PV_3P)*goal1_pv_discount)),0),2) as Gross_CPA' ELSE '' END
,CASE WHEN metrics_opt%512 >= 256 THEN
' ,round(ifnull(((sum(MRT_PC_REV_3P)+(sum(MRT_PV_REV_3P)*goal1_pv_discount))/sum(net_media_cost))-1,0),2) as Net_ROI' ELSE '' END
,CASE WHEN metrics_opt%1024 >= 512 THEN
' ,round(ifnull(((sum(MRT_PC_REV_3P)+(sum(MRT_PV_REV_3P)*goal1_pv_discount))/sum(gross_media_cost))-1,0),2) as Gross_ROI' ELSE '' END
,CASE WHEN metrics_opt%2048 >= 1024 THEN
' ,round(ifnull((sum(net_media_cost))/((sum(MRT_PC_3P))+(sum(MRT_PV_3P)*goal1_pv_discount)),0),2) as Alt_Net_CPA' ELSE '' END
,CASE WHEN metrics_opt%4096 >= 2048 THEN
' ,round(ifnull((sum(gross_media_cost))/((sum(MRT_PC_3P))+(sum(MRT_PV_3P)*goal1_pv_discount)),0),2) as Alt_Gross_CPA' ELSE '' END
,CASE WHEN metrics_opt%8192 >= 4096 THEN
' ,round(ifnull(((sum(MRO_PC_REV_3P)+(sum(MRO_PV_REV_3P)*goal1_pv_discount))/sum(net_media_cost))-1,0),2) as Alt_Net_ROI' ELSE '' END
,CASE WHEN metrics_opt%16384 >= 8192 THEN
' ,round(ifnull(((sum(MRO_PC_REV_3P)+(sum(MRO_PV_REV_3P)*goal1_pv_discount))/sum(gross_media_cost))-1,0),2) as Alt_Gross_ROI' ELSE '' END
,CASE WHEN metrics_opt%32768 >= 16384 THEN
' ,round(ifnull((sum(MRO_PC_3P)+(sum(MRO_PV_3P)*goal1_pv_discount))/sum(IMPS_3P)*1000,0),4) as Alt_RR_per_M' ELSE '' END
,' from external_02.creative
where strategy_id in (',strat_id,')
and date >= ''',rpt_start_date,'''
and date <= ''',rpt_end_date,''';');
# Prepares and executes the dynamic SQL statement.
PREPARE ExecStatement FROM #report_code;
EXECUTE ExecStatement;
# Deallocates statement.
DEALLOCATE PREPARE ExecStatement;
# Prepares and executes the dynamic SQL statement.
END$$
DELIMITER ;
Here is the error I am getting:
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near ') as MRO_PC
,ifnull(sum(MRO_PV_3P*goal1_pv_discount),0) as MRO_PV ,ifnull(rou' at line 38
This is confusing since I cannot find that code on line 38...
The error seems to revolve around the WHEN mrt_opt = 2 THEN part of the code. Since (as I said before) If that option is 0 or 1 the procedure works.
I've spent a long time looking at this and would like a fresh pair of eyes.
Thanks!
You've got an extra ,0) in your statement around that code.
Search for and change the following lines:
WHEN mro_opt = 2 THEN
' ,ifnull(sum(MRO_PC_3P),0),0) as MRO_PC
to this:
WHEN mro_opt = 2 THEN
' ,ifnull(sum(MRO_PC_3P),0) as MRO_PC