在语句时使用增量列的mySQL
我正在尝试在从表中选择数据到auto_increment the Row Count时创建列,而我发现的最佳解决方案是将row_number()跨越(按日期desc)作为行_
,因为它是与订单条款一致,并已证明在我的情况下有效。
但是,当在语句时在情况下创建stats时,我无法使用新列
行_
。
例如,我有以下语句:
SELECT ROW_NUMBER() OVER ( order by Date Desc) AS row_
,CASE WHEN row_ = 1 then 8 else 0 end as stat1
,CASE WHEN row_ = 2 then 2 else 0 end as stat2
FROM tblr R
JOIN tblrn RR ON R.RUniqueID = RR.WRID
WHERE WHID = @VHID AND RDate < @RD
ORDER BY RDate DESC;
但是,我会发现一个错误,说:field List list
中的未知列'row_'。
我真的在寻找可以有一个表格的东西(行_
是由row_number()...
部分计算的):
row_ | col1 | col2 |
---|---|---|
1 | blah blah | blah blah |
2 | blah | blah blah |
3 | 等等 | , |
情况
row_ | 可以 | 在 | 下 | 使用 |
---|---|---|---|---|
| | | | |
| | | | |
| | | | |
I am trying to create a column when selecting data from a table to auto_increment the row count and the best solution I have found is to use ROW_NUMBER() OVER ( order by Date DESC) AS row_
because it is consistent with the order clause and has proven to work in my situation.
However I can't use the new column row_
when creating stats in a case when
statement.
For example, I have the following statement:
SELECT ROW_NUMBER() OVER ( order by Date Desc) AS row_
,CASE WHEN row_ = 1 then 8 else 0 end as stat1
,CASE WHEN row_ = 2 then 2 else 0 end as stat2
FROM tblr R
JOIN tblrn RR ON R.RUniqueID = RR.WRID
WHERE WHID = @VHID AND RDate < @RD
ORDER BY RDate DESC;
However I get an error saying: Unknown column 'row_' in field list
.
Im really looking for something where I can have a table (row_
being calculated by the ROW_NUMBER()...
section):
row_ | col1 | col2 |
---|---|---|
1 | blah | blah |
2 | blah | blah |
3 | blah | blah |
and be able to use row_ in a case when situation so create another column to look like:
row_ | col1 | col2 | stat1 | stat2 |
---|---|---|---|---|
1 | blah | blah | 8 | 0 |
2 | blah | blah | 0 | 2 |
3 | blah | blah | 0 | 0 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以直接使用
row_number()
函数case
子句如下:demo
You can directly use the
ROW_NUMBER()
function within youCASE
clause Like below:DEMO