使用PostgreSQL从内部选择中获取更多列

时间:2014-08-06 13:32:23

标签: sql postgresql

我需要从内部选定表中检索更多列并将其存储到另一个表中。以下是相同的示例。

示例

我的第一次尝试:

create table "A_B_1" as select "SlNo","Rlno", 0 as got,"Name" as Nm,"Address" as Addr,
from    (
         select  "Rlno","SlNo", count(*) over (partition by "Rlno") cnt
         from    (
                  select  distinct "SlNo","Rlno"
                  from alldata
                  ) b
         ) a
where   cnt > 1

错误:列“名称”不存在。

注意 alldata表格中的所有字段均可用。但我不知道为什么会发生这样的错误。

我的第二次尝试:

create table "A_B_1" as select "SlNo","Rlno", 0 as got,ad."Name" as Nm,ad."Address" as Addr,
from    (
         select  "Rlno","SlNo", count(*) over (partition by "Rlno") cnt
         from    (
                  select  distinct "SlNo","Rlno"
                  from alldata as ad
                  ) b
         ) a
where   cnt > 1

错误:缺少表“m”

的FROM子句条目

1 个答案:

答案 0 :(得分:1)

select
    "SlNo",
    "Rlno",
    0 as got,
    Nm,
    Addr
from  (
    select "Rlno", "SlNo", Nm, Addr, count(*) over (partition by "Rlno") cnt
    from (
        select distinct on ("SlNo", "Rlno")
            "S1No", "R1no", "Name" as Nm, "Address" as Addr
        from alldata
    ) b
) a
where cnt > 1