I am working with APEX 4.2.1.00.08 and I keep getting the validation error "LOV query is invalid, a display and a return value are needed, the column names need to be different. If your query contains an in-line query, the first FROM clause in the SQL statement must not belong to the in-line query.". I'm not sure what's causing this. My sql is:
SELECT u1.name d, susu.subunitid r
FROM basic.subunitsuperunit susu
INNER JOIN basic.unit u1 ON susu.subunitid = u1.unitid
INNER JOIN basic.unit u2 ON susu.superunitid = u2.unitid
WHERE level = 3
AND u1.name != 'XYZ'
CONNECT BY PRIOR subunitid = superunitid
START WITH u1.name = (SELECT u3.name FROM basic.unit u3 WHERE u3.unitid = (SELECT TO_NUMBER(gp.value) FROM basic.global_parameters gp WHERE gp.name = 'A_UNIT'))
ORDER BY u1.name
I have made sure there was no semicolon, and I have checked this query in SQL Developer and it runs fine. I already realize those subqueries aren't optimal, but what am I doing wrong here?
Try to create an apex_collection or a database view with your query and then create your lov based on the new object.
I too face same kind of difficulties with other queries and I always solved it so.
This happens also if you begin your query using the WITH clause. You can wrap the whole lot up with
SELECT name d, ID r
From (your query here)
I see this post is old, but I ran into the same error in the "Lists of Values" portion of the application. My issue happened to be that a grant was missing for the applicaton's run as user.
Related
I'm using dbeaver with connection to snowflake database.
I want to select data with join clause.
but I need to do it with parameters.
my code is:
select count(*) from my_table as a ${join}
var join = 'LEFT JOIN table_b AS b ON a.ID = b.ID AND b.NAME = a.NAME'
when I run the select statement (in dbeaver), I get pop up asking me to fill ${join} value,
I put the value in the textbox and the command runs. I get WRONG result! (1,254,242)
But when run the following command:
select count(*) from my_table as a LEFT JOIN table_b AS b ON a.ID = b.ID AND b.NAME = a.NAME
I get correct result (900,254)
anybody can help please? thank you.
General approach to troubleshoot such scenarios:
Check if you are connecting to the same DB/schema from client tool/WebUI
SELECT current_database(), current_schema();
Check if you are using the same user(there may be access issues or Row Level Security applied that could affect number of rows)
SELECT current_user(), current_role();
Check the exact query text sent by client tool in Snowflake History tab and compare against the one run manually.
I'm trying to turn the following SQL query into a view.
SELECT * FROM system_accounts
WHERE system_accounts.id
NOT IN (SELECT account_id
FROM system_group_members
WHERE system_group_members.group_id = 1);
In other words, how do I go about creating a view that gives me all the non-members of a certain group, with those non-members coming from the total set of accounts known to the system?
The query works fine when I test it in my Adminer window. However, I'm at a loss how to express the variable group_id (which in my example is '1') in correct SQL for a view.
I'm sure I'm missing something trivial, but this is the sort of thing I'll bang my head on for hours. Hopefully some kind soul here will help me out.
Many thanks for your time.
You can create a view that has all non-members of all groups. Then you can filter down to the group you want:
CREATE VIEW v_nonmembers AS
SELECT g.group_id, sa.*
FROM system_groups g cross join
system_accounts sa LEFT JOIN
system_group_members sgm
ON gsm.group_id = g.group_id AND
gsm.account_id = sa.id
WHERE gsm.group_id IS NULL;
You can then use it as:
SELECT *
FROM v_nonmembers
WHERE group_id = 1;
I am working on a piece of SQL for a IBM U2 Rocket database. It's not a flavour of db platform I'm familiar with.
I do not have direct access to this database: I can only access it by composing statements in the calling code and testing it from there. That makes it kind of awkward to figure out where the problem is when there are syntax errors.
I'm trying to compose a query which has a condiational on a dervied column. As far as I can tell from what I've read about the database, this ought to work:
SELECT a.*
FROM (
SELECT dp.NAME
,dp.Code
,dp.BusinessType + ts.BusinessType AS bType
FROM dataPoints dp
LEFT OUTER JOIN trialSuppliers ts
WHERE ts.AccountStatus = 'A'
) a
WHERE a.bType LIKE '%cho%'
However, it throws this error:
Died in UCI::SQLExecDirect() with SQLSTATE 37000, Native error:0
[IBM][SQL Client][UNIDATA]
If you just run the inner query, it works fine. Trying to use any kind of inner select statement causes it to throw the same error, i.e. this:
SELECT *
FROM (
SELECT dp.NAME
,dp.Code
,dp.BusinessType + ts.BusinessType AS bType
FROM dataPoints dp
LEFT OUTER JOIN trialSuppliers ts
WHERE ts.AccountStatus = 'A'
)
Still fails.
What's the correct syntax to be able to filter my query by the derived column?
Just doing some clean-up, did the last comment resolve the issue?
I am not a UniData user but in its half cousin UniVerse the "from clause" has to be a table. You have to do sub-queries in the where clause. I would do what you want to do adding a couple of I-descriptors in the dictionary. I have often found the limited SQL compliance of the U2 products to be somewhat of a hindrance, but when your metadata is external to and separate from your data you are going to lose a bit of structured query sanity. – Van Amburg Sep 21 '17 at 17:31
I'm trying to put together a query that updates a field within a table. I'm attempting to run a sub select query that gives me a number, and then use that number that resulted from the sub-query as part of the criteria for the update query.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =[tbl_Foundation_Account].[Client_ID]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID =
[2_Import_tbl_AWCDSU].[ECPD Profile ID]))
My issue is I keep receiving this error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
Am I using the sub-query incorrectly? When I've received this error before, it's been because of some ambiguity in the table or field names, but this time I've checked for all that and it should be fine. Can anyone explain what SQL sin I have committed?
On the error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
This is because the table column [tbl_Foundation_Account].[Client_ID] does not exists in the scope of outer UPDATEquery .
The only table the outer query has an inkling about is [2_import_VZW_tbl_SMTN] and it does not have a column like [tbl_Foundation_Account].[Client_ID].
It is akin to writing a column name with a typo or like you said
When I've received this error before, it's been because of some
ambiguity in the table or field names
Please try a query like below.
Note that I am using Inner query syntax and ensuring that a single value is returned by using
select top 1 [Client_ID]
in the inner query. rest of the query syntax is same.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =
(
select top 1 [Client_ID]
from [tbl_Foundation_Account]
where [Foundation_Account_ID] =
(
Select TOP 1 a.Foundation_Account_ID
FROM tbl_Foundation_Account a
INNER JOIN [2_Import_tbl_AWCDSU] b
ON a.Foundation_Account_ID = b.[ECPD Profile ID]
)
)
Another poster submitted this answer earlier, but then deleted it. I was able to try it before they deleted it and it works exactly how I needed it to work. I will use this as the right answer unless someone else can tell me why this is a bad Idea.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] = [tbl_Foundation_Account].[Client_ID]
from [tbl_Foundation_Account]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID = [2_Import_tbl_AWCDSU].[ECPD Profile ID]))
I have an order system. Users with can be attached to different orders as a type of different user. They can download documents associated with an order. Documents are only given to certain types of users on the order. I'm having trouble writing the query to check a user's permission to view a document and select the info about the document.
I have the following tables and (applicable) fields:
Docs: DocNo, FileNo
DocAccess: DocNo, UserTypeWithAccess
FileUsers: FileNo, UserType, UserNo
I have the following query:
SELECT Docs.*
FROM Docs
WHERE DocNo = 1000
AND EXISTS (
SELECT * FROM DocAccess
LEFT JOIN FileUsers
ON FileUsers.UserType = DocAccess.UserTypeWithAccess
AND FileUsers.FileNo = Docs.FileNo /* Errors here */
WHERE DocAccess.UserNo = 2000 )
The trouble is that in the Exists Select, it does not recognize Docs (at Docs.FileNo) as a valid table. If I move the second on argument to the where clause it works, but I would rather limit the initial join rather than filter them out after the fact.
I can get around this a couple ways, but this seems like it would be best. Anything I'm missing here? Or is it simply not allowed?
I think this is a limitation of your database engine. In most databases, docs would be in scope for the entire subquery -- including both the where and in clauses.
However, you do not need to worry about where you put the particular clause. SQL is a descriptive language, not a procedural language. The purpose of SQL is to describe the output. The SQL engine, parser, and compiler should be choosing the most optimal execution path. Not always true. But, move the condition to the where clause and don't worry about it.
I am not clear why do you need to join with FileUsers at all in your subquery?
What is the purpose and idea of the query (in plain English)?
In any case, if you do need to join with FileUsers then I suggest to use the inner join and move second filter to the WHERE condition. I don't think you can use it in JOIN condition in subquery - at least I've never seen it used this way before. I believe you can only correlate through WHERE clause.
You have to use aliases to get this working:
SELECT
doc.*
FROM
Docs doc
WHERE
doc.DocNo = 1000
AND EXISTS (
SELECT
*
FROM
DocAccess acc
LEFT OUTER JOIN
FileUsers usr
ON
usr.UserType = acc.UserTypeWithAccess
AND usr.FileNo = doc.FileNo
WHERE
acc.UserNo = 2000
)
This also makes it more clear which table each field belongs to (think about using the same table twice or more in the same query with different aliases).
If you would only like to limit the output to one row you can use TOP 1:
SELECT TOP 1
doc.*
FROM
Docs doc
INNER JOIN
FileUsers usr
ON
usr.FileNo = doc.FileNo
INNER JOIN
DocAccess acc
ON
acc.UserTypeWithAccess = usr.UserType
WHERE
doc.DocNo = 1000
AND acc.UserNo = 2000
Of course the second query works a bit different than the first one (both JOINS are INNER). Depeding on your data model you might even leave the TOP 1 out of that query.