ORA-00936: missing expression oracle - sql

I have this query
SELECT DAL_ROWNOTABLE.DAL_ID FROM
(
SELECT ticket.id AS "DAL_ID", ROWNUMBER ( Order By ticket.id ) AS "DAL_ROWNUMBER"
FROM ticket_table ticket
WHERE ( ticket.type = N'I' )
AND
(
ticket.tenant IS NULL OR ticket.tenant IN
(
SELECT * FROM
(
SELECT tenant_group_member.tenant_id
FROM tenant_group_member
WHERE tenant_group_member.tenant_group = HEXTORAW('30B0716FEB5F4E4BB82A7B7AA3A1A42C')
ORDER BY ticket.id
)
)
)
) DAL_ROWNOTABLE
WHERE DAL_ROWNOTABLE.DAL_ROWNUMBER BETWEEN 1 AND 21
What is the problem with the allow query that is throwing ORA-00936 missing expression? anyone? Any help will be appreciated...Error thrown at column:80 which is at the beginning of first order by:

ORA-00936 usually indicates a syntax error.
ROWNUMBER is not an Oracle function. Unless you have a user-defined function of that name I suspect the function you're looking for is ROW_NUMBER().

Your query can be much simplified. It has things like extra layers of subqueries and an unnecessary order by in an in subquery. What you want to do with rownumber you can do with just rownum:
SELECT DAL_ROWNOTABLE.DAL_ID
FROM (SELECT ticket.id AS "DAL_ID"
FROM ticket_table ticket
WHERE (ticket.type = N'I' ) AND
(ticket.tenant IS NULL OR
ticket.tenant IN (SELECT tgm.tenant_id
FROM tenant_group_member tgm
WHERE tgm.tenant_group = HEXTORAW('30B0716FEB5F4E4BB82A7B7AA3A1A42C')
)
)
ORDER BY ticket.id
) DAL_ROWNOTABLE
WHERE rownum <= 21;

Related

"ORA-00923: FROM keyword not found where expected\n what should I fix

I have an oracle query as follows but when I make changes to pagination the results are different. what should i pass for my code
SELECT *
FROM (
SELECT b.*,
ROWNUM r__
FROM (
select a.KODE_KLAIM,
a.NO_SS,
a.LA,
a.NAMA_TK,
a.KODE_K,
(
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K and rownum=1
) TEM_LAHIR,
(
select TO_CHAR(tk.TLAHIR, 'DD/MM/RRRR')
from KN.VW_KTK tk
where tk.KODE_K = a.KODE_K
and rownum=1
) TLAHIR
from PN.KLAIM a
where nvl(a.STATUS_BATAL,'X') = 'T'
and A.NOMOR IS NOT NULL
and A.TIPE_KLAIM = 'JPN01'
)b
)
where 1 = 1
WHERE ROWNUM < ( ( ? * ? ) + 1 )
WHERE r__ >= ( ( ( ? - 1 ) * ? ) + 1 )
but i run this query i have result ORA-00900: invalid SQL statement
You have three WHERE clauses at the end (and no ORDER BY clause). To make it syntactically valid you could change the second and third WHERE clauses to AND.
However, you mention pagination so what you probably want is to use:
SELECT *
FROM (
SELECT b.*,
ROWNUM r__
FROM (
select ...
from ...
ORDER BY something
)b
WHERE ROWNUM < :page_size * :page_number + 1
)
WHERE r__ >= ( :page_number - 1 ) * :page_size + 1
Note: You can replace the named bind variables with anonymous bind variables if you want.
Or, if you are using Oracle 12 or later then you can use the OFFSET x ROWS FETCH FIRST y ROWS ONLY syntax:
select ...
from ...
ORDER BY something
OFFSET (:page_number - 1) * :page_size ROWS
FETCH FIRST :page_size ROWS ONLY;
Additionally, you have several correlated sub-queries such as:
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K and rownum=1
This will find the first matching row that the SQL engine happens to read from the datafile and is effectively finding a random row. If you want a specific row then you need an ORDER BY clause and you need to filter using ROWNUM AFTER the ORDER BY clause has been applied.
From Oracle 12, the correlated sub-query would be:
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K
ORDER BY something
FETCH FIRST ROW ONLY

Oracle SQL use subquery value in another subquery

