How to write DELETE Statement with Inner Query in PostgreSQL? - sql

The below query works fine in Oracle DB.
The same query fails in Postgres DB, but the inner query works fine in Postgres DB.
DELETE FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY A.RESORT, A.RESV_NAME_ID ORDER BY ACTION_ID DESC) RNK
FROM STAGE_DETAILS A ) B
WHERE B.RNK>1;
I am getting syntax error for this.
Error :-
[Code: 0, SQL State: 42601] ERROR: syntax error at or near "(" Position: 13 [Script position: 3109 - 3110]
How to fix this ?
Thanks.

use following syntax
DELETE FROM STAGE_DETAILS A
USING (SELECT ACTION_ID,RESV_NAME_ID,ROW_NUMBER() OVER (PARTITION BY RESORT, RESV_NAME_ID ORDER BY ACTION_ID DESC) RNK
FROM STAGE_DETAILS) B
WHERE B.RNK>1 AND B.ACTION_ID = A.ACTION_ID AND B.RESV_NAME_ID = A.RESV_NAME_ID;

You cannot delete from a subquery in Postgres. This should do what you want:
DELETE FROM STAGE_DETAILS SD
WHERE ACTION_ID < (SELECT MAX(SD2.ACTION_ID)
FROM STAGE_DETAILS SD2
WHERE SD2.RESORT = SD.RESORT AND
SD2.RESV_NAME_ID = SD.RESV_NAME_ID
);

Related

I am trying to create a table in teradata and it doesn't work

I am trying to create a table in teradata with sql, but I keep getting the following error:
CREATE TABLE FAILED. [3707] Syntax error, expected something like a name or a Unicode delimited identifier or an 'UDFCALLNAME' keyword or a 'SELECT' keyword or '(' between '(' and the 'WITH' keyword
My goal is to create a table that takes the maximum data, named "verwerkingdatum" in my code for every "contract_nr". Without the create table statement it worked just fine. Now I'm trying to create a table out of this. But I get the error above.
Here is my code:
create table mi_temp.beslagrek_saldo as
(SEL * FROM( WITH x AS
(
SELECT geld_contract_event_id, contract_nr, contract_soort_code,
contract_hergebruik_volgnr,
verwerking_datum,
event_dat,
valuta_code,
saldo_na_muteren_orig,
saldo_na_muteren_eur,
saldo_na_muteren_dc_ind,
valuta_datum,
geld_transactie_soort_code,
tegenrekening_nr,
tegenrekening_naam,
boek_datum,
storno_ind,
mutatie_bedrag_orig,
mutatie_bedrag_eur,
mutatie_bedrag_dc_ind,
soort_overboeking,
tegenrekening_nr_num,
automaat_transactie_type,
automaat_id,
automaat_datum,
automaat_tijd,
ROW_NUMBER() OVER (PARTITION BY contract_nr ORDER BY
verwerking_datum DESC) AS RowNum
FROM MI_VM_Ldm.vgeld_contract_event
WHERE verwerking_datum >= 1181201 AND verwerking_datum <= 1181231
)
SELECT geld_contract_event_id, contract_nr, contract_soort_code,
contract_hergebruik_volgnr,
verwerking_datum,
event_dat,
valuta_code,
saldo_na_muteren_orig,
saldo_na_muteren_eur,
saldo_na_muteren_dc_ind,
valuta_datum,
geld_transactie_soort_code,
tegenrekening_nr,
tegenrekening_naam,
boek_datum,
storno_ind,
mutatie_bedrag_orig,
mutatie_bedrag_eur,
mutatie_bedrag_dc_ind,
soort_overboeking,
tegenrekening_nr_num,
automaat_transactie_type,
automaat_id,
automaat_datum,
automaat_tijd
FROM X
WHERE RowNum = 1))
If I'm reading your post correctly, are you are trying to is filter your select so that your rownum = 1. You can just use qualify to accomplish that.
create table foo as (
SELECT geld_contract_event_id, contract_nr, contract_soort_code,
contract_hergebruik_volgnr,
verwerking_datum,
event_dat,
valuta_code,
saldo_na_muteren_orig,
saldo_na_muteren_eur,
saldo_na_muteren_dc_ind,
valuta_datum,
geld_transactie_soort_code,
tegenrekening_nr,
tegenrekening_naam,
boek_datum,
storno_ind,
mutatie_bedrag_orig,
mutatie_bedrag_eur,
mutatie_bedrag_dc_ind,
soort_overboeking,
tegenrekening_nr_num,
automaat_transactie_type,
automaat_id,
automaat_datum,
automaat_tijd
from
MI_VM_Ldm.vgeld_contract_event
WHERE verwerking_datum >= 1181201 AND verwerking_datum <= 1181231
qualify ROW_NUMBER() OVER (PARTITION BY contract_nr ORDER BY verwerking_datum DESC) =1
) with data;

Hive - select rows within 1 year of earliest date

