ORA-00998: must name this expression with a column alias - sql

I get that I should add alias with all the columns and I'm doing so but I'm still getting error.
CREATE TABLE MENTIONS AS SELECT
UM.USER_ID AS U_ID,
UM.SCREEN_NAME AS USER_SCREEN_NAME,
UM.MENTION_ID AS M_USER_ID,
(
SELECT
UI.USER_SCREEN_NAME AS MENTIONED_USER
FROM
USER_INFO UI
WHERE
UI.USER_ID = UM.MENTION_ID
AND ROWNUM = 1
)
FROM
USER_MENTION UM
USER_MENTION table
USER_ID SCREEN_NAME MENTION_ID
135846337 irisschrijft 774759032636727300
50117969 Chjulian 13769472
14411827 thenriques45 13769472
26681613 ahenotri 252074645
26681613 ahenotri 9796472
158378782 SpringerOpen 9796472
144241914 Kumarappan 252074645
User_INFO table:
USER_ID USER_SCREEN_NAME
22553325 jasonesummers
23435691 QRJAM false
67421923 inTELEgentMSP
97393397 knauer0x
85303739 MarriageTheorem
3842711 seki
3036414608 Bayes_Rule
838677852 BOLIGATOR
I'm still getting the above mentioned error, what am I doing wrong?

Lookup the Oracle Error Message Manual of the current Oracle version. Here the error is mentioned but without additional information.
In such a case look up the
Oracle Error Message Manual of version 9i
For reasons I don't know a lot of error messages have a description in the 9i manual but not in the manuals of higher versions. 9i is a rather old version so the description may be out of date. But it may contain valuable hints.
ORA-00998 must name this expression with a column alias
Cause: An expression or function was used in a CREATE VIEW statement, but no corresponding column name was specified. When expressions or functions are used in a view, all column names for the view must be explicitly specified in the CREATE VIEW statement.
Action: Enter a column name for each column in the view in parentheses after the view name.
We don't have a view but a a table that was created by a select. And actually the last expression of the select list is an expression without an alias. So try your statement using an alias for the last expression. So try
CREATE TABLE MENTIONS AS SELECT
UM.USER_ID AS U_ID,
UM.SCREEN_NAME AS USER_SCREEN_NAME,
UM.MENTION_ID AS M_USER_ID,
(
SELECT
UI.USER_SCREEN_NAME
FROM
USER_INFO UI
WHERE
UI.USER_ID = UM.MENTION_ID
AND ROWNUM = 1
) AS MENTIONED_USER
FROM
USER_MENTION UM
The column alias in the inner select list is useless and can be removed.

The problem with your query is that each column in the create table needs to have a name. You think you are assigning a name in the sub-select. However, you are not.
The subquery is just returning a value -- not a value with a name. So, the AS MENTIONED_USER in your version does nothing. This is a bit tricky, I guess. One way to think of the scalar subquery is that it is just another expression or function call. Things that happen inside it don't affect the outer query -- except for the value being returned.
The correct syntax is to put the column alias outside the subselect, not inside it:
CREATE TABLE MENTIONS AS
SELECT UM.USER_ID AS U_ID, UM.SCREEN_NAME AS USER_SCREEN_NAME, UM.MENTION_ID AS M_USER_ID,
(SELECT UI.USER_SCREEN_NAME
FROM USER_INFO UI
WHERE UI.USER_ID = UM.MENTION_ID AND ROWNUM = 1
) AS MENTIONED_USER
FROM USER_MENTION UM;

Related

An object or column name is missing or empty GEOMETRY

I'm trying to do a geometric union but it's not working
select b.[Region],geometry::UnionAggregate(geom.MakeValid()) into dbo.DRShape
from [dbo].[departements2] a join [dbo].[BM_REGIONFR] b
on a.[code_insee] = b.[dep_2]
group by b.Region
Msg 1038, Niveau 15, État 5, Ligne 1
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.
You are trying to create a table using SELECT INTO, the table needs a column name for each column. Your second column result is missing an alias (that will turn into a column name).
select
b.[Region],
geometry::UnionAggregate(geom.MakeValid()) AS Geo -- Add Alias!!
into
dbo.DRShape
from [dbo].[departements2] a join [dbo].[BM_REGIONFR] b
on a.[code_insee] = b.[dep_2]
group by b.Region
If you comment the INTO dbo.DRShape and you see that your query runs OK, means that the problem is the creation of your new table.

SQL statement for a join in dB2

