Getting error ORA-00909: invalid number of arguments - sql

CREATE VIEW ITCC.release_testcase_count
AS
(
SELECT CONCAT(rtm.requirement_id,'-',tct.release_id) AS id,
rtm.requirement_id AS requirement_id,
tct.release_id AS release_id,
COUNT(tct.release_id) AS testcase_count
from testcase_version tcv
INNER JOIN tcr_catalog_tree_testcase tct ON tcv.id = tct.testcase_version_id
LEFT JOIN requirement_testcase_mapping rtm ON rtm.testcase_id=tcv.testcase_id
GROUP BY tct.release_id , rtm.requirement_id
);
same query is working for ms sql and my sql without any syntax error. i want to execute it in oracle as well but i am getting error for the same

The Oracle CONCAT function only takes two, not three or more, parameters. Instead of using CONCAT, just use the concatenation operator:
CREATE VIEW ITCC.release_testcase_count AS (
SELECT rtm.requirement_id || '-' || tct.release_id AS id,
...
)
Or, if you really want to use CONCAT here, then you may chain them together:
CREATE VIEW ITCC.release_testcase_count AS (
SELECT CONCAT(rtm.requirement_id, CONCAT('-', tct.release_id)) AS id,
...
)

Related

PyPika how to select star minus a column

I'm trying to use PyPika to build a select statement. Both tables have an id column, and I'm trying to select all columns from both tables except the id column from the first table.
This is the query I want to end up with, which runs properly in BigQuery:
SELECT t.* EXCEPT(id), s.* from t join s on t.id = s.id
I haven't been able to figure out how to get the EXCEPT into the select clause.
I've tried the following syntax, but the except_of() isn't allowed in this context:
t = Table("t")
s = Table("s")
query = Query.from_(t).join(s).on(t.id == s.id).select(t.star.except_of("id")).select(s.star)
I also tried a custom function, but that puts an invalid comma in the resulting query. This is what I tried:
except_select = CustomFunction("EXCEPT", ["column_to_exclude"])
query = Query.from_(t).join(s).on(t.id == s.id).select(t.star).except_select("id")).select(s.star)
which incorrectly generates
SELECT t.*, EXCEPT(id), s.* from t join s on t.id = s.id
Does anyone have any suggestions on how to solve this problem?

TSQL / SQL - Error: Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I'm hoping someone can point me in the right direction here.
DB #1 NAME: "Wayin_Integration_SFMC_AD"
And I'm trying to merge with this Master table (DB #2): "Master_Users_SVOC_test_vt"
Assuming everyone in "Wayin_Integration_SFMC_AD" already has a primary key and record in "Master_Users_SVOC_test_vt", now I want to MERGE INTO the remaining data for that record.
Problem is, most of my fields aren't included in the GROUP BY clause.
Do I need to use a subquery here? I am very new to SQL. Here is the snippet that I am unsure of. Without the GROUP BY this would work, but it's the grouping that is confusing me.
ERROR MESSAGE: "Column 'Wayin_Integration_SFMC_AD.First_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
"
which is the first field not included in the GROUP BY clause.
THIS IS THE SQL THAT ISN'T WORKING:
MERGE INTO [bo_marketing_capability_dev].[dbo].[Master_Users_SVOC_test_vt] AS m
USING (
SELECT
m.subscriber_key,
m.email_address,
w.[First_Name] AS first_name,
w.[Last_Name] AS last_name,
, MAX (w.DateAdded) AS wayin_DateAdded
FROM Wayin_Integration_SFMC_AD w
INNER JOIN Master_Users_SVOC_test_vt m
ON w.Email = m.email_address
GROUP BY subscriber_key, w.[Email], m.email_address
) AS SRC
ON ([SRC].[email_address] = [m].[email_address])
MERGE INTO [bo_marketing_capability_dev].[dbo].[Master_Users_SVOC_test_vt] AS m
USING (
SELECT
m.subscriber_key,
m.email_address,
w.[First_Name] AS first_name,
w.[Last_Name] AS last_name,
, MAX (w.DateAdded) AS wayin_DateAdded
FROM Wayin_Integration_SFMC_AD w
INNER JOIN Master_Users_SVOC_test_vt m
ON w.Email = m.email_address
GROUP BY m.subscriber_key, m.email_address, w.[First_Name], w.[Last_Name]
) AS SRC
ON ([SRC].[email_address] = [m].[email_address])
Try above code.
GROUP BY should have all non aggregate columns.

Reuse function call inside select statement

