PL/SQL error with transposing string to rows - sql

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.

Related

What is the "DATA" keyword in T-SQL for?

I'm working with a database in T-SQL/SQL Server 2016 at the moment which has some stored procedures containing a keyword I'm not familiar with, namely the "DATA" suffix after a query:
SELECT * FROM dbo.TableName DATA
I'm struggling to find any documentation on what the purpose of this "DATA" keyword is. Could someone shed some light please?
It is not some specific keyword. It is just a table alias. Note that if you changed your select to
SELECT DATA.* FROM dbo.TableName DATA
it will work, as the table now has the "DATA" alias. For the same reason, this:
SELECT dbo.TableName.* FROM dbo.TableName DATA
will throw an error.
This is an alias for the table name, usually it is used if we are inner joining the same table more than one time, or when we need to call the table with a shortcut name.
For example if the table has a key named ID, then:
SELECT DATA.* FROM dbo.TableName DATA
where DATA.ID = "1"
is like
SELECT dbo.TableName.* FROM dbo.TableName
where TableName .ID = "1"

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

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;

Oracle: Accessing parent attribute in subquery

How can I access 'parent' attributes in subqueries.
E.g. if I have the following Minimal Working Example snippet, I expect as output
"1,2:3"
however it fails with
ORA-904, T1.F1 invalid Identifier.
Now I know I can rewrite this complete query to get this working, however reason for asking this is:
Why can't I access the 'outer' attrbute?
How can I access it with less modification and
I want to add a column without modyfing the outer query too much.
MWE:
create table T1(F1 INTEGER);
create table T2(F2 INTEGER,F3 INTEGER);
insert into T1(F1) VALUES(1);
insert into T2(F2,F3) VaLUES(1,2);
insert into T2(F2,F3) VALUES(1,2);
insert into T2(F2,F3) VALUES(1,3);
select T1.F1,
(SELECT LISTAGG(A,':') WITHIN GROUP (ORDER BY A) from (select distinct(F3) as A froM T2 where F2 = T1.F1)) as B
from T1;
1) Why can't I access the 'outer' attribute?
Oracle just allows the subquery to access its direct parent query tables... actually, you're trying to access the main query in a subquery of a subquery.
2) How can I access it with less modification
Your inner most subquery could be removed and you could apply a regex to remove duplicates as following:
select
T1.F1,
(
SELECT REGEXP_REPLACE(
LISTAGG(F3,':') WITHIN GROUP (ORDER BY F3),
'([^:]+):(\1(:|$))+',
'\1\3'
)
from T2
where F2 = T1.F1
) as B
from T1;
This regex finds any non-duplicate token (token = data before a : or before the end of line) and checks the next tokens to find any duplicate, replacing all the match for the first non-duplicate found and the : if it's not the end of line.
3) I want to add a column without modyfing the outer query too much
This way your outer query haven't changed, so you can manage it the way you want.

Table name after parenthesis notation

I was going through some old-ish SQL code someone else had written that I couldn't quite understand. I have simplified its structure here, but if anyone can walk me through what exactly is going on, that would be appreciated! You may ignore the specific column operations as they were just examples.
SELECT table.*,
column1 - column2
AS 'col1 - col2',
...
columnn
AS 'coln'
FROM
(SELECT
...
) table
What I don't understand is the final line. I am assuming it is the definition of "table" in the FROM (SELECT ...) part, and the ) table part indicates the name of the defined table.
Thanks in advance!
An inner select needs an alias name
select alias_name.* from
(
select * from some_table ...
) alias_name
The 'table' in that final line is an alias for the subquery.
In T-SQL it is AFAIK mandatory to specify an alias for a subquery if you're selecting from a subquery.
You can name the alias whathever you want. It is perfectly ok to use it like this
select * from
( select * from ... ) as X
(The as keyword is not mandatory, but I always specify the alias-name using 'as').

SELECT query to return a row from a table with all values set to Null

I need to make a query but get the value in every field empty. Gordon Linoff give me the clue to this need here:
SQL Empty query results
which is:
select t.*
from (select 1 as val
) v left outer join
table t
on 1 = 0;
This query wors perfectly on PostgreSQL but gets an error when trying to execute it in Microsoft Access, it says that 1 = 0 expression is not admitted. How could it be fixed to work on microsoft access?
Regards,
If the table has a numeric primary key column whose values are non-negative then the following query will work in Access. The primary key field is [ID].
SELECT t2.*
FROM
myTable AS t2
RIGHT JOIN
(
SELECT TOP 1 (ID * -1) AS badID
FROM myTable AS t1
) AS rowStubs
ON t2.ID = rowStubs.badID
This was tested with Access 2010.
I am offering this answer here, even though you didn't think it worked in my edit to your original question. What is the problem?
select t.*
from (select max(col) as maxval from table as t
) as v left join
table as t
on v.val < t.col;
You can use the following query, but it would still need a little "manual coding".
EDITS:
Actually, you do not need the SWITCH function. Modified query below.
Removed the reference to Description column from one line. Still, you would need to use a Text column name (such as Description) in the last line of the query.
For example, the following query would work for the Months table:
select Months.*
from Months
RIGHT OUTER JOIN
(select "" as DummyColumn from Months) Blank_Data
ON Months.Description = Blank_Data.DummyColumn; --hardcoded Description column