SQL Insert gagal pada i.name tidak ada, padahal tampaknya ada, selama penyisipan

Saya menggunakan Postgres SQL dan pgAdmin. Saya mencoba menyalin data antara tabel pementasan, dan tabel produksi menggunakan INSERT INTO dengan pernyataan SELECT FROM dengan to_char di sepanjang jalan. Ini mungkin pendekatan yang salah atau mungkin juga bukan. SELECT gagal karena ternyata "kolom i.tanggal tidak ada".

Pertanyaannya adalah: Mengapa saya mendapatkan 'kolom i.tanggal tidak ada'?

Skema untuk kedua tabel sama kecuali untuk konversi tanggal.

Saya sudah mencoba mencocokkan skema tabel dengan pengecualian konversi to_char. Saya telah memeriksa dan memeriksa ulang kolom tersebut ada.

Ini adalah kode yang saya coba:

INSERT INTO weathergrids (location, dates, temperature, rh, wd, ws, df, cu, cc)
  SELECT
    i.location AS location,
    i.dates as dates,
    i.temperature as temperature,
    i.rh as rh,
    i.winddir as winddir,
    i.windspeed as windspeed, 
    i.droughtfactor as droughtfactor,
    i.curing as curing,
    i.cloudcover as cloudcover
  FROM (
      SELECT location, 
             to_char(to_timestamp(dates, 'YYYY-DD-MM HH24:MI'), 'HH24:MI YYYY-MM-DD HH24:MI'), 
             temperature, rh, wd, ws, df, cu, cc 
       FROM wosweathergrids
  ) i;

Kesalahan yang saya terima adalah:

ERROR:  column i.dates does not exist
LINE 4:  i.dates as dates,
         ^
SQL state: 42703
Character: 151

Skema data saya seperti:


+-----------------+-----+-------------+-----------------------------+-----+
|      TABLE      | NUM |   COLNAME   |          DATATYPE           | LEN |
+-----------------+-----+-------------+-----------------------------+-----+
| weathergrids    |   1 | id          | integer                     |  32 |
| weathergrids    |   2 | location    | numeric                     |   6 |
| weathergrids    |   3 | dates       | timestamp without time zone |     |
| weathergrids    |   4 | temperature | numeric                     |   3 |
| weathergrids    |   5 | rh          | numeric                     |   4 |
| weathergrids    |   6 | wd          | numeric                     |   4 |
| weathergrids    |   7 | wsd         | numeric                     |   4 |
| weathergrids    |   8 | df          | numeric                     |   4 |
| weathergrids    |   9 | cu          | numeric                     |   4 |
| weathergrids    |  10 | cc          | numeric                     |   4 |
| wosweathergrids |   1 | id          | integer                     |  32 |
| wosweathergrids |   2 | location    | numeric                     |   6 |
| wosweathergrids |   3 | dates       | character varying           |  16 |
| wosweathergrids |   4 | temperature | numeric                     |   3 |
| wosweathergrids |   5 | rh          | numeric                     |   4 |
| wosweathergrids |   6 | wd          | numeric                     |   4 |
| wosweathergrids |   7 | ws          | numeric                     |   4 |
| wosweathergrids |   8 | df          | numeric                     |   4 |
| wosweathergrids |   9 | cu          | numeric                     |   4 |
| wosweathergrids |  10 | cc          | numeric                     |   4 |
+-----------------+-----+-------------+-----------------------------+-----+


person anakaine    schedule 23.09.2019    source sumber


Jawaban (1)


Tabel turunan Anda (sub-kueri) bernama i tidak memiliki kolom bernama dates karena kolom dates "tersembunyi" dalam fungsi to_char() dan karena tidak mendefinisikan alias untuk ekspresi tersebut, tidak ada kolom dates yang tersedia "di luar" turunan meja.

Tapi saya tidak melihat alasan untuk memulai tabel turunan. Juga: membuat alias pada kolom dengan nama yang sama juga tidak diperlukan i.location as location sama persis dengan i.location.

Jadi pertanyaan Anda dapat disederhanakan menjadi:

INSERT INTO weathergrids (location, dates, temperature, rh, wd, ws, df, cu, cc)
SELECT
    location,
    to_timestamp(dates, 'YYYY-DD-MM HH24:MI'),
    temperature,
    rh,
    winddir,
    windspeed, 
    droughtfactor,
    curing,
    cloudcover
FROM wosweathergrids

Anda tidak perlu memberikan alias pada ekspresi to_timestamp() karena kolomnya dicocokkan berdasarkan posisi, bukan nama dalam pernyataan insert ... select.

person a_horse_with_no_name    schedule 23.09.2019