PostgreSQL是否有相当于SAS'的障碍?

时间:2015-07-09 15:56:11

标签: postgresql sas

将脚本,表格,数据集等从SAS环境转换为PostgreSQL环境时,是否有相当于引用PostgreSQL中的SAS'障碍?例如,如果查询说:

SELECT FROM schema.table
WHERE obsnum = 1

有没有办法在PostgreSQL中跟踪观察数或类似情况?或者应采取不同的方法?

感谢。

应该指出我被告知obsnum是与数据集和表相关联的内置SAS值,并且在我的SAS脚本中没有obsnum声明,只有SELECT语句中的单数引用。

2 个答案:

答案 0 :(得分:0)

OBSNUM不是SAS中的自动变量,因此它是一个变量值。 您应该能够在Postgres中使用类似的查询来限制变量值为1的位置。

要添加 - PROC SQL没有自动变量来进行编号,它可以对行号使用monotonic()但不支持。

(错误答案):

尝试CTID

请参阅上一个问题,第一个答案和评论/问题。

What exactly is the/this data statement in SAS doing? PostgreSQL equivalent?

PostgreSQL的 http://www.postgresql.org/docs/8.2/static/ddl-system-columns.html

答案 1 :(得分:0)

        -- make(fake) a dataset
CREATE TABLE dataset
        ( val double precision NOT NULL
        );
        -- populate it with random
INSERT INTO dataset(val)
SELECT random()
FROM generate_series(1,100)
        ;

    -- Use row_number() to enumerate the unordered tuples
    -- note the subquery. It is needed because otherwise
    -- you cannot refer to row_number
SELECT * FROM (
        SELECT val
        , row_number() OVER() AS obsnum
        FROM dataset
        ) qq -- subquery MUST have an alias
WHERE qq.obsnum = 1
        ;

        -- you can just as well order over ctid (the result is the same)
SELECT * FROM (
        SELECT val
        , row_number() OVER(ORDER BY ctid) AS obsnum
        FROM dataset
        ) qq -- subquery MUST have an alias
WHERE qq.obsnum = 1
        ;

        -- In stead of a subquery you could use
        -- a CTE to wrap the enumeration part
WITH zzz AS (
        SELECT val
        , row_number() OVER(ORDER BY ctid) AS obsnum
        FROM dataset
        )
SELECT * FROM zzz
WHERE obsnum = 1
        ;

        -- Or, if you just want one observation: use LIMIT
        -- (order of records is not defined,
        -- but the order of ctid is not stable either)
SELECT * FROM dataset
LIMIT 1 
        ;