what is wrong with my query in oracle apex? - oracle-apex

The following code does not work, but it does work when I convert it to the next code. Why?
select * ,count(*) over(partition by colour) as counts_by_colour
from bricks;
output:
ORA-00923: FROM keyword not found where expected
modified:
select b.* ,count(*) over(partition by colour) as counts_by_colour
from bricks b;

This is just the way SQL works - nothing to do with APEX.
select * means select all columns from what follows. So...
select * from emp join dept;
...returns all columns from emp and all columns from dept.
You are not allowed to select anything else with select * - e.g. ...
select *, 'abc' from emp;
raises the same error you got.
However, you can use select alias.* to select all columns from one table/view in the query, and then you are also allowed to select other things:
select e.*, 'abc' from emp e;
or
select e.*, d.loc from emp e join dept d;
The "implicit" alias also works:
select emp.*, 'abc' from emp;

Related

Why does Calcite change GROUP_CONCAT to LISTAGG?

I built a RelNode using the following SQL:
SELECT GROUP_CONCAT(ename ORDER BY ename DESC SEPARATOR 'a') FROM emp
and I used RelToSqlConverter to converter it to SQL. I get this SQL:
SELECT LISTAGG(`ename`, 'a') WITHIN GROUP (ORDER BY `ename` IS NULL DESC, `ename` DESC) FROM `emp`
But I want to get GROUP_CONCAT not LISTAGG.
Check https://issues.apache.org/jira/browse/CALCITE-4349
GROUP_CONCAT is analogous to LISTAGG (see CALCITE-2754) (and also to BigQuery and > PostgreSQL's STRING_AGG, see CALCITE-4335). For example, the query
SELECT deptno, GROUP_CONCAT(ename ORDER BY empno SEPARATOR ';')
FROM Emp
GROUP BY deptno
is equivalent to (and in Calcite's algebra would be desugared to)
SELECT deptno, LISTAGG(ename, ';') WITHIN GROUP (ORDER BY empno)
FROM Emp
GROUP BY deptno

Label for a compare (except query) in SAS

hi I'm running the below.
1st query comes back with what I need... I just wanted to add a column to know the inviroment of the object. the 2nd query doesn't work cuz its looking at the 'P' and 'T' column and comparing that
1)
proc sql;
create table ALL_compare_PROD_TEST as
(select * from PROD Except select * from TEST)
UNION ALL
(select * from TEST Except select * from PROD)
ORDER BY code ASC
;QUIT;
2)
proc sql;
create table ALL_compare_PROD_TEST as
(select 'P'as PROD, * from PROD Except select * from TEST)
UNION ALL
(select 'T'as TEST, * from TEST Except select * from PROD)
ORDER BY code ASC
;QUIT;
Sounds like you just need another level of subquery. Also you will want the P and T to be in the same variable.
proc sql;
create table ALL_compare_PROD_TEST as
select 'P' as SRC,* from (select * from PROD except select * from TEST)
UNION ALL
select 'T' as SRC,* from (select * from TEST except select * from PROD)
ORDER BY code ASC
;
quit;

Sql to proc sql conversion

I have 2 tables that contains var values like (ID, date, var1,var2,var3....)
I need to get the data from table2 and add it to table1 for which (ID or date) does not exist in table1.
I am using the below code in sql to get new ID's from tab2 to tab1:
INSERT INTO table1
SELECT * FROM table2 a
WHERE ID not in(select ID from table1 where ID=a.ID)
Here is the code to add new date for existing ID's in tab2 to tab1 :
INSERT INTO table1
SELECT * FROM table2 a
WHERE date not in(select date from table1 where ID=a.ID)
I don't know how to do this in proc sql.
Please share an effective method to do this task.
To insert the new ID I used:
proc sql;
create table lookup as
select a.ID
from table1 a inner join table2 b
on a.ID = b.ID
;
insert into table1
select * from table2 a
where a.ID not in (select ID from lookup)
;
quit;
This works well. But, it failed to insert a date for existing IDs.
Please suggest some ideas to complete this step.
Thanks in advance!
SAS SQL is Similar to the SQl you have written.
The SAME insert statements can be warped as proc sqls in SAS and they work like charm.
If your SQL did work then the following will work too.
PROC SQL;
INSERT INTO work.table1
SELECT * FROM work.table2 a
WHERE ID not in(select ID from work.table1 where ID=a.ID);
INSERT INTO work.table1
SELECT * FROM work.table2 a
WHERE date not in(select date from work.table1 where ID=a.ID)
QUIT;

Joining two sets in SAS SQL with table/correlation error

I am trying to join two datasets. The first dataset1 has two columns item and price. The second dataset2 has three columns - item, customerid, and qty. I need to only include the unique rows from dataset1 that are not in dataset2. While trying to implement this code, I get the error:
Error: Unresolved reference to table/correlation name i.
I am unsure how to fix this error, thanks.
PROC SQL;
create table a as
select *
from dataset1 as i
except corr
select *
from dataset2 as p
where i.item = p.item;
describe table a;
QUIT;
EXCEPT is used to select records in the first set that do not exist in the second set. So if what you want is, to quote you, select records from dataset1 that do not appear in dataset2, you don't need the where clause:
PROC SQL;
create table a as
select *
from dataset1 as i
except corr
select *
from dataset2 as p
;
QUIT;
If however, like that where clause would suggest, you actually want to select records from dataset1 where the value of item is not found in dataset2, you could do this
proc sql;
select *
from dataset1 i
where not exists (select *
from dataset2 p
where i.item=p.item
)
;
quit;
EDIT: following your latest comment, and if you reaaaally need your query to feature an except, this should get you your result
proc sql;
create table a as
select t1.*
from dataset1 t1
inner join (select *
from dataset1 as i
except corr
select *
from dataset2 as p
) t2
on t1.item=t2.item
;
quit;
Even though this will do the same as the query above (with not exists) or, now that I think of it (stupid me), as this:
proc sql;
create table a as
select *
from dataset1
where item not in (select distinct item from dataset2)
;
quit;

Sybase 'select count' not showing up properly, trying to compare two tables

I'm doing a count from table1 whose records/rows don't exist in table2
Here is the query:
select count(1) from table1
where not exists (select 1 from table2 where
table1.col1 = table2.col1
and table2.id=1)
I need to see the records that are missing in table2 , whose id in table2=1, and these records should be available in table1. The PK here is col1.
The query returns me 0. But if I do an excel sheet comparing by removing both the tables to excel. I can find 1591 records that are missing from table1 and are available in table2.
Your query is working fine.
You query finds records that EXISTS in table1 but not in table2
You have found with excel records that does NOT EXISTS in table1 and EXISTS in table2
If you'd like to find these records with SQL than your query should be:
select count(1) from table2
where table2.id=1 and table2.col1 not in (select col1 from table1)
or with not exists version of this query:
select count(1) from table2
where table2.id=1 and
not exists (select 1 from table1 where table1.col1=table2.col1)
I didn't test the queries.