简体   繁体   中英

SQL Error [IX000]: Column (t) not found in any table in the query (or SLV is undefined)

I want to count on values of the column val of my table tab1 where date is '04/03/2012'.

Code

SELECT COUNT(DISTINCT val) FROM (
select val, dbinfo('utc_to_datetime', nbr_seconds) as X.t from tab1
 where X.t> '2012-03-02' and  X.t < '2012-03-05' and val>10) X; 

I'm getting this error SQL Error [IX000]: Column (t) not found in any table in the query (or SLV is undefined)

Does anyone know how to solve it please?

There are two problems:

  1. The column aliases in the select-list can't be referenced in the WHERE clause (which is why t can't be found).
  2. When you fix that, no rows will be found where the date portion is simultaneously both before and after the given date — 2012-03-04; you need an equality operation.

There are multiple ways to resolve this. One fairly simple change:

SELECT COUNT(DISTINCT val)
  FROM (SELECT val, dbinfo('utc_to_datetime', nbr_seconds) AS t FROM tab1) AS x
 WHERE EXTEND(x.t, YEAR TO DAY) = DATETIME('2012-03-04') YEAR TO DAY
   AND x.val > 10;

The sub-select generates a table x with columns val and t , and then the main query filters the results. The EXTEND call discards the time portion of the value in xt , and the comparison with '2012-03-04' . The optimizer may be able to push the conditions in the WHERE clause down into the sub-select — the val condition can be moved trivially, but the time condition is not so easy.

An alternative approach is to convert the start and end of the day to Unix times and do the comparison in the sub-query (which becomes unnecessary). See Convert DATETIME to Unix Epoch in Informix for the function tounixtime() .

SELECT COUNT(DISTINCT val)
  FROM tab1
 WHERE nbr_seconds >= tounixtime(DATETIME(2012-03-04) YEAR TO DAY)
   AND nbr_seconds <  tounixtime(DATETIME(2012-03-04) YEAR TO DAY + 1 UNITS DAY)
   AND val > 10;

When a DATETIME YEAR TO DAY value is extended to DATETIME YEAR TO SECOND , the time components are set to 0.

Warning : untested code — the concepts are correct, but there's a possibility of syntax errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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