SPSS Modeler 使用 python 从 SQL 代码中循环几个月

SPSS Modeler using python for doing loop through months from SQL code

我在 SPSS Modeler 中有一个节点,下面提供了 SQL 代码。 它选择一个月并计算一个月的计数。 我创建了一个参数“$P-p_ly_parameter”并为其分配了一个值 201807。

我想做的是 运行 从 201807 到 201907 这几个月的循环。

我使用 Python 代码将其放入“工具”、“流属性”、“执行”。

但是当我 运行 它不会让我得到我期望的结果。 事实上我没有得到任何结果。

显然,我遗漏了一些东西。 我想循环的结果不是每个月都分配给 month_id。

你能帮我正确地做循环吗? 我应该使用 Select 节点并包含类似这样的内容吗?

-- SQL
SELECT 
cust.month_id, 
count(*) as AB_P1_TOTAL

FROM tab1 cust
JOIN tab2 dcust ON dcust.month_id=cust.month_id and 
dcust.cust_srcid=cust.cust_srcid
WHERE   
cust.month_id ='$P-p_ly_parameter'
group by cust.month_id
order by cust.month_id

# Python

import modeler.api

# boilerplate definitions
stream = modeler.script.stream()
taskrunner = modeler.script.session().getTaskRunner()

# variables for starting year
startYear = 2018
# gets us to 2019
yearsToLoop = 1

# get the required node by Id
# double click on node, go to annotations and get ID from bottom right 
selectNode = stream.findByID('id5NBVZYS3XT2')
runNode = stream.findByID('id3N3V6JXBQU2')

# loop through our years
for year in range(0, yearsToLoop):
    # loop through months
    for month in range(1,13):
        #month_id = str(startYear + year) + str(month).rjust(2,'0')#ar
        p_ly_parameter = str(startYear + year) + str(month).rjust(2,'0')#ar
        #debug
        #print month_id
        print p_ly_parameter
        # set the condition in the select node
        #selectNode.setPropertyValue('condition', 'month_id = ' + month_id)
        #selectNode.setPropertyValue("condition", "'month_id = '$P-p_ly_parameter'")
        #selectNode.setPropertyValue('mode', 'Include')

        # run the stream
        runNode.run(None)

我希望按月计算结果,例如 201807 500、201808 1000 等。 但现在我一无所获

缺少的部分是设置流参数的值。 代码行说:

p_ly_parameter = str(startYear + year) + str(month).rjust(2,'0')

仅在Python脚本本身中设置变量的值,但不会更改具有相同名称的流参数的值。

您需要紧接着添加一行,明确设置 流参数 的值,例如:

stream.setParameterValue("p_ly_parameter", p_ly_parameter)