Hello I am trying to create a view using the following:
CREATE OR REPLACE VIEW CONCERT_EVENTS1 AS
SELECT CONCERT.CONCERT_ID, EVENT.EVENT_ID, CONCERT.NAME, EVENT.DATE1 , CONCERT.DURATION,
CONCERT.COST AS TO_CHAR(COST, 'L9,999.99')
FROM EVENT
INNER JOIN CONCERT
ON CONCERT.CONCERT_ID = EVENT.CONCERT_ID;
Below is the error I'm receiving:
Error starting at line 1 in command:
CREATE OR REPLACE VIEW CONCERT_EVENTS1 AS
SELECT CONCERT.CONCERT_ID, EVENT.EVENT_ID, CONCERT.NAME, EVENT.DATE1 , CONCERT.DURATION,
CONCERT.COST AS TO_CHAR(COST, 'L9,999.99')
FROM EVENT
INNER JOIN CONCERT
ON CONCERT.CONCERT_ID = EVENT.CONCERT_ID
Error at Command Line:2 Column:113
Error report:
SQL Error: ORA-00923: FROM keyword not found where expected
00923. 00000 - "FROM keyword not found where expected"
*Cause:
*Action:
What is the problem with my SQL?
The AS statement is incorrect. AS is used in selects to rename a column. So CONCERT.COST as CONCERT_COST would rename the column to CONCERT_COST in your view. If you want to format the column, that goes to the left of the as:
TO_CHAR(CONCERT.COST, 'L9,999.99') AS FORMATED_COST
if you still want to rename the column.
Your "AS TO_CHAR(COST, 'L9,999.99')" is not quite right. The "As" statement is used to alias a column, you don't want the column to be named "TO_CHAR(COST, 'L9,999.99')"
I believe what you want is:
CREATE OR REPLACE VIEW CONCERT_EVENTS1 AS
SELECT CONCERT.CONCERT_ID, EVENT.EVENT_ID, CONCERT.NAME, EVENT.DATE1 , CONCERT.DURATION,
TO_CHAR(CONCERT.COST, 'L9,999.99') as 'ALIAS_HERE'
FROM EVENT
INNER JOIN CONCERT
ON CONCERT.CONCERT_ID = EVENT.CONCERT_ID;
This message always indicates a syntax error in the query projection, the bit between the SELECT and the FROM keywords:
ORA-00923: FROM keyword not found where expected
You are still getting it because you have muffed the suggested fix for your original bloomer. Now you have two declarations for the COST column ....
CONCERT.COST TO_CHAR(CONCERT.COST, 'L9,999.99') AS FORMATED_COST
Syntax demands we put a comma between every column in the projection:
CREATE OR REPLACE VIEW CONCERT_EVENTS1 AS
SELECT CONCERT.CONCERT_ID
, EVENT.EVENT_ID
, CONCERT.NAME
, EVENT.DATE1
, CONCERT.DURATION
, CONCERT.COST
, TO_CHAR(CONCERT.COST, 'L9,999.99') AS FORMATED_COST
FROM EVENT INNER
JOIN CONCERT
ON CONCERT.CONCERT_ID = EVENT.CONCERT_ID
Alternatively, if you don't want the numeric COST column in the view just drop it.
I know syntax errors are hard for beginners but you really need to learn to look at your own code with a cool eye. You can't come running to SO every time you've dropped a comma. Laying out your code in a proper fashion will help: code which is easier to read is easier to debug. For the same reason you should avoid putting all your code in UPPER CASE.
Related
I've the following subquery in an sql query:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, NOMBRE AS NOMBREUNIDAD FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY ID_PLAN, ID_CURSO, NEDICION) ASIS
Problem I have I believe lies in that both table ALUMNOS and UNIDADES have a column named 'NOMBRE' so if I attempt to execute the query I obtain:
00000 - "column ambiguously defined"
To avoid that I thought about changing NOMBRE AS NOMBREUNIDAD to:
UNIDADES.NOMBRE AS NOMBREUNIDAD
But if I do that I get a:
00000 - "not a GROUP BY expression"
So, I don't know what to do so that subquery executes properly.
What should I change to properly execute query without changing the column name?
Aliases are pretty useful, if you use them. The simplify queries and make them easier to read and maintain. I'd suggest you to do so, as it'll also help query to work because Oracle doesn't know which table you actually meant when you selected those 4 columns - which tables do they belong to?
This is just a guess as I don't know your tables so you'll have to fix it yourself. Also, I literally JOINed tables; try to avoid comma-separating them in FROM clause and doing join in WHERE clause as it is supposed to filter data.
GROUP BY, as already commented, is probably useless. If you wanted to fetch distinct set of values, then use appropriate keyword: distinct.
SELECT DISTINCT n.id_plan,
s.id_curso,
u.nedicion,
u.nombre
FROM asisten n
JOIN alumnos s ON n.cod = s.cod
JOIN unidades u
ON u.idestructura = s.idestructura
AND u.cdundorg = s.cdundorg
WHERE UPPER (TRANSLATE (u.nombre, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
I managed to solve my problem:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, UNIDADES.NOMBRE AS NOMBREUNIDAD
FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY UNIDADES.NOMBRE,ID_PLAN, ID_CURSO, NEDICION
)
Can anyone please look over my code, I keep getting the following errors
An opening bracket was expected. (near "AS" at position 43)
At least one column definition was expected. (near " " at position 42)
Unexpected token. (near ")" at position 695)
However, I did use the opening bracket and close it at the end. I can not see any syntax error with Atom
CREATE TABLE marketin_testDatabase.results AS (
SELECT
/*begin of the variable list for log_link_visit_action*/
llva.`idlink_va`,
llva.`idsite`,
llva.`idvisitor`,
llva.`idvisit`,
/*end of the variable list for log_link_visit_action*/
/*begin of the variable list for log_visit*/
-- lv.`idvisit`, -- duplicate column name
lv.`idsite`,
lv.`idvisitor`,
lv.`last_idlink_va` -- remember to delete the last comma
/*end of the variable list for log_visit*/
FROM marketin_yolopiwik.matomo_log_link_visit_action llva -- create alias for the long table name
LEFT OUTER JOIN marketin_yolopiwik.matomo_log_visit lv
ON lv.idvisit =
llva.idvisit
);
When you use CREATE TABLE syntax, you need to specify all columns and data types in parenthesis, that's why an opening bracket is expected. You can use SELECT INTO statement:
SELECT
llva.`idlink_va`,
llva.`idsite`,
llva.`idvisitor`,
llva.`idvisit`,
-- lv.`idvisit`,
lv.`idsite`,
lv.`idvisitor`,
lv.`last_idlink_va`
INTO marketin_testDatabase.results
FROM marketin_yolopiwik.matomo_log_link_visit_action llva
LEFT OUTER JOIN marketin_yolopiwik.matomo_log_visit lv
ON lv.idvisit = llva.idvisit
Does the table marketin_testDatabase.results already exist? If yes, do you want to add to it or rewrite it?
I know that I have seen a couple of other questions about this error but I'm new to the sql JOIN so plz could you guy explain what I'm doing wrong.
Here's my query
SELECT Klanten.Klantnummer,`Barcode`, `Naam`, `BetaalStatus`, `ScanStatus`, `TijdScan`, `Prijs`
FROM `Klanten`, `kaart`
LEFT JOIN (`Intro`)
ON (Intro.KlantNummer = Klanten.Klantnummer)
WHERE kaart.KlantNummer = Klanten.Klantnummer
This is the Error I get like you have seen in the title
1054 - Unknown column 'Klanten.Klantnummer' in 'on clause'
And the db names are correct
Simple rule: Never use commas in the FROM clause. Always use proper, explicit JOIN syntax. If you did that, you would not have an error:
SELECT Klanten.Klantnummer,`Barcode`, `Naam`, `BetaalStatus`, `ScanStatus`, `TijdScan`, `Prijs`
FROM `Klanten` JOIN
`kaart`
ON kaart.KlantNummer = Klanten.Klantnummer LEFT JOIN
`Intro`
ON Intro.KlantNummer = Klanten.Klantnummer ;
The problem is that the precedence of , and JOIN are different. Hence, the table before the comma is not known to the ON clause.
So I have the following query which gives me the error in the title. I have no idea why it's giving me the error.
CREATE OR REPLACE VIEW AUSTRALIANMEDALTALLY
AS
SELECT concat(SG_HOSTCITY, SG_YEAR) as 'ioc_game',
SUM(p.part_gold) as 'gold_medals',
SUM(p.part_silver) as 'silver_medals',
SUM(p.part_bronze) as 'bronze_medals'
FROM GAMES.SUMMERGAMES, GAMES.PARTICIPATION
WHERE COUNTRY_ISOCODE = AUS
GROUP BY COUNTRY_ISOCODE;
Any idea why I'm getting the error?
As well as AUS needing to to be quoted as 'AUS', your aliases either need to be double-quoted:
SELECT concat(SG_HOSTCITY, SG_YEAR) as "ioc_game",
which makes them case-sensitive, or not quoted at all:
SELECT concat(SG_HOSTCITY, SG_YEAR) as ioc_game,
And the same for the other aliases.
You also refer to a p as a table alias but you don't define that - presumably that's for participants - and you aren't defining how the tables are related, so you're doing a cross-join. Seems like you probably have the country in both tables, too, so you need to prefix the references with the table alias.
And you're grouping by a column that isn't in the select-list, which will cause its own error; you need to group by any columns you aren't aggregating.
CREATE OR REPLACE VIEW AUSTRALIANMEDALTALLY
AS
SELECT concat(s.SG_HOSTCITY, s.SG_YEAR) as ioc_game,
SUM(p.part_gold) as gold_medals,
SUM(p.part_silver) as silver_medals,
SUM(p.part_bronze) as bronze_medals
FROM GAMES.SUMMERGAMES s
JOIN GAMES.PARTICIPATION p
ON p.gamesno = s.gamesno
WHERE p.COUNTRY_ISOCODE = 'AUS'
GROUP BY s.SG_HOSTCITY, s.SG_YEAR;
WHERE COUNTRY_ISOCODE = AUS
change to
WHERE COUNTRY_ISOCODE = 'AUS'
I created a view and then I want to update the cost and show the error. But it reterns that virtual column not allowed.
View:
CREATE OR REPLACE VIEW CONCERT_EVENTS1 AS
SELECT CONCERT.CONCERT_ID, EVENT.EVENT_ID, CONCERT.NAME, EVENT.DATE1 , CONCERT.DURATION,
CONCERT.TYPE, TO_CHAR(CONCERT.COST, 'L9,999.99') AS FORMATED_COST
FROM EVENT
INNER JOIN CONCERT
ON CONCERT.CONCERT_ID = EVENT.CONCERT_ID;
Below is the error:
Error starting at line 1 in command:
UPDATE CONCERT_EVENTS1
SET formated_cost = '300.00'
WHERE formated_cost = '200.00'
Error at Command Line:2 Column:5
Error report:
SQL Error: ORA-01733: virtual column not allowed here
01733. 00000 - "virtual column not allowed here"
*Cause:
*Action:
Thanks in advance
FORMATED_COST is probably a calculated field, or in any case the
database cannot infer what change is to be made to the tables
underlying the view based on the change you are requiring.
Include an extra column in your view on CONCERT.COST without the formatting. And update only that new column. The explanation of why this is needed is given by Vignesh Kumar.