SQL错误:ROW_NUMBER()OVER(PARTITION

时间:2018-05-05 22:37:57

标签: sql sqlite row-number

我的SQL代码出了什么问题?

我尝试在此answer

之后删除重复的行

但我一直收到以下错误:

  

near"(":语法错误:SELECT rn = ROW_NUMBER()OVER(

这是SQL代码:

SELECT 
    rn = ROW_NUMBER() OVER (PARTITION BY s.stop_id, s.stop_name 
                            ORDER BY s.stop_id, s.stop_name)
FROM stops s

我在某处读到它与SQL版本或sqlite3的用法有关吗?

这里有一些问题的其他信息:

我有开始表:

table_beginning =
[some_text0 , some_text1 , some_text2 ,  some_text3   ]
[ bla_1     ,   monday   ,    red     , bla_something ]
[ bla_77    ,   tuesday  ,   green    , bla_other     ]
[ bla_99    ,   monday   ,    blue    , bla_anything  ]
[ bla_00    ,  wednesday ,    red     , bla_maybe     ]
[ bla_55    ,   monday   ,   violet   , bla_more      ]
[ bal_66    ,   monday   ,    red     , bla_kind      ]
[ bal_22    ,   monday   ,    red     , bla_stuff     ]

我想最终找到一个没有关于col_2和col_3的问题的表(不关心col_1和col_4是什么!!)

如:

table_final1 =
[some_text0 , some_text1 , some_text2 ,  some_text3   ]
[ bla_1     ,   monday   ,    red     , bla_something ]
[ bla_77    ,   tuesday  ,   green    , bla_other     ]
[ bla_99    ,   monday   ,    blue    , bla_anything  ]
[ bla_00    ,  wednesday ,    red     , bla_maybe     ]
[ bla_55    ,   monday   ,   violet   , bla_more      ]

( - >被踢出的哪些行并不重要。因此,接受的决赛桌也可能如下所示:

table_final2 =
[some_text0 , some_text1 , some_text2 ,  some_text3   ]
[ bla_77    ,   tuesday  ,   green    , bla_other     ]
[ bla_99    ,   monday   ,    blue    , bla_anything  ]
[ bla_00    ,  wednesday ,    red     , bla_maybe     ]
[ bla_55    ,   monday   ,   violet   , bla_more      ]
[ bal_66    ,   monday   ,    red     , bla_kind      ]

table_final3 =
[some_text0 , some_text1 , some_text2 ,  some_text3   ]
[ bla_77    ,   tuesday  ,   green    , bla_other     ]
[ bla_99    ,   monday   ,    blue    , bla_anything  ]
[ bla_00    ,  wednesday ,    red     , bla_maybe     ]
[ bla_55    ,   monday   ,   violet   , bla_more      ]
[ bal_22    ,   monday   ,    red     , bla_stuff     ]

重要的是col_2和col_3从来没有相同的两个条目!

正如您所见,some_text1 = monday AND some_text2 = red现在只存在一次!! (从仅仅查看col_2和col_3的观点来消除重复)

截至col_1和col_4中的内容 - 我并不关心那里的内容!我只关心col_2和col_3里面的内容(那里没有任何疑问!)

一个解决方案:

我找到了一种方法(但也许有一种更优雅的方式?)

CREATE TABLE table_intermediate AS
  SELECT DISTINCT col_2, col_3
FROM table_beginning;

- >这会创建一个中间表 - >使用DISTINCT关键字可以消除疑点 - > (缺点,我完全松开col_1和col_4信息)

也许有一个解决方案,我可以保存col_1和col_4的信息??? (再一次,我不关心col_1或col_4中的哪一个!)

1 个答案:

答案 0 :(得分:2)

在SQLite中,您通常可以使用rowid

select s.*
from stops s
where s.rowid = (select min(s2.rowid)
                 from stops s2
                 where s2.stop_id = s.stop_id and s2.stop_name = s.stop_name
                );

我不确定这是否是你真正需要的。但这似乎是你想要用row_number()做的。如果没有达到这个位置,那么就用样本数据和期望的结果提出另一个问题。