SQL Server order desc and also show 6/6 - sql-server-2008

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

Related

Using an OnCalcField with a query

// accounts receivable
qAR.SQL.Clear;
qAR.SQL.Text := 'Select C.Lastname,C.Firstname,C.Address,C.City,C.State,C.Zip,' +
'C.Hercell,C.Hiscell,C.Phone,C.Code,' +
'I.Date,I.Clino,I.Date,I.ToBePaid,' +
'SUM(CASE when C.Clino = I.Clino then I.ToBePaid END) as nToBePaid,' +
'SUM(CASE when C.Clino = I.Clino and I.Date between :S0 and :E0 then I.ToBePaid END) as nCurrent,' +
'SUM(CASE when C.Clino = I.Clino and I.Date between :S1 and :E1 then I.ToBePaid END) as nOver30,' +
'SUM(CASE when C.Clino = I.Clino and I.Date between :S2 and :E2 then I.ToBePaid END) as nOver60,' +
'SUM(CASE when C.Clino = I.Clino and I.Date <= :E3 then I.ToBePaid END) as nOver90 ' +
'from Invoice I ' +
'join Client C on C.Clino = I.Clino ' +
'where I.Date <= :E ' +
'and I.ToBePaid <> 0 ' +
'and I.Active = true ' +
'and C.Code <> 1 ' +
'group by C.Clino ' +
'having nToBePaid > 0 ' +
'order by C.Lastname,C.Firstname';
qAR.ParamByName('S0').AsDate := dDate30 + 1;
qAR.ParamByName('E0').AsDate := dBillingDate;
qAR.ParamByName('S1').AsDate := dDate60 + 1;
qAR.ParamByName('E1').AsDate := dDate30;
qAR.ParamByName('S2').AsDate := dDate90 + 1;
qAR.ParamByName('E2').AsDate := dDate60;
qAR.ParamByName('E3').AsDate := dDate90;
qAR.ParamByName('E').AsDate := dBillingDate;
qAR.FieldDefs.Updated := false;
qAR.FieldDefs.Update;
oField := TBooleanField.Create( qAR );
oField.FieldKind := fkCalculated;
qAR.OnCalcFields := qARCalcFields;
qAR.DetailFields := 'Clino';
qAR.Open;
I have a calculated field Sel (boolean) define in this query. I want to click a button and have the Sel fields on all records to be set to TRUE and another button and have them all set to FALSE. What should the code of 1) the OnCalcField and 2) each of the buttons be?
Thanks,
Jim Sawyer