Before I go any further please mind that I am not well experienced with SQL.
I have one query that is getting a single value (netto value) such as:
WITH cte_value_net AS (
SELECT
nvl(min(value_net),0) as value_net
FROM (
SELECT
i.serial as serial,
nvl(lag(i.value_net) OVER (PARTITION BY i.serial ORDER BY i.month), i.value_net) as value_net
FROM
inventory i
WHERE
i.ctypde IN (
SELECT
ctypde
FROM
appar ap
WHERE
ap.serial = in_serial -- this is the variable I want to set
)
AND
i.month IN (to_char(add_months(sysdate, -1), 'YYYYMM'), to_char(add_months(sysdate, -2), 'YYYYMM'))
AND
i.serial = in_serial -- this is the variable I want to set
) vn
GROUP BY vn.serial
)
In here I have to feed in the variable in_serial that I thought I could get from another subquery such as:
SELECT
(SELECT * FROM cte_value_net) AS value_net
FROM (
SELECT
lap.serial AS in_serial
FROM
applap lap
)
but I can not wrap my head around it why this in_serial is not visible to my custom CTE. Could someone explain me how can I propagate the value from subquery like this?
The error I am obviously getting is:
SQL Error [904] [42000]: ORA-00904: "IN_SERIAL"
Unfortunately I do not have any sample data. What I want to achieve is that I could feed in the returned in_serial from main subquery to my CTE.
Before I can get value_net I need my main query to return the in_serial, otherwise I do not have access to that value.
The trick I use is to produce an extra CTE that I usually call params that includes a single row with all computed parameters. Then, it's a matter of performing a CROSS JOIN with this CTE in any other CTE, subquery or main query, as needed.
For example:
with
params as ( -- 1. Create a CTE that returns a single row
select serial as in_serial from applap
),
cte_value_net AS (
select ...
from inventory i
cross join params -- 2. cross join against the CTE anywhere you need it
where ...
and i.serial = params.in_serial -- 3. Use the parameter
)
select ...
First, your second query is not syntactically correct, as there's a ',' before the FROM. You can write your query like this:
WITH cte_value_net AS (
SELECT
serial, nvl(min(value_net),0) as value_net
FROM (
SELECT
i.serial as serial,
nvl(lag(i.value_net) OVER (PARTITION BY i.serial ORDER BY i.month), i.value_net) as value_net
FROM
inventory i
WHERE
i.ctypde IN (
SELECT
ctypde
FROM
appar ap
WHERE
ap.serial = i.serial
)
AND
i.month IN (to_char(add_months(sysdate, -1), 'YYYYMM'), to_char(add_months(sysdate, -2), 'YYYYMM'))
) vn
GROUP BY vn.serial
)
select ...
from cte_value_net s join applap lap on (lap.serial=s.serial)
(adjust query to your schema ....)

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;

PostgreSQL grouping error with Rails

I have the following query:
SELECT
EXTRACT (
HOUR
FROM
interventions.created_at :: TIMESTAMP
) AS DATE,
COUNT (interventions. ID) AS total
FROM
"interventions"
INNER JOIN medical_records ON (
(
medical_records.medical_recordable_type = 'Intervention'
)
AND (
medical_records.medical_recordable_id = interventions. ID
)
)
INNER JOIN patients ON (
patients. ID = medical_records.patient_id
)
WHERE
(
interventions.created_at BETWEEN '2011-11-10 00:00:00'
AND '2014-11-10 00:00:00'
)
AND (
medical_records.hospitalization = FALSE
)
AND (
patients.birth_date BETWEEN '1892-12-31 23:50:39'
AND '2013-12-31 22:59:59'
)
GROUP BY
DATE
ORDER BY
interventions. ID DESC,
DATE ASC
And I get the following error:
PG::GroupingError: ERROR: column "interventions.id" must appear in the GROUP BY clause or be used in an aggregate function
Here is the relevant part of my code :
query = query.select("EXTRACT(HOUR FROM interventions.created_at::timestamp) AS date, COUNT(interventions.id) AS total")
query = query.group("EXTRACT(HOUR FROM interventions.created_at::timestamp)")
query = query.order("EXTRACT(HOUR FROM interventions.created_at::timestamp) ASC")
I don't understand because the column "interventions.id" is used in the COUNT() function inside the select.
Any idea on how to resolve this issue?
Thank you.
Remove interventions_id from ORDER BY clause.

Bubbling Up Columns in Sql

Pardon the convoluted example, but I believe there is something fundamental about sql I am missing and I'm not sure what it is. I have this crazy query...
SELECT *
FROM (
SELECT *
FROM (
SELECT #t1 := #t1 +1 AS leaderboard_entry_youngness_rank, 1 - #t1 /100 AS
leaderboard_entry_youngness_based_on_expiry, leaderboard_entry . * ,
NOW( ) - leaderboard_entry_timestamp AS leaderboard_entry_age_in_some_units,
TO_DAYS( NOW( ) ) - TO_DAYS( leaderboard_entry_timestamp )
AS leaderboard_entry_age_in_days
FROM leaderboard_entry) AS inner_temp
NATURAL JOIN leaderboard
NATURAL JOIN user
WHERE (
leaderboard_load_key = 'sk-en-adjectives-1'
OR leaderboard_load_key = '-sk-en-adjectives-1'
)
AND leaderboard_quiz_mode = '0'
ORDER BY leaderboard_entry_age_in_some_units ASC , leaderboard_entry_timestamp ASC
LIMIT 0 , 100
) AS outer_temp
ORDER BY leaderboard_entry_elapsed_time_ms ASC , leaderboard_entry_timestamp ASC
LIMIT 0 , 50
I added the second nested SELECT statement because the user_name in the user table was not being returned in the outermost query. But now the leaderboard_entry_youngness_based_on_expiry field, which is being generated based on a row index ratio, is not working correctly.
If I remove the second nested SELECT statement, the leaderboard_entry_youngness_based_on_expiry works as expected, but the user_name column is not returned.
How can I satisfy both? Why is this happening?
Thanks!
This stems from the following question:
Add a numbered list column to a returned MySQL query
In your inner SELECT statement, you do not have user.user_name, that's why username is not returned. Remove the outer query, do it like earlier but with user.user_name like this:
....
SELECT #t1 := #t1 +1 AS leaderboard_entry_youngness_rank, 1 - #t1 /100 AS
leaderboard_entry_youngness_based_on_expiry, leaderboard_entry . * ,
NOW( ) - leaderboard_entry_timestamp AS leaderboard_entry_age_in_some_units,
TO_DAYS( NOW( ) ) - TO_DAYS( leaderboard_entry_timestamp )
AS leaderboard_entry_age_in_days, user.user_name
....
Try putting a ORDER BY in the inner most query, since there currently is no ORDER BY clause, its wrong to say that "is not working correctly".
Check if you take away the outer SELECT * FROM..., see if there are duplicate user_name columns.
BTW, Since you are not using the row index columns in your query, why not just put this logic in the application itself? it will be more reliable doing so.