Simplified example of the problem:
select p.id,
p.name,
-- other columns from joined tables
decode(get_complicated_number(p.id), null, null, "The number is: " || get_complicated_number(p.id)))
from some_table p
-- join other tables and WHERE clause
It includes get_complicated_number call which queries multiple tables. I wasn't able to write it as a JOIN statement that would be as fast and as easy to maintain as a separate function so far.
Currently the function is called twice in case its return value is not NULL.
In reality I have an XML generation package that gets the data with a select:
select distinct xmlAgg
(
xmlelement
(
"TestElement",
xmlelement("Id", p.id),
xmlelement("Name", p.name),
-- other elements from joined tables
decode(get_complicated_number(p.id), null, null, xmlelement("ComplicatedNum", get_complicated_number(p.id)))
)
)
from some_table p
-- join other tables and WHERE clause
Is there a way to make it only one call and still avoid creating an empty element on NULL?
You can use WITH Syntax (Common Table Expressions) as:
with complicated_number as (
select get_complicated_number(p.id) as num from some_table p
) select distinct xmlAgg
--...
decode(complicated_number.num, null, null, xmlelement("ComplicatedNum", complicated_number.num))
from complicated_number
common table expression (CTE) is a named temporary result set that exists within the scope of a single statement and that can be referred to later within that statement, possibly multiple times
user7294900's answer is good, but if it's hard to combine with your existing joins, here's an alternate version with an inline view instead of a CTE.
select distinct xmlAgg
(
xmlelement
(
"TestElement",
xmlelement("Id", p2.id),
xmlelement("Name", p2.name),
-- other elements from joined tables
decode(p2.num, null, null, xmlelement("ComplicatedNum", p2.num))
)
)
from (
select p.id, p.name, get_complicated_number(p.id) as num
from some_table p
) p2
-- join other tables to p2. or put them inside it.
If you want help with adding your existing joins to these example queries, you might need to edit your question and add your other tables and WHERE clauses.

QuerySyntaxException when using SQL EXCEPT and count(*)

I have a situation in Hibernate where I need to get the count(*) on a SQL EXCEPT query. Below is the query (imitated my original code):
String query = """
select count(*) as totalCount
from ( select distinct id from Employee
where name like '%Roger%
EXCEPT select distinct id from Manager ) Temporary
"""
Now, when I say:
hibernateSession.createQuery(query);
The below exception is thrown:
org.hibernate.hql.ast.QuerySyntaxException:
unexpected token: ( near line 1, column 36
My logs also show the below parsing errors when I catch the exception:
org.hibernate.hql.PARSER line 1:36: unexpected token: (
org.hibernate.hql.PARSER line 14:315: unexpected token: EXCEPT
org.hibernate.hql.PARSER line 15:68: unexpected token: from
I cannot avoid the count, WHERE or EXCEPT.
Because you're using hibernateSession.createQuery(query), hibernate is creating a query using HQL syntax, which doesn't work with your query, as you're using SQL syntax.
You most likely need to use something resembling hibernateSession.createSQLQuery(query).
For more on using native sql queries, see Native SQL in the Hibernate documentation.
The answers to this related question might also be useful.
Can you try this query instead? Replaced EXCEPT with NOT EXISTS.
String query = """
select count(*) as totalCount
from ( select distinct id from Employee as emp
where emp.name like '%Roger%
and not exists (
from Manager as m where emp.id = m.id
)
) Temporary
"""

Need help with error in Oracle SQL query

this is the query:
SELECT DISTINCT pprom.pk
FROM
(
SELECT
item_t0.SourcePK as pk
FROM
links item_t0
WHERE (? = item_t0.TargetPK AND item_t0.SourcePK in (?,?))
AND (item_t0.TypePkString=? )
UNION
SELECT
item_t1.TargetPK as pk
FROM
cat2prodrel item_t1
WHERE ( item_t1.SourcePK in (? ) AND item_t1.TargetPK in (?,?))
AND (item_t1.TypePkString=? )
) AS pprom
And this is the error:
ORA-00933: SQL command not properly ended
Any ideas what could be wrong?
Edit:
The question marks are replaced by PKs of the respective items:
values = [PropertyValue:8802745684882, PropertyValue:8796177006593, PropertyValue:8796201713665, 8796110520402, PropertyValue:8796125954190, PropertyValue:8796177006593, PropertyValue:8796201713665, 8796101705810]
Edit 2:
The query is executed deep inside some proprietary software system so I don't know exactly the code that runs it.
Edit 3:
I found one more query that's a little shorter but results in the same error message:
SELECT DISTINCT pprom.pk
FROM
(
SELECT
item_t0.SourcePK as pk
FROM
links item_t0
WHERE (? = item_t0.TargetPK AND item_t0.SourcePK in (?))
AND (item_t0.TypePkString=? )
) AS pprom
Using the following values:
values = [PropertyValue:8799960601490, PropertyValue:8796177006593, 8796110520402]
Edit 4
I found the SQL code that is sent to the db after replacing the values:
SELECT DISTINCT pprom.pk
FROM
(
SELECT
item_t0.SourcePK as pk
FROM
links item_t0
WHERE (8801631769490 = item_t0.TargetPK AND item_t0.SourcePK in (8796177006593))
AND (item_t0.TypePkString=8796110520402 )
) AS pprom
I also tried executing the inner SELECT statement and that alone runs ok and returns a single PK as a result.
I could not find any obvious syntax error in your query so I'd presume the issue is the client library you are using to convert the ? place holders into actual values. Your question edit displays a sort of dump where there are 8 integers but only 6 PropertyValue items. Make sure that's not the issue: IN (?, ?) requires 2 parameters.
Edit
Try removing the AS keyword when you assign an alias to the subquery:
SELECT DISTINCT pprom.pk
FROM
(
SELECT
item_t0.SourcePK as pk
FROM
links item_t0
WHERE (8801631769490 = item_t0.TargetPK AND item_t0.SourcePK in (8796177006593))
AND (item_t0.TypePkString=8796110520402 )
) AS pprom
^^