H
H
Hope2021-07-07 09:21:40
SQL
Hope, 2021-07-07 09:21:40

Why does Select return fewer columns than it says?

Hello
Please advise.
There is a query

with AAA(A) as (select 1 union select 2 union select 3 union select 4 union select 5)
select
'А01' as "id",
1 as "sensor",
generate_series('2021-07-01 00:00 :00', '2021-07-31 23:59:59', '10 sec'::interval) as "time",
t.status[floor(random()* 4 + 1)::int] from ( select array_agg(distinct AAA.A) as "status" from AAA ) as t ,
round((random()* 400 - 200)::numeric, 2) as "value"

in it - 5 fields should be displayed, but only 4- the last value field is not there for some reason.
If you change the order of the last two fields - and make status last in the list,
But why in the order that I wrote to be displayed one field less? I can't find the reason in the tutorials. Tell me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Akina, 2021-07-07
@Akina

The reason is simple. Noodles instead of code. This does not allow you to understand what is what.
Let's format:

with AAA(A) as (
    select 1 union 
    select 2 union 
    select 3 union 
    select 4 union 
    select 5
)
select 'А01' as "id",
       1 as "sensor",
       generate_series('2021-07-01 00:00:00', '2021-07-31 23:59:59', '10 sec'::interval) as "time",
       t.status[floor(random()* 4 + 1)::int] 
from (select array_agg(distinct AAA.A) as "status" from AAA ) as t ,
     round((random()* 400 - 200)::numeric, 2) as "value"

Now you can immediately see that value is not a field in the output set. This is a synthetic table alias in the FROM clause .
PS. For logic and even just for syntactic correctness, I am completely silent ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question