sqlite3: near "." : syntax error - sql

I have two tables already created:
create table movies(id integer, name text, score integer);
create table cast(movie_id integer, cast_id integer, cast_name text);
I need the first 10 (distinct, alphabetically by cast_name) cast members and their average movie scores, so I tried:
select movie_id,cast_id,cast_name,id,score from cast,movies
where movies.id=cast.movie_id and cast_name in
(select distinct cast_name from cast order by cast_name limit 10);
But then I got an error message: near "." : syntax error
After that, I tried to make it simpler:
select cast_name, score from cast,movies where movies.id=cast.movie_id;
I still got the same error.
I guess this might be because '.' is a special command in sqlite3, but cannot figure out how to solve this problem.
Any help will be appreciated.

cast is a reserved word. The list of reserved words is here.
select cast_name, score
from `cast` c join
movies m
on m.id = c.movie_id;
You can escape it using backticks or double quotes. This query uses table aliases to simplify the query and more modern syntax for the join.

You cannot specify a column using . notation just call the columns by their name not the tablename.columnname

Related

Issue with Postgres not recognizing CAST on join

I'm trying to join two tables together based on an ID column. The join is not working successfully because I cannot join a varchar column on an integer column, despite using cast().
In the first table, the ID column is character varying, in the format of: XYZA-123456.
In the second table, the ID column is simply the number: 123456.
-- TABLE 1
create table fake_receivers(id varchar(11));
insert into fake_receivers(id) values
('VR2W-110528'),
('VR2W-113640'),
('VR4W-113640'),
('VR4W-110528'),
('VR2W-110154'),
('VMT2-127942'),
('VR2W-113640'),
('V16X-110528'),
('VR2W-110154'),
('VR2W-110528');
-- TABLE 2
create table fake_stations(receiver_sn integer, station varchar);
insert into fake_stations values
('110528', 'Baff01-01'),
('113640', 'Baff02-02'),
('110154', 'Baff03-01'),
('127942', 'Baff05-01');
My solution is to split the string at the dash, take the number after the dash, and cast it as an integer, so that I may perform the join:
select cast(split_part(id, '-', 2) as integer) from fake_receivers; -- this works fine, seemingly selects integers
However, when I actually attempt to perform the join, I'm getting the following error, despite using an explicit cast:
select cast(split_part(id, '-', 2) as integer), station
from fake_receivers
inner join fake_locations
on split_part = fake_locations.receiver_sn -- not recognizing split_part cast as integer!
>ERROR: operator does not exist: character varying = integer
>Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Strangely enough, I can perform this join with my full dataset (a queried result set shows up) but I then can't manipulate it at all (e.g. sorting, filtering it) - I get an error saying ERROR: invalid input syntax for integer: "UWM". The string "UWM" appears nowhere in my dataset or in my code, but I strongly suspect it has to do with the split_part cast from varchar to integer going wrong somewhere.
-- Misc. info
select version();
>PostgreSQL 10.5 on x86_64-apple-darwin16.7.0, compiled by Apple LLVM version 9.0.0 (clang-900.0.39.2), 64-bit
EDIT: dbfiddle exhibiting behavior
You need to include your current logic directly in the join condition:
select *
from fake_receivers r
inner join fake_stations s
on split_part(r.id, '-', 2)::int = s.receiver_sn;
Demo

Oracle SQL Cast two different columns to one column?

I am selecting data from two different columns depending on what sort of record it is.
Using a Case statement I check what type of record it is and select the appropriate column to insert.
However the syntax for using CAST is not correct and after checking the Oracle docs, there is no reason why it is not working.
CREATE TABLE TEST123 AS
SELECT DISTINCT
bill.ROW_ID,
bill.ACCNT_TYPE_CD,
CASE
WHEN bill.ACCNT_TYPE_CD = 'TestAccount'
THEN CAST(TO_CHAR(bill.INTEGRATION_ID) AS VARCHAR2(30)) AS NUM
ELSE CAST(TO_CHAR(bill.OU_NUM )AS VARCHAR2(30)) AS NUM
END
FROM SIEBEL.S_ORG_EXT bill
INNER JOIN Products prod
ON prod.BILL_ACCNT_ID = bill.ROW_ID
OR prod.CUST_ACCNT_ID = bill.ROW_ID;
Error code is:
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
AS NUM must follow the END of the CASE. If you could set the column name for each condition separately, then the column could have two different names. Clearly that can't work.
CASE
WHEN bill.ACCNT_TYPE_CD = 'TestAccount'
THEN CAST(TO_CHAR(bill.INTEGRATION_ID) AS VARCHAR2(30))
ELSE CAST(TO_CHAR(bill.OU_NUM )AS VARCHAR2(30))
END AS NUM
Also, using both TO_CHAR and CAST(... as VARCHAR(30)) is redundant. They are both ways of converting a non-string to a string.
And since CASE .. END is a function, you can go even further than explained by Allan:
SELECT DISTINCT ...
CAST(
CASE WHEN bill.ACCNT_TYPE_CD = 'TestAccount'
THEN bill.INTEGRATION_ID
ELSE bill.OU_NUM
END
AS VARCHAR(30) -- `AS` here is part of the `CAST` function
-- and will denote the requested data type
) AS NUM -- `AS` here introduces the column alias
If your not familiar with the CAST function, please beware of the double meaning of the AS keyword here. See the comments in the code above

