我正在尝试根据一组'if true'条件将查询的startDate设置为fiirst_completed。我收到错误,语法错误。 我愿意接受有关如何做到这一点的建议。 我正在处理我正在处理的整个查询以及分别给我带来麻烦的部分。
抛出错误的查询的特定部分
SELECT
IF (coolingHours is null AND rowNum = min(rowNum) THEN ''',startDate,''':= first_completed END IF,
rowNum,l.logId,l.completed,l.next_completed,l.first_completed,
isError,charge,totalTimeRange,startingDate,endingDate,coolingHours,
databaseName,i.name,l.itemId, kwDifference, cost, costT,l.details,timeInSeconds
FROM tempTable3 l
inner join items i ON l.logId = i.logId AND i.name LIKE ''%KW PRE%''
WHERE l.itemId = ''',myItemId,'''
AND completed BETWEEN ''', startDate, ''' AND ''', endDate, '''
样本数据
行Num完成next_completed first_completed冷却时间 10 11/27/2013 1:05:45 AM 11/29/2013 11:18:34 PM 11/25/2013 11:21:15 PM(null) 11 11/29/2013 11:18:34 PM 11/30/2013 7:37:45 PM 11/27/2013 1:05:45 AM 20.3197 12 11/30/2013 7:37:45 PM 11/30/2013 11:36:57 PM 11/29/2013 11:18:34 PM(null) 13 11/30/2013 11:36:57 PM 12/1/2013 6:52:19 PM 11/30/2013 7:37:45 PM 19.2561
完整查询
DECLARE startDate VARCHAR (50);
DECLARE endDate VARCHAR (50);
DECLARE mylogID INT;
DECLARE myItemId varchar (50);
DECLARE myItemId2 varchar (50);
DECLARE xHours varchar (50);
SET startDate = FROM_UNIXTIME(fromDate/1000);
SET endDate = FROM_UNIXTIME(toDate/1000);
SET mylogID = (SELECT logID FROM logs WHERE details LIKE 'FCT%' LIMIT 1);
SET myItemID = (SELECT i.itemId FROM items i WHERE i.name LIKE '%KW PRE%');
SET myItemID2 = (SELECT i.itemId FROM items i WHERE i.name LIKE '%KW STA%');
SET @sql = NULL;
SET @sql = CONCAT(
'CREATE TEMPORARY TABLE tempTable3 AS (SELECT
@row := @row + 1 AS rownum,
a.logId,
L1.recordId,
L2.recordId AS next_recordId,
L1.completed,
L2.completed AS next_completed,
L3.completed AS first_completed,
L1.activityId AS activityJoin,
L2.activityId AS next_activityId,
IF(L1.activityId = L2.activityId, 1, NULL) AS isError,
CASE
when a.name LIKE ''%Enable%'' THEN time_to_sec(timediff(L2.completed, L1.completed)) / 3600
ELSE NULL
END AS coolingHours,
TO_SECONDS(L2.completed) - TO_SECONDS(L1.completed) AS timeInSeconds,
((SUBSTR(l.details, INSTR(l.details, '':'') + 1))) AS charge,
time_to_sec(timediff(''', endDate, ''', ''', startDate, ''')) / 3600 AS totalTimeRange,
CONVERT_TZ(''', startDate, ''', ''UTC'', ''US/Pacific'') AS startingDate,
CONVERT_TZ(''', endDate, ''', ''UTC'', ''US/Pacific'') AS endingDate,
((L1.item', myItemID, ' - L1.item', myItemID2, ') *
(TIME_TO_SEC(TIMEDIFF(L2.completed, L1.completed)))) / 3600 AS kwDifference,
((L1.item', myItemID, ' - L1.item', myItemID2, ') * (SUBSTR(l.details, INSTR(l.details, '':'') + 1))) AS cost,
((((L1.item', myItemID, ' - L1.item', myItemID2, ') * (SUBSTR(l.details, INSTR(l.details,'':'') + 1)))
* (TIME_TO_SEC(TIMEDIFF(L2.completed, L1.completed)) / 3600))) AS costT,
DATABASE() AS databaseName, i.itemId ,l.details
FROM
(SELECT @row:=0)R,
(SELECT T1.completed
, (SELECT MIN(T2.completed)
FROM log',mylogID, ' T2
WHERE T2.completed > T1.completed) AS next_completed
, (SELECT MAX(T2.completed)
FROM log',mylogID, ' T2
WHERE T2.completed < T1.completed) AS frst_completed
FROM log',mylogID, ' T1
ORDER BY T1.completed
)TimeOrder
LEFT JOIN log', mylogID, ' L1 ON (L1.completed = TimeOrder.completed)
LEFT JOIN log', mylogID, ' L2 ON (L2.completed = TimeOrder.next_completed)
LEFT JOIN log', mylogID, ' L3 ON (L3.completed = TimeOrder.frst_completed)
LEFT JOIN activities a ON L1.activityId = a.activityId
LEFT JOIN logs l ON a.logId = l.logId
inner join items i ON l.logId = i.logId AND i.name LIKE ''%KW PRE%''
)order by L1.completed');
SET @sqlTemp = NULL;
SET @sqlTemp = CONCAT(
'SELECT
IF (coolingHours is null AND rowNum = min(rowNum) THEN ''',startDate,''':= first_completed END IF,
rowNum,l.logId,l.completed,l.next_completed,l.first_completed,
isError,charge,totalTimeRange,startingDate,endingDate,coolingHours,
databaseName,i.name,l.itemId, kwDifference, cost, costT,l.details,timeInSeconds
FROM tempTable3 l
inner join items i ON l.logId = i.logId AND i.name LIKE ''%KW PRE%''
WHERE l.itemId = ''',myItemId,'''
AND completed BETWEEN ''', startDate, ''' AND ''', endDate, '''
');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
PREPARE stmtTemp FROM @sqlTemp;
EXECUTE stmtTemp;
DEALLOCATE PREPARE stmtTemp;
DROP TEMPORARY TABLE tempTable3
答案 0 :(得分:0)
内联IF采用此格式IF (condition,truevalue,falsevalue)
,没有THEN
或END IF
尝试IF (coolingHours is NULL AND rowNum = min(rowNum),first_completed,",startDate,"),