Lets say we have the following relationships
friend_from
friend_to
BOB
JOHAN
TRACY
JOHAN
How can i use an hierarchical query to get all rows that are related to 'BOB'. So basically an BI-directional query.
Expected Result:
friend_from
friend_to
BOB
JOHAN
TRACY
JOHAN
My query:
SELECT friend_from, friend_to
FROM friends
start with friend_from='BOB'
CONNECT BY PRIOR friend_from = friend_to;/
This only returns
friend_from
friend_to
BOB
JOHAN
You need a larger sample data set to fully describe the problem but I think, if you want to connect on both sides of the relationship then you can use:
SELECT friend_from,
friend_to
FROM friends
START WITH friend_from = 'BOB'
CONNECT BY NOCYCLE
PRIOR friend_from IN (friend_from, friend_to)
OR PRIOR friend_to IN (friend_from, friend_to);
Which, for the sample data:
CREATE TABLE friends (friend_from, friend_to) AS
SELECT 'BOB', 'JOHAN' FROM DUAL UNION ALL
SELECT 'TRACY', 'JOHAN' FROM DUAL;
Outputs:
FRIEND_FROM
FRIEND_TO
BOB
JOHAN
TRACY
JOHAN
fiddle
Related
I'm trying to query a table. I want the results to include the FROM and TO columns, but then also include rows with these two values reversed. And then I want to eliminate all duplicates. (A duplicate is the same two cities in the same order.)
For example, given this data.
Trips
FROM TO
-------------------- --------------------
West Jordan Taylorsville
Salt Lake City Ogden
West Jordan Taylorsville
Sandy South Jordan
Taylorsville West Jordan
I would want the following results.
West Jordan Taylorsville
Taylorsville West Jordan
Salt Lake City Ogden
Ogden Salt Lake City
Sandy South Jordan
South Jordan Sandy
I want to do this using C# and Entity Framework, but I could use raw SQL if I need to.
Is it possible to do this in a query, or do I need to manually perform some of this logic?
Not sure if I'm following, but doesn't just a simple union work for your sample?
select from, to
from some_table
union
select to, from
from some_table
I do believe the first sub query should handle the first part of your question. the WHERE ID NOT IN will handle the second part of your question.
SELECT *
FROM
(
SELECT *
FROM Trips
WHERE ID IN (
SELECT ID
FROM Trips t1
INNER JOIN Trips AS t2
ON t2.To = t1.From AND t2.From = t1.To
)
)
WHERE ID NOT IN
(
SELECT MIN(ID)
FROM Trips
GROUP BY [From], [To]
)
I am assuming there is more to the table than just those fields. Usually you have a field (primary key) to uniquely identify the row. I am using ID for that field, replace with whatever your table is using.
I am hoping to create a function that can delete all items in a group if any one of them matches a specific condition.
Specifically, I have a dataset 'family' and I want to delete all members of the family if that family contains twins.
A portion of the dataset looks like this:
Subject ID
Mother_ID
Zygosity_SR
1001
2001
MZ
1002
2001
MZ
1003
2001
NotTwin
1004
2002
NotTwin
1005
2002
NotTwin
In this case I want to delete all rows with individuals with the same Mother_ID as the Subjects with Zygosity_SR = MZ.
My resulting table would look like this:
Subject ID
Mother_ID
Zygosity_SR
1004
2002
NotTwin
1005
2002
NotTwin
This is the SQL code I have so far, but I could really use some guidance on where to go from here:
proc sql;
delete from family
where ZygositySR = 'MZ'
group by Mother ID
;
Thank you so much!
Use exists:
delete from family
where exists (select 1
from family f2
where f2.mother_id = family.mother_id and
f2.ZygositySR = 'MZ'
);
If exists is not working how about the alternative in method
delete from family
where mother_Id in (select Mother_Id from family where zygosity_SR='MZ')
I have a table data with have 100.000 rows+ and its content like below;
Service Owner
ABC JOHN
ABC MARK
ABC MARK
ABC STEVE
ABC STEVE
The output what i want is like below. Only getting unique values for services;
Service Owner
ABC JOHN
ABC MARK
ABC STEVE
How can i select the query?
An aggregate query should do the trick:
SELECT MIN(service), owner
FROM mytable
GROUP BY owner
Selecting the distinct service and owner will give you the unique combinations of service and owner.
SELECT DISTINCT service,owner FROM TABLE
Here's a SQLFiddle.
what is the equivalent sql of this table? i want to get only the names of the persons from all_user table who are not in your_friend table
Angelina Jolie
Brad Pitt
Peter Parker
Clark Kent
table name: all_user
(users)
Angelina Jolie
Brad Pitt
Peter Parker
Mary Jane
Clark Kent
Lois Lane
table name: your_friend
(friend)
Lois Lane
Marj Jane
select distinct * from all_user where not in (select * from your_friend where all_user.users = your_friend.friend)
This is my answer and i am getting an error near IN syntax.
You haven't specified a RDBMS. If your engine supports it the EXCEPT operation is what you are looking for. It evaluates the output of two query expressions and returns the difference between the results. The result set contains all rows returned from the first query expression except those rows that are also returned from the second query expression.
SELECT DISTINCT <Columns_To_Be_Included>
FROM all_user
EXCEPT
SELECT DISTINCT <Columns_To_Be_Included>
FROM your_friend
But be careful that this works at the record level. So, you have to only specify the columns that you want to include in the comparison.
Try this
select distinct * from all_users where users not in (select distinct friend from your_friend)
I have 2 separate queries that returns to me the following tables:
===========================
Id f_name l_name
===========================
15 Little Timmy
16 John Doe
17 Baby Jessica
---------------------------
===========================
Id item_name item_price
===========================
15 Camera 100
15 Computer 200
16 Pony 55
---------------------------
In MySQL, how do I combine the 2 into this:
===================================================
Id f_name l_name item_name item_price
===================================================
15 Little Timmy Camera 100
15 Little Timmy Computer 200
16 John Doe Pony 55
17 Baby Jessica
---------------------------------------------------
Any assistance is greatly appreciated. Thanks.
select
name_table.Id,
name_table.f_name,
name_table.l_name,
item_table.item_name,
item_table.item_price
from
name_table
left join item_table
on name_table.Id = item_table.Id
Enjoy!
There are a lot of types of Joins, and it's a very interesting and important topic in database management. SO's own Jeff Atwood has a very good post describing the different types of joins and cases in which they would be used.
You need to use a left outer join:
SELECT names.Id, names.f_name, names.l_name, prices.item_name, prices.item_price
FROM names
LEFT OUTER JOIN prices
ON names.Id = prices.Id
If you want to perform this select statement regularly in your application, you might want to consider creating a view in the database. A view is basically a combination of data in more than one table. A major benefit of using a view here would be a simpler query in your app, so instead of doing the join in the app, you would simply select all the fields from the view.
Your CREATE VIEW statement would look more or less like Doug's answer, whilst your select would then be
select ID, f_name, l_name, item_name, item_price from my_view;
More details on MySQL CREATE VIEW
And if you want to to create a table with that result in database, do any of these selects/joins in a create table command.
CREATE TABLE foobar SELECT ...