I am trying to select all rows in a table that are within 1 year of the earliest date in the table. I'm using the following code:
select *
from baskets a
where activitydate < (select date_add((select min(activitydate) mindate_a from baskets), 365) date_b from baskets)
limit 10;
but get the following error message:
Error while compiling statement: FAILED: ParseException line 1:55 cannot recognize input near 'select' 'date_add' '(' in expression specification
Total execution time: 00:00:00.338
Any suggestions?
EDIT:
With this code:
select *
from baskets a
where activitydate < (select date_add(min(activitydate), 365) from baskets)
limit 10;
I'm getting this error:
Error while compiling statement: FAILED: ParseException line 1:55 cannot recognize input near 'select' 'date_add' '(' in expression specification
I'd be tempted to use window functions:
select b.*
from (select b.*, min(activity_date) as min_ad
from baskets b
) b
where activity_date < add_months(min_ad, 12);
If you really want your syntax to work, try reducing the number of selects:
where activitydate < (select date_add(min(activitydate), 365) from baskets)
Use JOINs instead of select in Sub-query. I don't think Hive supports select in where clause with < condition. Only IN and EXISTS could be used as of Hive 0.13.
: Language Manual SubQueries
SELECT a.*
FROM baskets a
JOIN (SELECT DATE_ADD(MIN(b.activitydate), 365) maxdate
FROM baskets) b
ON a.activitydate < b.maxdate
LIMIT 10;

Invalid Identifier on Update (Oracle)

I try to execute following command:
UPDATE DB_TEST.STOCK_ITEMS
SET STATUS = (SELECT *
FROM (SELECT STOCK_ITEM_STATUS
FROM DB_TEST.STOCK_ITEMS_HISTORY
WHERE STOCK_ITEM_ID = DB_TEST.STOCK_ITEMS.ID
ORDER BY CHANGED_ON DESC, ID DESC)
WHERE ROWNUM <= 1)
WHERE EXISTS (SELECT *
FROM DB_TEST.STOCK_ITEMS_HISTORY
WHERE STOCK_ITEM_ID = DB_TEST.STOCK_ITEMS.ID);
But I get the error:
SQL-Error: ORA-00904: "DB_TEST"."STOCK_ITEMS"."ID": invalid identifier
I looked up the Oracle error, but all I get is that I supposedly used a wrong or missing column name, but the DB_TEST.STOCK_ITEMS.ID field definitely exists.
What other reasons can cause this error?
Oracle limits the scope of a table to one level of subqueries. Here is a method that solves your problem using keep:
UPDATE DB_TEST.STOCK_ITEMS
SET STATUS = (SELECT MAX(STOCK_ITEM_STATUS) KEEP (DENSE_RANK FIRST ORDER BY CHANGED_ON DESC, ID DESC)
FROM DB_TEST.STOCK_ITEMS_HISTORY
WHERE STOCK_ITEM_ID = DB_TEST.STOCK_ITEMS.ID
)
WHERE EXISTS (SELECT 1
FROM DB_TEST.STOCK_ITEMS_HISTORY
WHERE STOCK_ITEM_ID = DB_TEST.STOCK_ITEMS.ID
);

Is syntax like "select field1,field2,field3 from A where (a,b) in (select a,b from B)" supported in derby jdbc?

I once wrote similar queries as below in oracle, and it worked at that time. now I try to do put this in prepareStatement of Derby JDBC.
SELECT THREADID,THREADID2,SIMILARITY FROM S WHERE
(THREADID,THREADID2) IN
(
SELECT T1.ID,T2.ID FROM
(
( SELECT T.ID FROM T WHERE T.POSTTYPEID = '1' ORDER BY ANSWERCOUNT DESC FETCH FIRST 200 ROWS ONLY ) AS T1
JOIN
( SELECT T.ID FROM T WHERE T.POSTTYPEID = '1' ORDER BY ANSWERCOUNT DESC FETCH FIRST 200 ROWS ONLY ) AS T2
)
)
It turned out that I got errors as:
java.sql.SQLSyntaxErrorException: Syntax error:Encountered "," at line 1, column 78
I checked the code, and this error points to the (THREADID,THREADID2) part, is it that this is not supported in JDBC Derby?
no
(THREADID,THREADID2) IN
not working
you must seperate like
WHERE THREADID IN ('your condition') OR THREADID2 IN ('your condition')

HQL syntax problem

I have a problem with the following HQL query:
select sum(MYTABLE.COUNTER) from (
select count(DISTINCT bi.products.id) as COUNTER
from BusinessInformation as bi
where bi.informationOwners.id in (100)
and bi.products.id in (10)
and bi.valueAmount not in ('NA')
and ((bi.valueType = 'ACHIEVED' and bi.referenceYears.id = 1) or (bi.valueType = 'FINAL_BALANCE' and bi.referenceYears.id = 2))
group by bi.informationOwners.id
) MYTABLE
The compiler reports:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 34
Do you have any idea what's wrong with the query? I tested the inner query and it works fine.
Thanks,
C
HQL subqueries can occur only in the select or where clauses, not FROM.