Recursive CTE in SQL [closed] - sql

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.

Related

Merge row values from the same value key in SQL [closed]

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

SQL returns null data [closed]

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 2 years ago.
Improve this question
I need to remove values ​​that has null result (second row)
Your query that you posted uses correlated sub queries to get the results that could easily achieved with a left join. Changing that left join into an inner join will get rid of the nulls as long as you do not have nulls in the name column of the emp table.
select e.empno,e.ename,mngr.name
from emp e
inner join emp mngr on e.mgr=mngr.empno

sql in ms-access [closed]

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])
);

SQL Quiz SELECT / subquery [closed]

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
Refer to the given SQL statement.
1. Select SPNAME
2. FROM SALESPERSON
3. Where SPNUM=
4. FROM CUSTOMER
5. WHERE CUSTNUM=20900);
What is missing from the subquery at the beginning of line 4?
(SELECT SPNUM
(SPNUM
(Subquery SPNUM
(FIND SPNUM
I think the query should be WHERE SPNUM = ... or WHERE SPNUM IN ... followed by a subquery. To be safe, we can use WHERE SPNUM IN ... in the event that the subquery return more than one record.
SELECT SPNAME
FROM SALESPERSON
WHERE SPNUM IN (SELECT SPNUM FROM CUSTOMER WHERE CUSTNUM = 20900)

How can I get the Unit Number to be displayed in place of the "ID" [closed]

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 7 years ago.
Improve this question
How can I write a SQL query that will replace the "ComputerID" and "PartID" with the "UnitNumber". The ComputerID's and PartID's are in the same column under InventoryID. The InventoryID's are unique but the UnitNumber (sticker label/name) doesn't have this requirement.
Here are the tables and how they relate.
Let's call your tables Object and Inventory
If you use a couple of joins then this should work:
select o.ObjectID as ObjectID, a.UnitNumber as Computer, b.UnitNumber as Part,
o.InputID from Object o
join Inventory a on a.InventoryID=o.CombputerID
join Inventory b on b.InventoryID=o.PartID