Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The idea is to select 2 column response representing the table name and a boolean flag if it exists in the database for predefined list of tables. Is it possible to make such request in a single query?
You can use USER_TABLES dictionary view.
This view contains detail of the all tables that is owned by the current user.
You can achieve the desired result using following query:
WITH PREDEFINED_LIST_TABLES AS
(SELECT 'TABLE1' TABLE_NAME FROM DUAL UNION ALL
....
....)
SELECT P.TABLE_NAME ,
CASE WHEN U.TABLE_NAME IS NOT NULL THEN 1 ELSE 0 END AS TAB_EXISTS
FROM PREDEFINED_LIST_TABLES P
LEFT JOIN USER_TABLES U
ON (P.TABLE_NAME = U.TABLE_NAME);
Cheers!!
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I have this SQL statement:
select
v.venue_id, v.name
from
venues v
where
not exists (select e.vid
from events e
where e.vid = v.venue_id);
Here is the result of the query:
It works fine but the last row in the result is null, how do I remove that? Been trying for a bit now with no luck
I think this might be what you're experiencing
Why does SQL workbench always return a row full of null values in every query?
Looks like that's normal behavior
The null row is per design, disregard it. i thought you were firstly referring to a column.
on your where statement <field> is not null
So:
select v.venue_id, v.name
from venues v
where not exists (
select e.vid
from events e
where e.vid = v.venue_id
and name is not null
);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Permission to ask, why is this the error? I try
select user_idea.idea_id,user_idea.title, idea_events.events, idea_categories.category_name AS category
from user_idea
LEFT join idea_events
on user_idea.idea_id=idea_events.idea_id
LEFT JOIN idea_categories
ON user_idea.idea_categories=idea_categories.category_id;
The result is sqliteonline like this:
I want to combine Dewasa and Anak events because of the same idea. For category why is the result NULL for values 1;2 not read in the idea_category table which should be Mudah; Sedang.
For table:
Output expected:
select user_idea.idea_id,user_idea.title,
(SELECT GROUP_CONCAT(events,';') FROM idea_events Where user_idea.idea_id=idea_events.idea_id) as events,
(SELECT GROUP_CONCAT(category_name ,';') FROM idea_categories Where user_idea.idea_categories=idea_categories.category_id) AS category
from user_idea;
1:2 idea_categories in user_idea is not considered as relevant to join so when joined it cannot find the data in other table here so data is null
reminder
before doing join just analyse the data anomalies like not having common data and properly formatted
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a table name customers with two columns, case id and owner. I need to write a query to select random 5 caseids for every name in owner column.please help
For a start, you need something like:
SELECT TOP 5
ID,
[Case ID],
[Owner],
Rnd(-Timer()*[ID]) AS RandomRecord
FROM
[Cases]
ORDER BY
Rnd(-Timer()*[ID]);
to be used as a subquery filtered on OwnerID of your Owners' table.
I once posted an article on this with a lot more details:
Random Rows in Microsoft Access
You can use in:
select t.*
from t
where t.id in (select top 5 id
from t as t2
where t2.name = t.name
order by Rnd(-Timer()*[ID])
);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have to find riskunits.descriptions that are missing on policies.
I can match riskunitid on policyid - but how do I find a policy that does not have a riskunit.description? I am new to SQL
Not knowing your exact table structure, we'll have to make do with some pseudo code:
select *
from policy p
where not exists (
select *
from riskunit r
where r.policyid = p.policyid
)
This will find policies with no riskunit record. If you expect there will always be a riskunit record, but the description may be null or an empty string, go with this instead:
select *
from policy p
join riskunit r
on r.policyid = p.policyid
where (r.description is null or r.description = '')
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Union all function is not working inside the Recursive CTE...?
with CTE_Manager(id,manager,man_id,[Level])
as
(
select id,manager,man_id,1
from manager
where man_id is null
union all
select a.id,a.manager,a.man_id,b.[Level]+1
from manager a
join CTE_Manager b
on b.man_id= a.id
)
select a.manager,ISNULL(a.manager,'SUPER BOSS'),b.Level
from CTE_Manager a
join CTE_Manager b
on a.man_id=b.id
Actually i am getting the output:
I am retrieving the value before the union all function.I have to get all the values from the recursive CTE.
The on clause in your join is the wrong way around. It should be b.id = a.man_id.
What you have done is selected all managers that don't have a manager and then tried to find their manager. When what I suspect you want is all of their subordinates.