我很抱歉发布这么低质量的问题,但我真的很茫然。我无法创建这个存储过程,因为工作台一直告诉我我有语法错误......这两个错误被***包围并且在第6行。
当我尝试保存存储过程时,名称会自动解析为工作台中的_SYNTAX_ERROR
,并且sql失败。
CREATE PROCEDURE fetchBudgetInfo(IN year YEAR, IN costCenter INT(11))
BEGIN
BEGIN
SELECT gls.gl, gls.name AS gl_name, IFNULL(budgets.total, 0) as totalBudgeted,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 1 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap1,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 1 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year)***Error HERE (missing closing bracket)*** / 12) * 100, 2), 0 ***Error HERE (missing semicolon)***) AS ap1Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 1 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap1Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 2 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap2,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 2 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap2Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 2 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap2Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 3 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap3,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 3 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap3Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 3 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap3Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 4 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap4,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 4 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap4Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 4 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap4Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 5 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap5,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 5 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap5Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 5 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap5Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 6 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap6,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 6 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap6Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 6 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap6Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 7 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap7,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 7 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap7Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 7 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap7Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 8 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap8,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 8 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap8Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 8 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap8Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 9 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap9,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 9 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap9Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 9 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap9Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 10 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap10,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 10 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap10Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 10 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap10Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 11 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap11,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 11 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap11Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 11 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap11Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 12 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap12,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 12 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap12Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 12 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap12Year,
IFNULL(ROUND(SUM(totalCost), 2), 0) AS totalCost,
IFNULL(ROUND(SUM(totalCost) / (SELECT total FROM budgets WHERE budgets.costCenter = costCenter AND budgets.gl = gls.id) * 100, 2), 0) AS totalPercent
FROM gls
LEFT JOIN requests ON requests.glid = gls.id AND requests.costCenter = costCenter AND requests.status = 'approved' AND (SELECT YEAR(ap.start_date) FROM ap WHERE ap.id = requests.ap) = year
LEFT JOIN budgets ON budgets.gl = gls.id AND budgets.costCenter = costCenter
GROUP BY gls.id;
END
修改
这也失败了......
CREATE PROCEDURE fetchBudgetInfo(IN year YEAR, IN costCenter INT(11))
BEGIN
SELECT gls.id, gls.gl, gls.name AS gl_name, budgets.total as totalBudgeted
GROUP BY gls.id;
END
答案 0 :(得分:0)
好的..我在这里做的是删除所有子选择并替换为常量 - 因为此时我们不关心答案..我们关心运行和返回数据。 ()但保留所有括号等..并将其归结为基本框架。我可以让它回到工作台中。然后我会一次添加一个子选项来“看”它是否打破了子选择或它仍在运行..
Select
IFNULL(ROUND(SUM(CASE WHEN 1 = (1) THEN 10 ELSE 0 END), 2), 0) AS ap2,
IFNULL(ROUND(SUM(CASE WHEN 1 = (0) THEN 20 ELSE 0 END) / ((1) / 12) * 100, 2), 0) AS ap2Ap
from test.maketable
group by make
这可能会揭示真正的罪魁祸首。
答案 1 :(得分:0)
两个查询都不正确。第一个使用第二个BEGIN
(没有结束END
),第二个使用from
子句。