我试图在mysql中运行一个存储过程,它返回0行。个人选择工作很好,他们也作为一个团队一起工作很好。当一起运行时,它们会创建一系列临时tabel,然后在末尾返回一个最终表。再说一遍,一切正常。我已经看了其他问题,没有看到我的查询中的错误。
我的问题:
USE leeds_so;
#### OPEN STORED PROCEDURE
DELIMITER //
CREATE PROCEDURE LadderConversionUpDown3()
BEGIN
#### DROP TABLES IF NEEDED
DROP TABLE cte_leenk_ladder_history;
DROP TABLE cte_ladder_value_between;
DROP TABLE cte_most_recent_record;
DROP TABLE cte_most_recent_record_ladder_level;
DROP TABLE cte_ladder_value_before;
DROP TABLE cte_most_recent_record_before;
DROP TABLE cte_most_recent_record_before_ladder_level;
DROP TABLE cte_additions_to_current_rung;
DROP TABLE cte_before_additions_count;
DROP TABLE cte_sum_additions_to_ladder;
#### CREATE PARAMETERS FOR LADDERS
SET @ladder_type = 'ladder_advocacy'; # remove in data studio
CREATE TEMPORARY TABLE cte_leenk_ladder_history
SELECT
member_id
,ladder_change
,date_trigger_event
,@ladder_type
,CASE WHEN @ladder_type='ladder_advocacy' THEN ladder_advocacy
WHEN @ladder_type='ladder_elected' THEN ladder_elected
WHEN @ladder_type='ladder_policy' THEN ladder_policy
WHEN @ladder_type='ladder_organizing' THEN ladder_organizing
WHEN @ladder_type='ladder_collective' THEN ladder_collective
WHEN @ladder_type='ladder_engagement' THEN ladder_engagement
END
AS ladder_value
FROM leeds_new.leenk_ladder_history;
#### GET CURRENT LADDER INFO
# 1. Get member_id AND ladder hist value between X dates ## NEED TO FIX DATES IN DATA STUDIO
CREATE TEMPORARY TABLE cte_ladder_value_between
SELECT
cte_leenk_ladder_history.member_id AS member_id,
cte_leenk_ladder_history.date_trigger_event
FROM cte_leenk_ladder_history
WHERE cte_leenk_ladder_history.ladder_change = 1 AND cte_leenk_ladder_history.ladder_value IS NOT NULL
AND date_trigger_event BETWEEN CAST('2018-01-01' AS DATE) AND CAST('2018-06-01' AS DATE);
# 2. First get members most recent record date
CREATE TEMPORARY TABLE cte_most_recent_record
SELECT
cte_ladder_value_between.member_id AS member_id,
MAX(cte_ladder_value_between.date_trigger_event) AS date_trigger_event_max
FROM cte_ladder_value_between
GROUP BY cte_ladder_value_between.member_id;
# 3. Get the rung value for the most recent ladder level
CREATE TEMPORARY TABLE cte_most_recent_record_ladder_level
SELECT
cte_leenk_ladder_history.member_id,
cte_leenk_ladder_history.ladder_value,
cte_leenk_ladder_history.date_trigger_event
FROM cte_leenk_ladder_history
INNER JOIN
cte_most_recent_record ON cte_leenk_ladder_history.member_id = cte_most_recent_record.member_id
AND cte_leenk_ladder_history.date_trigger_event = cte_most_recent_record.date_trigger_event_max
WHERE cte_leenk_ladder_history.ladder_change = 1 AND cte_leenk_ladder_history.ladder_value IS NOT NULL;
#### GET LADDER IMMEDIATELY BEFORE
# 4. Get member_id AND ladder hist value between X dates BEFORE
CREATE TEMPORARY TABLE cte_ladder_value_before
SELECT
cte_leenk_ladder_history.member_id AS member_id,
cte_leenk_ladder_history.date_trigger_event
FROM cte_leenk_ladder_history
WHERE cte_leenk_ladder_history.ladder_change = 1 AND cte_leenk_ladder_history.ladder_value IS NOT NULL
AND date_trigger_event < CAST('2018-01-01' AS DATE);
# 5. First get members most recent record date BEFORE
CREATE TEMPORARY TABLE cte_most_recent_record_before
SELECT
cte_ladder_value_before.member_id AS member_id,
MAX(cte_ladder_value_before.date_trigger_event) AS date_trigger_event_max
FROM cte_ladder_value_before
GROUP BY cte_ladder_value_before.member_id;
# 6. Get the rung value for the most recent ladder level BEFORE
CREATE TEMPORARY TABLE cte_most_recent_record_before_ladder_level
SELECT
cte_leenk_ladder_history.member_id,
cte_leenk_ladder_history.ladder_value,
cte_leenk_ladder_history.date_trigger_event
FROM cte_leenk_ladder_history
INNER JOIN
cte_most_recent_record_before ON cte_leenk_ladder_history.member_id = cte_most_recent_record_before.member_id
AND cte_leenk_ladder_history.date_trigger_event = cte_most_recent_record_before.date_trigger_event_max
WHERE cte_leenk_ladder_history.ladder_change = 1 AND cte_leenk_ladder_history.ladder_value IS NOT NULL;
#### COUNT CURRENT & BEFORE LADDERS, THEN DIFFERENCE
# 7. current period counts
CREATE TEMPORARY TABLE cte_additions_to_current_rung
SELECT
cte_most_recent_record_ladder_level.ladder_value,
COUNT(cte_most_recent_record_ladder_level.ladder_value) AS additions_to_current_rung
FROM cte_most_recent_record_ladder_level
GROUP BY cte_most_recent_record_ladder_level.ladder_value;
# 8. before COUNTs
CREATE TEMPORARY TABLE cte_before_additions_count
SELECT
cte_most_recent_record_before_ladder_level.ladder_value,
COUNT(cte_most_recent_record_before_ladder_level.ladder_value) AS before_additions_count
FROM cte_most_recent_record_before_ladder_level
GROUP BY cte_most_recent_record_before_ladder_level.ladder_value;
# 9. SUM additions to rung + before additions
SELECT
cte_additions_to_current_rung.ladder_value,
additions_to_current_rung,
before_additions_count,
additions_to_current_rung + before_additions_count AS total_ladder_counts
FROM cte_additions_to_current_rung
INNER JOIN cte_before_additions_count ON cte_additions_to_current_rung.ladder_value = cte_before_additions_count.ladder_value;
# 10. ADD: HOW MANY WERE LOST IN TIME PERIOD
### as to figure this one out
#### END STORED PROCEDURE
END //
DELIMITER ;
预期结果(返回存储过程外部):
有人知道发生了什么事吗?谢谢。
1条答案
按热度按时间0x6upsns1#
我想出来了。做了些小小的改变,效果不错: