简体   繁体   English

AS400 - 为什么标记 *, ? 无效? 使用 STRSQL 运行查询的替代方法是什么 - SQL 交互式会话?

[英]AS400 - Why token *, ! not valid? What could be the alternate way to run the query using STRSQL - SQL Interactive Session?

In AS400, How can I run this query using STRSQL?在 AS400 中,如何使用 STRSQL 运行此查询?

For the below query getting the following error message instead returning results.对于以下查询,获取以下错误消息而不是返回结果。

"Token, was not valid. Valid tokens: FROM INTO." “令牌,无效。有效令牌:FROM INTO。”

Code Snippet for Original Query:原始查询的代码片段:

WITH cte AS ( SELECT *, 
    !SUM(status != 'INACTIVE') OVER (PARTITION BY loc_code, user_id, service_area, service_sector) only_inactive,
    ROW_NUMBER() OVER (PARTITION BY loc_code, user_id, service_area, service_sector ORDER BY last_changed DESC) rn
    FROM test )
              
SELECT * 
FROM cte
WHERE only_inactive AND rn = 1

After checking, the query I found the problem is the SELECT statement inside WITH clause.经过检查,我发现问题的查询是 WITH 子句中的 SELECT 语句。 I don't know why this error is there?我不知道为什么会出现这个错误? Unable to find a possible way to solve this issue.无法找到解决此问题的可能方法。

Now, I tried to remove * from the select statement inside WITH clause, somehow avoided this error.现在,我尝试从 WITH 子句中的 select 语句中删除 *,以某种方式避免了这个错误。 After executing my updated query again, I am getting same kind but a different error message.再次执行更新后的查询后,我得到相同类型但不同的错误消息。

Token.令牌。 was not valid: Valid tokens?无效:有效令牌? ( + * -: . DAY INF LAG NAN RID ROW RRN CASE CAST CHAR DATE DAYS. ( + * -: . DAY INF LAG NAN RID ROW RRN CASE CAST CHAR DATE DAYS。

Code Snippet for Updated Query:更新查询的代码片段:

WITH cte AS ( SELECT !SUM(status != 'INACTIVE') OVER (PARTITION BY loc_code, user_id, service_area, 
    service_sector) only_inactive,
    ROW_NUMBER() OVER (PARTITION BY loc_code, user_id, service_area, service_sector ORDER BY last_changed 
    DESC) rn
    FROM test )
              
SELECT * 
FROM cte
WHERE only_inactive AND rn = 1

I tried:我试过了:

Instead of.代替。 token I tried using <> and ¬= but it didn't help me in both cases.我尝试使用 <> 和 ¬= 的令牌,但在这两种情况下都没有帮助。 When I tried to run my code I encountered the same error message but now with <> and ¬= tokens.当我尝试运行我的代码时,我遇到了相同的错误消息,但现在使用 <> 和 ¬= 标记。

Expected Result:预期结果:

I want to return all records satisfying the logic given in the query with all columns in my table.我想返回满足查询中给出的逻辑的所有记录以及我的表中的所有列。

Could someone please tell me how to solve this issue?有人可以告诉我如何解决这个问题吗?


I tried the following updated query just to test whether it's working without.我尝试了以下更新的查询只是为了测试它是否可以正常工作。 or NOT token or not.或不是令牌。

WITH cte AS ( SELECT test.*, 
    SUM(status < 'INACTIVE') OVER (PARTITION BY loc_code, user_id, service_area, service_sector) only_inactive,
    ROW_NUMBER() OVER (PARTITION BY loc_code, user_id, service_area, service_sector ORDER BY last_changed DESC) rn
    FROM test )
              
SELECT * 
FROM cte
WHERE only_inactive AND rn = 1

I expected this query must have listed some results but this time I get this error message.我预计此查询必须列出一些结果,但这次我收到此错误消息。

Token < was not valid.令牌 < 无效。 Valid tokens: ),.有效令牌:),。

I think the problem is with tokens.我认为问题出在令牌上。 Not sure what.不确定是什么。

Try writing the query like this:尝试像这样编写查询:

WITH cte AS (
      SELECT t.*, 
             MIN( status = 'INACTIVE') OVER (PARTITION BY loc_code, user_id, service_area, service_sector) as only_inactive,
             ROW_NUMBER() OVER (PARTITION BY loc_code, user_id, service_area, service_sector ORDER BY last_changed DESC) as rn
      FROM test t
     )

Here is a db<>fiddle showing that this syntax works in DB2. 是一个 db<>fiddle,显示此语法在 DB2 中有效。

Note that the types of the columns should not matter for the syntax error you are seeing.请注意,列的类型与您看到的语法错误无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM