简体   繁体   English

WHERE子句中的DB2别名

[英]DB2 alias in WHERE clause

I have a couple DB2 tables, one for users and one for newsletters and I want to select using an alias in the WHERE clause. 我有几个DB2表,一个用于users ,一个用于newsletters ,我想在WHERE子句中使用别名进行选择。

SELECT a.*, b.tech_id as user FROM users a  
JOIN newsletter b ON b.tech_id = a.newsletter_id
WHERE timestamp(user) < current_timestamp

This is radically simplified so I can see what's going on, but I am getting an error that makes me think that the user alias isn't getting passed correctly: 这从根本上简化了,所以我可以看到发生了什么,但是我收到一个错误,使我认为user别名没有正确传递:

ERROR: An invalid datetime format was detected; that is, an 
invalid string representation or value was specified. 

The user.tech_id is a string built from the datetime when the record was created, so it looks something like 20150210175040951186000000 . user.tech_id是从创建记录的datetime时间开始构建的字符串,因此其外观类似于20150210175040951186000000 I've verified that I can execute a timestamp(tech_id) successfully-- so it can't be the format of the field causing the problem. 我已经验证我可以成功执行timestamp(tech_id)因此它不能是导致问题的字段的格式。

Any ideas? 有任何想法吗?

More information: 更多信息:

There's multiple newsletters per user. 每个用户有多个新闻通讯。 I need to get the most recent newsletter (by the tech_id) and check if that was created in the past week. 我需要获取最新的新闻通讯(通过tech_id),并检查它是否在过去一周内创建。 So the more complex version would be something like: 因此,更复杂的版本如下所示:

SELECT a.*, b.tech_id as user FROM users a  
JOIN newsletter b ON b.tech_id = a.newsletter_id
WHERE timestamp(max(user)) < current_timestamp

Is there a way to JOIN only on the most recent record? 有没有一种办法JOIN仅在最近的记录?

The order of execution is different to the order of writing. 执行的顺序与编写的顺序不同。 The FROM & WHERE clauses are executed before the SELECT clause hence the alias does not exist when you are trying to use it. FROM&WHERE子句在SELECT子句之前执行,因此在尝试使用别名时该别名不存在。

You would have to "nest" part of the query so that the alias is defined before the where clause. 您必须“嵌套”查询的一部分,以便在where子句之前定义别名。 Can be easier in many cases to not use the alias. 在许多情况下,不使用别名会更容易。

try 尝试

WHERE timestamp(b.tech_id) < current_timestamp

The generic "order of execution" of SQL clauses is SQL子句的通用“执行顺序”是

FROM
   JOINs (as part of the from clause)
WHERE
GROUP BY
HAVING
SELECT
ORDER BY

Is there a way to JOIN only on the most recent record? 有没有办法仅在最近的记录上加入?

A useful technique for this is using ROW_NUMBER() assuming your DB2 supports it, and would look something like this: 一项有用的技术是假设您的DB2支持ROW_NUMBER(),并且看起来像这样:

SELECT
      a.*
    , b.tech_id AS techuser
FROM users a
JOIN (
      SELECT
            *
          , ROW_NUMBER() OVER (ORDER BY timestamp(tech_id) DESC) AS RN
      FROM newsletter
) b
      ON b.tech_id = a.newsletter_id
      AND b.rn = 1

this would give you just one row from newsletter, and using the DESCending order gives you the "most recent" assuming timestamp(tech_id) works as described. 这将使您从新闻简讯中仅获得一行,并且使用DESCending顺序将给您“最新的”信息,前提是timestamp(tech_id)如所述。

To get most recent newsletter of user, consider ordering the join query, then select top record (in DB2 you would use FETCH FIRST ONLY ): 要获取用户的最新新闻,请考虑对联接查询进行排序,然后选择顶部记录(在DB2中,您将使用FETCH FIRST ONLY ):

SELECT a.*, b.tech_id as user 
  FROM users a  
INNER JOIN newsletter b ON b.tech_id = a.newsletter_id
ORDER BY b.tech_id
FETCH FIRST 1 ROW ONLY;

Alternatively, you can use a subquery in WHERE clause that aggregates the max user: 或者,您可以在WHERE子句中使用子查询来聚合最大用户:

SELECT a.*, b.tech_id as user 
  FROM users a  
WHERE b.tech_id IN (
  SELECT Max(n.tech_id) as maxUser
  FROM users u 
  INNER JOIN newsletter n ON n.tech_id = u.newsletter_id)

I left out the condition of timestamp(user) < current_timestamp as data stored in database will always be less than current time (ie, now). 我省略了timestamp(user) < current_timestamp的条件,因为存储在数据库中的数据将始终小于当前时间(即现在)。

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

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