Can any find the solution for this Error in IBM DB2? - db2

%sql select Name_of_School, Safety_Score from SCHOOLS where \
Safety_Score= (select MAX(Safety_Score) from SCHOOLS)
i am trying to execute this query i got the message.
ibm_db_sa://rbm44299:***#dashdb-txn-sbox-yp-lon02-04.services.eu-gb.bluemix.net:50000/BLUDB
(ibm_db_dbi.ProgrammingError) ibm_db_dbi::ProgrammingError: SQLNumResultCols failed: [IBM][CLI Driver][DB2/LINUXX8664] SQL0206N "SAFETY_SCORE" is not valid in the context where it is used. SQLSTATE=42703 SQLCODE=-206
[SQL: select Name_of_School, Safety_Score from SCHOOLS where Safety_Score= (select MAX(Safety_Score) from SCHOOLS)]
(Background on this error at: http://sqlalche.me/e/f405)

SQL0206N is this error message https://www.ibm.com/support/knowledgecenter/SSEPGG_11.5.0/com.ibm.db2.luw.messages.sql.doc/com.ibm.db2.luw.messages.sql.doc-gentopic1.html#sql0206n
SQL0206N name is not valid in the context where it is used.
This error can occur in the following cases:
For an INSERT or UPDATE statement, the specified column is not a column of the table, or view that was specified as the object of the insert or update.
For a SELECT or DELETE statement, the specified column is not a column of any of the tables or views identified in a FROM clause in the statement.
among other cases.
I.e. Column SAFETY_SCORE does not exist in your table. Maybe the column is "Safety_Score" or "Safety Score" or some other name.
If the column name is not in UPPER CASE in your table, you will need to surround it in double quotes.

I could fix by using the %%sql structure and the double quotes :
%%sql
select MAX("Safety Score") AS MAX_SAFETY_SCORE from Chicago_SCHOOLS;

Related

How can I insert into another table selecting from another ignoring the data type

I am running the query below and I am getting the error shown
core=# INSERT INTO transactionp SELECT * FROM transaction WHERE posting_date>'2021-08-17';
ERROR: column "balance" is of type u_money but expression is of type u_datetime
LINE 1: INSERT INTO transactionp SELECT * FROM transaction WHERE pos...
^
HINT: You will need to rewrite or cast the expression.
It's most likely because of the dreaded select * and not specifying target columns for the insert. Both are considered "code smell" and should be avoided.
The error message suggests that the order of columns is different in the source and target tables (they are matched by position not by name)
To fix this, specify column names in both parts: the INSERT and the SELECT statement:
INSERT INTO transactionp (balance, some_column, another_column, ...)
SELECT some_balance, column_one, column_two, ...
FROM transaction
WHERE posting_date>'2021-08-17';

unable to use a select clause in PostgreSQL

Here is a very interesting problem I found.
Here is a snapshot of my table
majorversions Table
Now I try to execute a simple select statement
select * from majorversions mav
where mav.name = "Default-Media"
It throws an error
********** Error **********
ERROR: column "Default-Media" does not exist
SQL state: 42703
Character: 53
It is happening mainly due to the fact that name columnType is Text, if I user client_id in where clause everything works fine.
So how to write a where clause with name as the Column?
Use single quotes for strings.
If it's encased in double quotes it'll think it's a column name and it can't find that column. Thats why you're getting that error.
It should be:
select * from majorversions mav
where mav.name = 'Default-Media'
What it tries to execute in your query is:
select * from majorversions mav where mav.name = mav.Default-Media
Default-Media should be written 'Default-Media' and not "Default-Media"

I am trying to create a summary by country for all the active fires and group them according to month

I am trying to create a summary by country for all the active fires and group them according to month
SELECT
date_part('month',acqdate) AS month
,date_part('year',acqdate) AS year
, count(the_geom) as num_fires
FROM
merged_activefires
JOIN
"california_county.wgs84"
ON
ST_Intersects(merged_activefires.the_geom, california_county.wgs84.the_geom)
WHERE
california_county.wgs84.name = 'Modoc' --acqdate >'2013-12-31' and acqdate<'2014-02-01'
and
GROUP BY date_part('month',acqdate),date_part('year',acqdate)
ORDER BY month;
ERROR: missing FROM-clause entry for table "wgs84"
LINE 4: ON ST_Intersects(merged_activefires.the_geom, california_cou...
^
****** Error ******
ERROR: missing FROM-clause entry for table "wgs84"
SQL state: 42P01
Character: 209
In the JOIN clause, this syntax with double quotes around the entire name:
"california_county.wgs84"
means that this entire name designates a table, as opposed to a schema name followed by a dot followed by a table name, which could be written like this: california_county.wgs84 or like that: "california_county"."wgs84"
On the other hand, this other part of your query:
california_county.wgs84.name
suggests exactly the opposite, that california_county is a schema name and wgs84 is a table inside it, and name is a column of that table.
It is this contradiction that confuses the SQL parser.
Either "california_county.wgs84" is a table name in the current schema (an unfortunate choice, but valid) and you should consistently refer to it with "california_county.wgs84",
or california_county is a schema and you should just use california_county.wgs84 without any quote.

Update table with value from another table

I try to update value from one table with another table by use IP_ID to compare 2 table by following sybtax
UPDATE EDWID02.CUSTOMER_MOBILE t1
SET T1.MOBILE = (
SELECT T2.MOBILE
FROM EDWID02.NEW_MOBILE t2
WHERE T1.IP_ID=T2.IP_ID)
The error I found was DB2 Database Error:
ERROR [21000] [IBM][DB2/AIX64] SQL0811N The result of a scalar fullselect,
SELECT INTO statement, or VALUES INTO statement is more than one row.
SQLSTATE=21000
even I change = to in it's told me another error DB2 Database Error:
ERROR [42601] [IBM][DB2/AIX64] SQL0104N An unexpected token "in" was found
following "t1 SET T1.MOBILE". Expected tokens may include: "=".
SQLSTATE=42601
I am coding in DB2.
your misunderstanding
'UPDATE t1 SET value=onevalue'
onevalue needs to be a single value. You may achieve this by (SELECT value FROM t2 WHERE t1.id = t2.id FETCH FIRST 1 ROW ONLY)

currval Function in PostgreSQL complaining that "column does not exist"

I am trying to use PostgreSQL's currval function to return the last inserted row id of a table called Concept. Concept has a serial primary key called cid and there was an automatically generated Sequence called Concept_cid_seq.
I try the following statement and get an error:
SELECT currval("Concept_cid_seq");
ERROR: column "Concept_cid_seq" does not exist
LINE 1: SELECT currval("Concept_cid_seq");
^
********** Error **********
ERROR: column "Concept_cid_seq" does not exist
SQL state: 42703
Character: 16
But when I run the query :
SELECT * from "Concept_cid_seq";
I get a table with one row (as I'd expect) showing columns like last_value, start_value, etc...
What am I missing here? Am I passing the wrong information to currval? Why does it say the 'column does not exist?'
It turns out that this was an issue with capitalization and quotes. Because I wanted to preserve the capitalization of the relation name I needed to use both single and double quotes in order to pass the correct relation name to currval.
I changed the query to SELECT currval('"Concept_cid_seq"'); (note the outer single quotes) and it worked correctly.