CASE WHEN with multiple THEN ELSE - mysql

Right now i get for reporting purposes the following data in 3 different columns:
//Number
'CASE WHEN should_be.sh = "50"
THEN
REPLACE(SUBSTRING_INDEX(account.code,".",1)," ","")
ELSE
"500."
END',
// Account
'CASE WHEN should_be.sh = "50"
THEN
REPLACE(
SUBSTRING(SUBSTRING_INDEX(account.code,".",2),
INSTR(account.code, ".") + 1)
," ","")
ELSE
"92145000000"
END',
// Costsnumber
'CASE WHEN should_be.sh = "50"
THEN
CASE WHEN LENGTH(account.code) < 15
THEN
REPLACE(
SUBSTRING_INDEX(account.code,".",-1)
," ","")
ELSE
""
END
ELSE
""
END',
If every condition meets ELSE, then the 3 columns look like that:
+--------+-------------+------------+
| number | account | costnumber |
+--------+-------------+------------+
| 500 | 92145000000 | |
+--------+-------------+------------+
How to combine the 3 columns, so I will have one column like that (for example in the ELSE case:
+-------------------------+
| combined |
+-------------------------+
| 500-92145000000-(empty) |
+-------------------------+

Just use concat_ws():
CONCAT_WS('-',
(CASE WHEN should_be.sh = '50'
THEN REPLACE(SUBSTRING_INDEX(account.code, '.', 1), ' ', '')
ELSE '500.'
END),
(CASE WHEN should_be.sh = '50'
THEN REPLACE(SUBSTRING(SUBSTRING_INDEX(account.code, '.',2),
INSTR(account.code, '.') + 1), ' ', '')
ELSE '92145000000'
END),
(CASE WHEN should_be.sh = '50' AND LENGTH(account.code) < 15
THEN REPLACE(SUBSTRING_INDEX(account.code, '.', -1), ' ', '')
ELSE ''
END)
)
If you want "(empty)" to really appear for the third element, then replace the final ELSE '' with ELSE '(empty)'.

Related

SQL Server order desc and also show 6/6

I have a 3 columns in a table named col1, col2, col3 containing integer values. there are 4000 + rows. there is three parameter #first, #second , #third. I want to get rows who has match according to this parameter. The query is giving results but it is not giving me 3/3 or if there is no match it is not showing and also I want to make it descending order.
My output should be like this
table :
col1 col2 col3
3 4 5
1 2 2
7 7 9
3 2 3
1 8 9
1 2 3
first = 1 second = 2 and third = 3
3 / 3 1 ( as sixth row contains 1, 2, 3)
2 / 3 2 ( as second row and fourth row )
1 / 3 1 (fifth row)
0 / 3 2 (ist , third )
(
SELECT count (CASE WHEN col1 = #First THEN 1 ELSE 0 END +
CASE WHEN col2= #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END
)AS "NUM_OF_MATCHES" ,
CAST( CASE WHEN col1 = #First THEN 1 ELSE 0 END +
CASE WHEN col2= #sec/ond THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END
AS VARCHAR(10)) + '/ 3'
AS "match"
FrOM dbo.FormsDataRowFormat a
where ( SELECT CASE WHEN col1 = #First THEN 1 ELSE 0 END +
CASE WHEN col2= #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END AS "NUM_OF_MATCHES" ) >= 1
group by ( CASE WHEN col1 = #First THEN 1 ELSE 0 END +
CASE WHEN col2 = #second THEN 1 ELSE 0 END+
CASE WHEN col3 = #third THEN 1 ELSE 0 END )
)
SELECT
'0/3' AS Matches,
SUM(CASE WHEN (CASE WHEN col1 = #first THEN 1 ELSE 0 END +
CASE WHEN col2 = #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END) = 0 THEN 1 ELSE 0 END) AS NumMatches
FROM FormsDataRowFormat
UNION
SELECT '1/3' AS Matches,
SUM(CASE WHEN (CASE WHEN col1 = #first THEN 1 ELSE 0 END +
CASE WHEN col2 = #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END) = 1 THEN 1 ELSE 0 END) AS NumMatches
FROM FormsDataRowFormat
UNION
SELECT '2/3' AS Matches,
SUM(CASE WHEN (CASE WHEN col1 = #first THEN 1 ELSE 0 END +
CASE WHEN col2 = #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END) = 2 THEN 1 ELSE 0 END) AS NumMatches
FROM FormsDataRowFormat
UNION
SELECT '3/3' AS Matches,
SUM(CASE WHEN (CASE WHEN col1 = #first THEN 1 ELSE 0 END +
CASE WHEN col2 = #second THEN 1 ELSE 0 END +
CASE WHEN col3 = #third THEN 1 ELSE 0 END) = 3 THEN 1 ELSE 0 END) AS NumMatches
FROM FormsDataRowFormat
For input data:
/--------------------\
| col1 | col2 | col3 |
|------+------+------|
| 8 | 7 | 1 |
| 5 | 7 | 1 |
| 4 | 4 | 4 |
| 4 | 5 | 2 |
| 4 | 6 | 2 |
| 2 | 2 | 3 |
| 5 | 2 | 1 |
| 2 | 7 | 7 |
| 2 | 1 | 3 |
| 1 | 2 | 3 |
\--------------------/
with:
#first = 1
#second = 2
#third = 3
gives output:
/----------------------\
| Matches | NumMatches |
|---------+------------|
| 0/3 | 6 |
| 1/3 | 2 |
| 2/3 | 1 |
| 3/3 | 1 |
\----------------------/
Edit - Dynamic SQL solution:
Following your comment clarifying that the number of columns is dynamic (between 3 and 6 inclusive), below is a dynamic SQL solution which will handle any such number of columns.
Note however that dynamic SQL is probably not the best solution to this problem, especially if it's something which will be repeated many times and/or runs on very large datasets. However, it's the only way I know to achieve this, so I wanted to include it (and would be very happy to see better solutions from other users!).
DECLARE #numColumns INT
SELECT #numColumns = COUNT(*) FROM sysobjects o JOIN syscolumns c ON o.id = c.id WHERE o.name = 'FormsDataRowFormat'
DECLARE #sql NVARCHAR(MAX)
SET #sql =
'SELECT ' +
'''0/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 0 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' +
'UNION ' +
'SELECT ''1/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 1 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' +
'UNION ' +
'SELECT ''2/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 2 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' +
'UNION ' +
'SELECT ''3/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 3 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' +
CASE WHEN #numColumns >= 4 THEN
'UNION ' +
'SELECT ''4/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 4 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN
'UNION ' +
'SELECT ''5/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 5 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat ' ELSE '' END +
CASE WHEN #numColumns = 6 THEN
'UNION ' +
'SELECT ''6/' + CONVERT(VARCHAR,#numColumns) + ''' AS Matches, ' +
'SUM(CASE WHEN (CASE WHEN col1 = ' + CONVERT(VARCHAR,#first) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col2 = ' + CONVERT(VARCHAR,#second) + ' THEN 1 ELSE 0 END + ' +
'CASE WHEN col3 = ' + CONVERT(VARCHAR,#third) + ' THEN 1 ELSE 0 END' +
CASE WHEN #numColumns >= 4 THEN ' + CASE WHEN col4 = ' + CONVERT(VARCHAR,#fourth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns >= 5 THEN ' + CASE WHEN col5 = ' + CONVERT(VARCHAR,#fifth) + ' THEN 1 ELSE 0 END' ELSE '' END +
CASE WHEN #numColumns = 6 THEN ' + CASE WHEN col6 = ' + CONVERT(VARCHAR,#sixth) + ' THEN 1 ELSE 0 END' ELSE '' END +
') = 6 THEN 1 ELSE 0 END) AS NumMatches ' +
'FROM FormsDataRowFormat' ELSE '' END
EXEC sp_executesql #sql

Flattening a Table in prep for Json

Maybe it's that I'm tired but this is escaping me.
Let's say that I want to flatten this table:
a_id a_val b_id b_val c_id c_val d_id d_val
1 a 10 b 100 c 1000 f
1 a 20 d 200 g null null
2 e 30 h 300 i null null
2 j 40 k null null null null
3 l null null null null null null
Into this query result:
id mystring
1, (1:a,10:b,100:c,1000:f),(1:a,20:d,200:g)
2, (2:e,30:h,300:i),(2:j,40:k)
3, (3:l)
The table only renders four levels deep (a, b, c, d) so no dynamic sql issue.
Now I'd usually just use GROUP_CONCAT(CONCAT(...)) but that won't work with the Nulls present. And maybe using coalesce somehow will solve this but... I feel pretty stupid at the moment... and I can't figure it out.
Unfortunately I can't use mysql json services on this installation so I need to construct the data. thanks.
The solution here will probably just be a combination of clever concatenation and IFNULL calls. My shot in the dark:
SELECT a_id, GROUP_CONCAT(CONCAT('(',
a_id, ':', a_value, ',',
IFNULL(b_id, ''), IF(b_id IS NOT NULL, ':', ''), IFNULL(b_val, ''),
...repeat for c and d
')'
) SEPARATOR ',')
FROM table
GROUP BY a_id;
select a_id as id,
group_concat(concat(
case isnull(a_id) when true then '' else '(' end,
coalesce(a_id, ''),
case isnull(a_id) when true then '' else ':' end,
coalesce(a_val, ''),
case isnull(b_id) when true then '' else ',' end,
coalesce(b_id, ''),
case isnull(b_id) when true then '' else ':' end,
coalesce(b_val, ''),
case isnull(c_id) when true then '' else ',' end,
coalesce(c_id, ''),
case isnull(c_id) when true then '' else ':' end,
coalesce(c_val, ''),
case isnull(d_id) when true then '' else ',' end,
coalesce(d_id, ''),
case isnull(d_id) when true then '' else ':' end,
coalesce(d_val,''),
case isnull(a_id) when true then '' else ')' end
) separator ',')
from table
group by a_id;

Find columns with empty value in all rows of a table

i'm searching for an SQL request which gives me all columns in a table that are never populated.
For example, in a table like this one :
column 1 | column 2 | column 3 | column 4 | column 5
---------+----------+----------+----------+---------
value | NULL | value | value | NULL
NULL | NULL | value | value | NULL
value | NULL | value | NULL | NULL
value | NULL | value | value | NULL
The request will return : column 2, column 5
EDIT :
I've created this little PHP script to generate the query and print the result :
$columns = $sql->select("SELECT column_name FROM information_schema.COLUMNS WHERE TABLE_NAME='table_name'");
$query = "SELECT ";
foreach($columns as $column) {
$query .= "case when count(".$column[0].") = 0 then '".$column[0]."' end, ";
}
$query = substr($query, 0,-2);
$query .= " FROM table_name";
var_dump($sql->select($query));
Something like:
select case when count(column1) = 0 then 'column 1' end,
case when count(column2) = 0 then 'column 2' end,
case when count(column3) = 0 then 'column 3' end,
case when count(column4) = 0 then 'column 4' end,
case when count(column5) = 0 then 'column 5' end
from tablename
I.e. count all values for a column, if 0 found return column name.
You can determine if a column contains no values by doing:
select 'column2'
from table t
having max(column2) is null;
You can repeat this for all columns using union all:
select 'column1'
from table t
having max(column1) is null
union all
select 'column2'
from table t
having max(column2) is null
. . .
This returns the results on multiple rows.
If you want a single row:
select concat_ws(', ',
(case when max(column1) is null then 'column1' end),
(case when max(column2) is null then 'column2' end),
(case when max(column3) is null then 'column3' end),
(case when max(column4) is null then 'column4' end),
(case when max(column5) is null then 'column5' end)
)
from table t;

Select string till first or second space in string

I have table column that contains in each row data like this:
| Simbols |
|--------------------------------------|
|H412 Text text |
|H413 Text text text text |
|EUH 001 Text text text text text text |
|EUH 006 text text |
|EUH 201/201A Text text. Text text |
And I need from that data get data like this:
|Simbols |
|------------|
|H412 |
|H413 |
|EUH 001 |
|EUH 006 |
|EUH 201/201A|
I tried with SUBSTRING and CHARINDEX but it till the end don't work... It takes only first space or something like that...
QUERY:
SELECT
CASE
WHEN SUBSTRING(Simbols, 1, CHARINDEX(' ', Simbols)) = ''
THEN Simbols + ' '
ELSE SUBSTRING(Simbols, 1, CHARINDEX(' ', Simbols))
END 'Simbols'
FROM dbo.table
RESULT:
| Simbols |
|------------|
|H412 |
|H413 |
|EUH |
|EUH |
|EUH |
How can I make this work, and where is the problem?
Maybe there is different way to get these Simbols?
P.S. "Text text text" is a example, there comes a explanations of "Simbols"
The CharIndex() function has an optional 3rd parameter - start_location - that will be key here.
SELECT your_column
, CharIndex(' ', your_column) As first_space
, CharIndex(' ', your_column, CharIndex(' ', your_column) + 1) As second_space
, SubString(your_column, 1, CharIndex(' ', your_column, CharIndex(' ', your_column) + 1)) As first_two_words
FROM your_table
Unfortunately when the CharIndex() function can't find the specified string (in this case a single space ' ') it will return 0 (zero).
This means that if there isn't a first or second space the result of first_two_words in my above example will return an empty string as SubString(your_column, 1, 0) = ''.
To get around this you need to be a little clever.
Essentially, if second_space = 0 then we need to return the full string. We have a few options for this:
SELECT your_column
, CharIndex(' ', your_column) As first_space
, CharIndex(' ', your_column, CharIndex(' ', your_column) + 1) As second_space
, SubString(your_column, 1, CharIndex(' ', your_column, CharIndex(' ', your_column) + 1)) As first_two_words
, SubString(your_column, 1, Coalesce(NullIf(CharIndex(' ', your_column, CharIndex(' ', your_column) + 1), 0), Len(your_column))) As first_two_words_option1
, CASE WHEN CharIndex(' ', your_column, CharIndex(' ', your_column) + 1) = 0 THEN your_column ELSE SubString(your_column, 1, CharIndex(' ', your_column, CharIndex(' ', your_column) + 1)) END As first_two_words_option2
FROM (
SELECT 'one' As your_column
UNION ALL SELECT 'one two'
UNION ALL SELECT 'one two three'
UNION ALL SELECT 'one two three four'
) As x
Try this: It works
SELECT CASE WHEN charindex(' ', Simbols, charindex(' ', Simbols) + 1) = 0
THEN Simbols
ELSE LEFT(Simbols, charindex(' ', Simbols, charindex(' ', Simbols) + 1))
END
FROM dbo.table
Here is screenshot what I tried.
Here is new EDIT
SELECT REPLACE(Simbols, 'text', '') FROM dbo.table
Here is screen shot
Try something like this:
select TRIM(REPLACE(lower(type),"text" ,"")) as T, type from supportContacts
Sql Fiddle: http://sqlfiddle.com/#!2/d5cf8/4
for more info :http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

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