The following is my sql statement for a join in dB2.
select name, address, bloodgroup
from user_tb, health_tb
where user_tb.id = health_tb.id;
I am getting the following error:
"health_tb.id" is not valid in the context where it is used..
SQLCODE=-206, SQLSTATE=42703, DRIVER=4.12.79
I understand that one reason why I could be getting this error is because id may not exist in health_tb, but that is not the case. I hope someone can advise. Thank you.
First, you should learn to use modern join syntax, although this has nothing to do with your problem:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.id;
A simple search on Google pointed me to the documentation for this error. One of the first things it mentions is:
Possible reasons for this error include:
The specified column is not a column of any of the source or target
tables or views of the statement.
In a SELECT or DELETE statement, the specified column is not a column of any of the tables or views that are identified in a FROM
clause in the statement.
A column list of an SQL data change statement specified the name of a column of the target table or view of the statement.
I suspect that the id column is really called something like user_id. The working query might look like:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.user_id;
1) check if the id column in both tables have the same data type
2) check if there is any trailing space in the column name
select '<' || column_name || '>' from user_tab_columns
where tbname = 'health_tb'
If the id columns are defined as different types, that could be a problem.

PL/SQL error with transposing string to rows

I am trying to spilt a string in my table into separate rows and then looking up those values in another table to see which values doesn't exists. This is the code I am using:
SELECT NAME, desc, LABEL, trim(x.column_value.extract('e/text()')) AS ID
from table1 T1, table (xmlsequence(xmltype('<e><e>' || replace(str_work,' ','</e><e>')||
'</e></e>').extract('e/e'))) x
where
NOT EXISTS (SELECT *
FROM
tabl2 T2
WHERE
T1.ID = x.ID) AND t1.str_work IS NOT NULL);
The code is giving an error:
ORA-00904: "X"."ID": invalid identifier
I am not able to figure out what the issue is. I would appreciate all your help and suggestions.
Thank you.
From the documentation (emphasis added):
You can use a column alias, c_alias, to label the immediately
preceding expression in the select list so that the column is
displayed with a new heading. The alias effectively renames the select
list item for the duration of the query. The alias can be used in the
ORDER BY clause, but not other clauses in the query.
So you can't refer to the ID alias in the subquery. That alias isn't coming from the inline view you've labelled as x anyway, though. You could solve both issues, I think, by repeating the trim in the subquery:
NOT EXISTS (SELECT *
FROM
tabl2 T2
WHERE
T1.ID = trim(x.column_value.extract('e/text()')))
AND t1.str_work IS NOT NULL);
But is that supposed to be referring to T2.ID - otherwise there is no correlation?
Alternatively you could introduce another inline view to avoid the repetition, but it's probably adding complexity for not much gain here.

Update records using select query with subquery returning error

Error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
I understand this error, but I don't know how to rewrite the query to avoid it. I'm looking for records with duplicate values in f1, f2, and f3 based on the timestamp of the file they were imported from. The records not in the subquery should be modified as indicated. I don't need the import_file_timestamp in my results but I believe I have to include it to select the proper version of the record. The subquery, by itself, returns the correct information. How do I do this?
update import_raw_records
set raw_record_status = 'I'
from import_raw_records
where f1+f2+f3 NOT IN (
select a.f1+a.f2+a.f3,
max(b.import_file_timestamp)
from import_raw_records a
inner join import_files b
on a.import_file_id = b.import_file_id
group by a.f1+a.f2+a.f3)
Thanks,
John

Invalid columns on trigger

I get the error invalid column media.user and tuid. Before running this i see that media.user does exist and i would think that the coalesce() as tuid would solve the tuid problem.
Why are these columns invalid?
CREATE TRIGGER media_comment_trig_0 ON media_comment AFTER INSERT AS
INSERT INTO user_incoming_media_comments(recipient, comment)
SELECT coalesce(p.author, [media.user]) as tuid, INSERTED.id
FROM media
JOIN INSERTED ON media.id = INSERTED.media
LEFT JOIN media_comment p on p.id=INSERTED.parent
WHERE tuid <> INSERTED.author;
USER is a reserved SQL keyword, so you'll need to write it as [media].[user] instead.
Also, you can't use an alias in the WHERE clause. You'll have to put the full expression back in there:
WHERE coalesce(p.author, [media].[user]) <> INSERTED.author;
change [media.user] to [media].[user], the [ and ] cause SQL Server to think [media.user] is the column name, and not the table.column.
Also, you need to change WHERE tuid <> INSERTED.author; to WHERE coalesce(p.author, [media].[user]) <> INSERTED.author;. You must repeat the COALESCE and can't use the column alias in the WHERE.
Does the column exist in the table? COALESCE will substitute a NULL value not a missing column.
Also this [media.user] should be media.[user]
you also need to repeat it in your WHERE clause