How To Handle Table Column Named With Reserved Sql Keyword?

I have a table that has a column named RANK which is a keyword in Oracle.
Now I need to insert data in this table :
insert into mytbl (RANK)
select RANK from other_table
when executing this query I got the following error :
ORA-00907: missing right parenthesis
How does one escape a keyword?
Oracle uses double quotes " to escape reserved words.
insert into mytbl ("RANK")
select "RANK"
from other_table
One other note, Oracle requires correct case as well.
First of all, You shall not use reserved keywords as column name and table name.
Oracle uses Double quotes " to parse reserved keywords so you can parse the keywords
by placing it in doubles quotes "".
insert into mytbl ("RANK")
select "RANK"
from other_table
In my case, there is , in my query.
UPDATE SCHEMA.TABLE SET PART_NO = '1S7F530400', WHERE PART_NO = '1S7?F5304?00';
This should be:
UPDATE SCHEMA.TABLE SET PART_NO = '1S7F530400' WHERE PART_NO = '1S7?F5304?00';
It has been mentioned before, but to emphasize, one must exactly match the case in all uses. When I needed to extract the GROUP column and order it, "Group" did not work. It had to be "GROUP" in both cases such as
select "GROUP" from PICKLIST
order by "GROUP"

Basic SQL Select in Postgresql fails

I am doing a select * on a postgresql table, and everything looks good. But if I do:
SELECT Name from People
It says:
ERROR: column People.Name does not exist
SQL state: 42703
Character: 8
But the name column shows up during select *. I've tried:
SELECT People.Name from People
as well, with the same result. Am I missing something? It should be pretty easy to do this in any other database.
Try putting quotation marks around the column name, i.e. "Name"
PostgreSQL converts everything to lowercase.
If you asks for this:
CREATE TABLE People
{
id SERIAL,
Att1 varchar(100)
-- constraints
};
SELECT Name FROM People;
You should get all the information, because pgsql converts this to
CREATE TABLE people
{
id SERIAL,
att1 varchar(100),
-- constraints
};
SELECT name FROM people;
If you built your table with pgAdmin and created field with mixed casing, you will need to quote them to be sucessfull, like this:
SELECT "Att1" FROM people
Name is a keyword, so it might not be handled well in this case. The best thing to do would be to alias the table like this:
SELECT Name from Peoplep
and then use the p to select the column:
SELECTp.Namefrom People p

Why can't I reference a user defined type field when using nested queries?

So I have the following user defined type in my oracle database:
CREATE OR REPLACE TYPE METRIC_IMPERIAL_DISTANCE AS OBJECT
(
METERS_FEET INTEGER,
CENTIMETERS_INCHES INTEGER,
FRACTION NUMBER
)
I can do the following:
SELECT t_id, get_distance_breakdown (h.height, h.unit_of_measure_id) height_breakdown
FROM heights h
and even
SELECT t_id, get_distance_breakdown (h.height, h.unit_of_measure_id).meters_feet height_meters_feet
FROM heights h
but the following fails with a ORA-00904: "HEIGHT_BREAKDOWN"."METERS_FEET": invalid identifier error:
SELECT t_id, height_breakdown.meters_feet
FROM (SELECT t_id, get_distance_breakdown (h.height, h.unit_of_measure_id) height_breakdown
FROM heights h);
What gives? If there isn't some simple and obvious work-around for this then that decimates much of what would be useful about oracle's user defined types. I feel like I must be missing something.
I believe Oracle needs an alias for the in-line view:
SELECT v.t_id, v.height_breakdown.meters_feet
FROM (SELECT t_id, get_distance_breakdown (h.height, h.unit_of_measure_id) height_breakdown
FROM heights h) v;