Mysql Stored Proc Call Error - mysql
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
Related
sql two order by give different result
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
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
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)'.
Using CASE statement in SUM() In Stored Procedure
Here is my code: BEGIN SELECT SUM( CASE WHEN first_name > '' THEN 1 ELSE 0 END AS a CASE WHEN last_name > '' THEN 1 ELSE 0 END AS b, CASE WHEN country > '' THEN 1 ELSE 0 END AS c, CASE WHEN state > '' THEN 1 ELSE 0 END AS d, CASE WHEN city > '' THEN 1 ELSE 0 END AS e) AS total FROM employee_profile WHERE id = user_id_in; END This generates the following error: 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 xxx error for the first line. Can anyone help with the correct syntax for this, please? MySQL Server version: 5.5.8
SUM takes a single argument (/ column) which it sums across multiple rows, not multiple arguments (/ columns) for a single row, this is why it's not working. This should work: SELECT (CASE WHEN first_name > '' THEN 1 ELSE 0 END + CASE WHEN last_name > '' THEN 1 ELSE 0 END + CASE WHEN country > '' THEN 1 ELSE 0 END + CASE WHEN state > '' THEN 1 ELSE 0 END + CASE WHEN city > '' THEN 1 ELSE 0 END) AS total FROM employee_profile WHERE id = user_id_in;
Try this query this will suerly help you.. SELECT SUM( CASE WHEN first_name = '' THEN '1' WHEN last_name = '' THEN '1' WHEN country = '' THEN '1' WHEN state = '' THEN '1' ELSE '0' END) AS total FROM employee_profile WHERE id = user_id_in;