【问题标题】:SPSS Modeler using python for doing loop through months from SQL codeSPSS Modeler 使用 python 从 SQL 代码中循环数月
【发布时间】:2019-08-21 15:36:02
【问题描述】:

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

【问题讨论】:

    标签: python sql oracle loops spss-modeler


    【解决方案1】:

    缺少的部分是设置流参数的值。 上面写着的代码行:

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

    只设置一个Python脚本本身的变量的值,不会改变同名流参数的值。

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

    stream.setParameterValue("p_ly_parameter", p_ly_parameter)
    

    【讨论】:

    • 如果您不介意我想问一下您知道如何将开始日期 (startYear) 更改为除 201801 之外的任何其他日期吗?例如到 201807。因为现在它从 201801 开始。并且将变量 startYear 分配给 201807 不会进行任何更改。如果您知道如何将循环中所有表格的结果放在一张表格或 Excel 中?因为现在它给出了单独的表格(或者在 excel 的情况下只有一个表格)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多