SPSS Modeler使用python从sqlcod执行循环

2024-04-16 10:11:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我在spssmodeler中有一个节点,下面提供了SQL代码。 它选择一个月并计算一个月的计数。 我创建了一个参数'$P-P\u ly\u parameter',并给它赋值201807。你知道吗

我想做的是在从201807到201907的几个月里做一个循环。你知道吗

我使用Python代码将其放入工具、流属性和执行中。你知道吗

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

显然,我遗漏了什么。 我假设循环的结果不是为每个月分配month\u id。你知道吗

你能帮我用正确的方法做这个循环吗? 我应该使用Select node并包含这样的内容吗?你知道吗

-- 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等等。 但现在我什么也得不到


Tags: theidnodeforstreambyparameterly
1条回答
网友
1楼 · 发布于 2024-04-16 10:11:30

缺少的部分是设置stream参数的值。 代码行显示:

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

只设置Python脚本本身中变量的值,但不更改同名的stream参数的值。你知道吗

您需要在紧跟其后添加一行,该行显式设置流参数的值,例如:

stream.setParameterValue("p_ly_parameter", p_ly_parameter)

相关问题 更多 >