My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '8:45 AM' to data type int.
My Script is
Declare #SpecimenId bigint = 0, #script varchar(MAX);
Declare #StartTime int = (Select Convert(int,SUBString(START_TIME,0,CHARINDEX(':', START_TIME)) - 12) From APPT
Where APPT._ID=112601)
--select #StartTime
Set #script =
'SELECT distinct PTLAST_NAME + '', '' + PT.FIRST_NAME As PATIENT_NAME,
dbo.FormatDate(PT.DOB,''MM/dd/yyyy'') As DOB,
(Convert(varchar, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END)) + ''/'' +
Convert(varchar, (DATEDIFF(m, DATEADD(yy, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END), DOB), APPT_DATE) - CASE WHEN DAY(DOB) > DAY(APPT_DATE) THEN 1 ELSE 0 END)) + ''/'' +
Convert(varchar, (DATEDIFF(d, (DATEADD(m, (DATEDIFF(m, DATEADD(yy, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END), DOB), APPT_DATE) - CASE WHEN DAY(DOB) > DAY(APPT_DATE) THEN 1 ELSE 0 END), (DATEADD(yy, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END), DOB)))), APPT_DATE)))) As PatAge,
PT.Org_Id, PT.ADDRESS1 As PatAddress,
PT.LAST_NAME As LastName,PT.FIRST_NAME As FirstName,
PT.MIDDLE_NAME As MiddleName,
PHY.LAST_NAME + '', '' + PHY.FIRST_NAME As Physician,
dbo.FormatDate(APPT.APPT_DATE,''MM/dd/yyyy'') As CollectedDate,
(Case when SUBString(START_TIME,0,CHARINDEX('':'', START_TIME)) < 12 Then Convert(varchar,START_TIME) + Convert(varchar,'' AM'') ELSE' +Convert(varchar,#StartTime)+ '+ SUBString(START_TIME,3,6) + '' PM'' END) As CollectedTime,
(Case When PT.GENDER =''1'' Then ''M'' Else ''F'' End) As Gender,PT.CITY,LST.State_Short_Name As State ' +
(Case When #SpecimenId = 0 Then ', 0 As SpeciId' Else ', (Case When Tbl_Pat.Speci_Id > 0 Then Tbl_Pat.Speci_Id Else '''' End) As SpeciId' End) +
' FROM PT INNER JOIN
APPT ON PT.PAT_ID = APPT.PAT_ID INNER JOIN
PHY ON APPT.PHY_ID = PHY.PHY_ID INNER JOIN
LST ON PT.STATE = LST.STATE_ID ' +
(Case When #SpecimenId = 0 Then '' Else 'LEFT OUTER JOIN Tbl_Pat ON APPT.APPT_ID = Tbl_Pat.Speci_ApptId' End)
+ ' WHERE (APPT.APPT_ID = 112601) ' +
(Case When #SpecimenId = 0 Then '' Else 'AND (Tbl_Pat.Speci_Id = ' + Convert(varchar, #SpecimenId) + ')' End)
exec (#script)
Can Anyone help or correct my script please

CASE WHEN with multiple THEN ELSE

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)'.

MySQL 'union all' not bringing back information for second select statement

EDIT: UNION and UNION ALL have the same result.
I have the following statement - I had assumed that this would bring back the following columns
I've realised that I am asking to UNION from the same table, does that matter? I need to bring back data for 'hotel_name, company, hotel_1 and q351' - is that possible?
hotel_name, company, hotel_1 and q351
(SELECT hotel_name, company,
((AVG (q1) + AVG(q2) + AVG(q3) + AVG(q4) + AVG(q5) + AVG(q6) + AVG(q7) + AVG(q8) + AVG(q9) + AVG(q10) + AVG(q11) + AVG(q12) + AVG(q13) + AVG(q14) + AVG(q15) + AVG(q16) + AVG(q17) + AVG(q18) + AVG(q19) + AVG(q20) + AVG(q21) + AVG(q22) + AVG(q23)+ AVG(q24)+ AVG(q25) ) / 25 ) AS hotel_1
FROM thotels_respondents
LEFT JOIN thotels_results_new ON thotels_respondents.login_id = thotels_results_new.company AND thotels_results_new.sdate = 'NOV2014'
WHERE thotels_respondents.brand = 'ACME' AND thotels_respondents.sdate = 'NOV2014' AND completion_status = 'Not Started'
GROUP BY hotel_name
ORDER BY hotel_name)
UNION ALL
(SELECT hotel_name, company,
((AVG (q1) + AVG(q2) + AVG(q3) + AVG(q4) + AVG(q5) + AVG(q6) + AVG(q7) + AVG(q8) + AVG(q9) + AVG(q10) + AVG(q11) + AVG(q12) + AVG(q13) + AVG(q14) + AVG(q15) + AVG(q16) + AVG(q17) + AVG(q18) + AVG(q19) + AVG(q20) + AVG(q21) + AVG(q22) + AVG(q23)+ AVG(q24)+ AVG(q25) ) / 25 ) AS q351
FROM thotels_respondents
LEFT JOIN thotels_results_new ON thotels_respondents.login_id = thotels_results_new.company AND thotels_results_new.sdate = 'NOV2014'
WHERE q35 = 1 AND thotels_respondents.brand = 'ACME' AND thotels_respondents.sdate = 'NOV2014' AND completion_status = 'Not Started'
GROUP BY hotel_name
ORDER BY hotel_name)
But it actually, only brings back data for:
hotel_name, company, hotel_1
It is missing q351
I can only surmise I am using UNION ALL incorrectly - can anyone point out where I am going wrong?
Move the different condition from WHERE in the aggregate function
SELECT hotel_name, company,
((AVG (q1) + AVG(q2) + AVG(q3) + AVG(q4) + AVG(q5) + AVG(q6) + AVG(q7) + AVG(q8) + AVG(q9) + AVG(q10) + AVG(q11) + AVG(q12) + AVG(q13) + AVG(q14) + AVG(q15) + AVG(q16) + AVG(q17) + AVG(q18) + AVG(q19) + AVG(q20) + AVG(q21) + AVG(q22) + AVG(q23)+ AVG(q24)+ AVG(q25) ) / 25 ) AS hotel_1,
((AVG (CASE WHEN q35 = 1 THEN q1 END) + AVG(CASE WHEN q35 = 1 THEN q2 END) + AVG(CASE WHEN q35 = 1 THEN q3 END) + AVG(CASE WHEN q35 = 1 THEN q4 END) + AVG(CASE WHEN q35 = 1 THEN q5 END) + AVG(CASE WHEN q35 = 1 THEN q6 END) + AVG(CASE WHEN q35 = 1 THEN q7 END) + AVG(CASE WHEN q35 = 1 THEN q8 END) + AVG(CASE WHEN q35 = 1 THEN q9 END) + AVG(CASE WHEN q35 = 1 THEN q10 END) + AVG(CASE WHEN q35 = 1 THEN q11 END) + AVG(CASE WHEN q35 = 1 THEN q12 END) + AVG(CASE WHEN q35 = 1 THEN q13 END) + AVG(CASE WHEN q35 = 1 THEN q14 END) + AVG(CASE WHEN q35 = 1 THEN q15 END) + AVG(CASE WHEN q35 = 1 THEN q16 END) + AVG(CASE WHEN q35 = 1 THEN q17 END) + AVG(CASE WHEN q35 = 1 THEN q18 END) + AVG(CASE WHEN q35 = 1 THEN q19 END) + AVG(CASE WHEN q35 = 1 THEN q20 END) + AVG(CASE WHEN q35 = 1 THEN q21 END) + AVG(CASE WHEN q35 = 1 THEN q22 END) + AVG(CASE WHEN q35 = 1 THEN q23 END)+ AVG(CASE WHEN q35 = 1 THEN q24 END)+ AVG(CASE WHEN q35 = 1 THEN q25 END) ) / 25 ) AS q351
FROM thotels_respondents
LEFT JOIN thotels_results_new ON thotels_respondents.login_id = thotels_results_new.company AND thotels_results_new.sdate = 'NOV2014'
WHERE thotels_respondents.brand = 'ACME' AND thotels_respondents.sdate = 'NOV2014' AND completion_status = 'Not Started'
GROUP BY hotel_name
ORDER BY hotel_name

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