easy family tree query [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 1 year ago.
Improve this question
There is only one table.
Table name Family_tree.
Query all children for Nick. Nick can be also mother!
NAME ID FATHER_ID MOTHER_ID
--------------- ---------- ---------- ----------
Nick 23 25 24
Jane 10 27 26
Perl 15 9 13
Katrin 50 6 12
Sandra 1 3 8
Demi 2 3 8
Deimar 3 7 5
Gandalf 4 6 5
Bill 5 10 23
Kelly 6 22 43
Dolmar 7 11 20

May be like this:
select name, father_id, mother_id from family_tree
where father_id in
(select id from family_tree
where name = 'Nick')
or mother_id in
(select id from family_tree
where name = 'Nick')
or if you need Nick only as a FATHER, then:
select name, father_id, mother_id from family_tree
where father_id in
(select id from family_tree
where name = 'Nick')
Also, if you need only children , you don't need to use connect by...prior for grandchildren, grandgrandchildren, etc.

select t1.name as child, t2.name as father
from table t1
join table t2
on t1.code = t2.father_id
This will give the following o/p
Child Father
______________________________
Nick fill from your tab
Calvin fill from your tab
Sofia Nick

If you're saying that Code = 3 means that Nick is that person's father:
If you only want to return the name:
SELECT Name FROM table WHERE code = 3
IF you want to return everything:
SELECT * FROM table WHERE code = 3
The '3' might have to be in single-quotes as I typed in this sentence, depending on the data type of the Code field + the particular SQL dialect.
EDIT: not quite sure I understand the ask, but Rajee's answer might correct. You can join a table to itself with aliasing i.e. Family_Tree A, Family_Tree B
Another possibility would be a subquery. Because I'm not sure understand the ask, I don't know if this is what you're looking for but consider it an example of what a subquery would look like:
SELECT * FROM Family_Tree A WHERE FatherID = (SELECT ID From Family_Tree B WHERE Other Conditions = Other Conditions)
Something like that, but same thing could probably be accomplished with my first answer or Rajee's answer with some more clarification.

You can try below code -
SELECT
FROM Family_tree T1
JOIN (SELECT ID, FATHER_ID
FROM Family_tree
UNION ALL
SELECT ID, MOTHER_ID
FROM Family_tree) T2 ON T1.ID = T2.FATHER_ID;

Related

Select in a select from another table [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I need to make a query to another table and add the result to the current one as a new record if there is no record with an id in it, but if the current table has a value with such an id then add the value.
table 1:
id
name
date
1
alex
2022-01-01
2
tom
2022-01-01
table 2:
id
name
pass
1
alex
111
3
mitch
222
expected result:
id
name
date
value
1
alex
2022-01-01
111
2
tom
2022-01-01
3
mitch
222
Its a merge results for both the tables so you query will be
select * from table1 full outer join table2 on table1.(some column name) = table2.(some column name)
here some column name represents you id in table1 and table2 respectively.
so your query will be
select * from table1 full outer join table2 on table1.id = table2.id

Selecting Distinct IDs From a Table

I have a table that looks like such:
firstName ID
Mike 1
James 2
Mike 3
Sally 4
Emma 5
Sally 6
and am trying to get an output that returns each person who has more than 1 different ID, and what those IDs are. In my example it would be like such:
firstName ID
Mike 1
Mike 3
Sally 4
Sally 6
I am working on it and have something like what is below but it is erroring. There is something in the logic I am clearly missing but I am struggling to see what it is. Can someone point me in the direction of what is wrong here?
SELECT firstName, ID
FROM table
GROUP BY ID
HAVING COUNT(ID) > 1
You select names with more than one id using exists:
select t.*
from t
where exists (select 1
from t t2
where t2.firstName = t.firstName and t2.id <> t.id
);

Write nested SQL query

I have 2 tables
Table1
ID Name
--------------------
1 John Carter
2 Jack Hammer
3 John Adams
4 John Doe
5 Brian Adams
Table2
ID ID_FromTable1
-----------------------------
1 2
2 3
3 1
4 1
5 1
6 2
7 3
8 1
9 1
10 5
11 4
12 5
13 4
ID in both tables is the primary key
ID_FromTable1 is the foreign key pointing to ID of Table1.
Now I do something like this:
SELECT ID
FROM Table1
WHERE Name like '%John%'
This will give me the IDs 1, 3, 4.
Now using these IDs, I want to write a query on Table2 to delete all entries where ID_FromTable1 are 1, 3, 4.
Please help me write one single SQL query to get all the IDs from Table1 where Name is 'John' and then using those IDs to delete entries from Table2.
I hope I have made the question clear. Do let me know if you need any clarification.
You can use IN with subquery:
DELETE FROM Table2
WHERE ID_FromTable1 IN ( SELECT ID
FROM Table1
WHERE Name LIKE '%John%' )
In MySQL you can do it with this join
delete table2
from table2
join table1 on table2.id_fromtable1 = table1.id
WHERE t1.Name like '%John%'

How to update all records with a NULL value to the next existing value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
This might be SQL 101, but it's stumping me.
I have data like this:
ID ZipCode Value
1 12345 1
2 12346 Null
3 12347 Null
4 12348 2
5 12349 3
6 12350 Null
7 12351 Null
8 12352 4
I need a way to update records that have a null 'value' is updated to the NEXT available value.
ie:
ID ZipCode Value
1 12345 1
2 12346 2
3 12347 2
4 12348 2
5 12349 3
6 12350 4
7 12351 4
8 12352 4
I think this can be done easily enough with a cursor, but there has to be a better way.
There is no need to use cursor.
Updating the table in a single statement can be tricky.
So for safety I would first get the result set with the values to assign for all NULL values:
WITH B AS
(
SELECT ID, (SELECT MIN(Value)
FROM MyTable
WHERE ID > A.ID AND MyTable.Value IS NOT NULL) ValueToAssign
FROM MyTable A
WHERE Value IS NULL
)
UPDATE MyTable
SET Value = B.ValueToAssign
FROM MyTable JOIN B ON MyTable.ID = B.ID
It works if there are gaps between ID.
Here's a Demo on SqlFiddle.
;with cte
as
(
select ID, ZipCode, Value, ROW_NUMBER() OVER(ORDER BY ID) rn
from tb o
)
, ct
as
(
select top 1 *
from cte
order by rn desc
union all
select t.ID, t.ZipCode,
case when t.Value is null then o.Value else t.Value end 'Value', t.rn
from cte t inner join ct o on t.rn = o.rn - 1
)
update tb
set Value = ct.Value
from tb inner join ct on tb.ID = ct.ID
where tb.Value is null

Recursive SQL CTE's and Custom Sort Ordering

Image you are creating a DB schema for a threaded discussion board. Is there an efficient way to select a properly sorted list for a given thread? The code I have written works but does not sort the way I would like it too.
Let's say you have this data:
ID | ParentID
-----------------
1 | null
2 | 1
3 | 2
4 | 1
5 | 3
So the structure is supposed to look like this:
1
|- 2
| |- 3
| | |- 5
|- 4
Ideally, in the code, we want the result set to appear in the following order: 1, 2, 3, 5, 4
PROBLEM: With the CTE I wrote it is actually being returned as: 1, 2, 4, 3, 5
I know this would be easy to group/order by using LINQ but I am reluctant to do this in memory. It seems like the best solution at this point though...
Here is the CTE I am currently using:
with Replies as (
select c.CommentID, c.ParentCommentID 1 as Level
from Comment c
where ParentCommentID is null and CommentID = #ParentCommentID
union all
select c.CommentID, c.ParentCommentID, r.Level + 1 as Level
from Comment c
inner join Replies r on c.ParentCommentID = r.CommentID
)
select * from Replies
Any help would be appreciated; Thanks!
I'm new to SQL and had not heard about hierarchyid datatype before. After reading about it from this comment I decided I may want to incorporate this into my design. I will experiment with this tonight and post more information if I have success.
Update
Returned result from my sample data, using dance2die's suggestion:
ID | ParentID | Level | DenseRank
-------------------------------------
15 NULL 1 1
20 15 2 1
21 20 3 1
17 22 3 1
22 15 2 2
31 15 2 3
32 15 2 4
33 15 2 5
34 15 2 6
35 15 2 7
36 15 2 8
I am sure that you will love this.
I recently find out about Dense_Rank() function, which is for "ranking within the partition of a result set" according to MSDN
Check out the code below and how "CommentID" is sorted.
As far as I understand, you are trying to partition your result set by ParentCommentID.
Pay attention to "denserank" column.
with Replies (CommentID, ParentCommentID, Level) as
(
select c.CommentID, c.ParentCommentID, 1 as Level
from Comment c
where ParentCommentID is null and CommentID = 1
union all
select c.CommentID, c.ParentCommentID, r.Level + 1 as Level
from Comment c
inner join Replies r on c.ParentCommentID = r.CommentID
)
select *,
denserank = dense_rank() over (partition by ParentCommentID order by CommentID)
from Replies
order by denserank
Result below
You have to use hierarchyid (sql2008 only) or a bunch of string (or byte) concatenation.
Hmmmm - I am not sure if your structure is the best suited for this problem. Off the top of my head I cannot think of anyway to sort the data as you want it within the above query.
The best I can think of is if you have a parent table that ties your comments together (eg. a topic table). If you do you should be able to simply join your replies onto that (you will need to include the correct column obviously), and then you can sort by the topicID, Level to get the sort order you are after (or whatever other info on the topic table represents a good value for sorting).
Consider storing the entire hierarchy (with triggers to update it if it changes ) in a field.
This field in your example would have:
1
1.2
1.2.3
1.2.5
1.4
then you just have to sort on that field, try this and see:
create table #temp (test varchar (10))
insert into #temp (test)
select '1'
union select '1.2'
union select '1.2.3'
union select '1.2.5'
union select '1.4'
select * from #temp order by test asc