I've got this problem with this exercise apologize that we insert into the table friends that at start is empty some couple of friends
Table Friends:
+------------+------------+
| Friend1 | Friend2 |
+------------+------------+
| Marc | Luc |
| Luc | Marc |
| Marc | John |
| John | Marc |
+-------------------------+
The table Relationship at the beginning is empty
+--------------------------------+-----------+---------+
| GradeOfRelationShip | Friend1 | Friend2 |
+--------------------------------+-----------+---------+
| | | |
+--------------------------------+-----------+---------+
After the insert of the tuple of friends I'll see something like this
+--------------------------------+-----------+---------+
| GradeOfRelationShip | Friend1 | Friend2 |
+--------------------------------+-----------+---------+
| 1 | Marc | Luc |
| 1 | Luc | Marc |
| 1 | Marc | John |
| 1 | John | Marc |
+--------------------------------+-----------+---------+
I inserted into Relationship only the couple of friends directly (in table Friends I insert only direct friends so with Grade 1)
Then I need to find all possible couple of friends from what I inserted into relationship so the output will be:
+--------------------------------+-----------+---------+
| GradeOfRelationShip | Friend1 | Friend2 |
+--------------------------------+-----------+---------+
| 1 | Marc | Luc |
| 1 | Luc | Marc |
| 1 | Marc | John |
| 1 | John | Marc |
| 2 | Luc | John |
| 2 | John | Luc |
+--------------------------------+-----------+---------+
So what my trigger must do:
After the insert of a couple of friends into table Friends I need to check if this couple already exist into table relationship.
If exists with GradeOfRelationship = 0 I need to set this GradeOfRelationship with the new value = 1 (now I've got a path between this friends)
If not exists i need to insert this tuple into the table Relationship with GradeOfRelationship = 1
I must search all possible path between all friend that I've inserted into Relationship, for do that i think that it's necessary a Join like this
Select
Table1.Friend1, Table1.Friend2, Table1.GradeOfRelationship,
Table2.Friend1, Table2.Friend2, Table2.GradeOfRelationship
From
Relationship Table1
join
Relationship Table2 on Table1.Friend2 = Table2.Friend1
And the output that i think that I'll see:
+-------------------+------+-----------+---------+-------+
| Friend1 | Friend2| Lv | Friend1 | Friend2 | Lv |
+-------------------+------+-----------+---------+-------+
| Marc | Luc | 1 | Luc | Marc | 1 |
| Marc | John | 1 | John | Marc | 1 |
| John | Marc | 1 | Marc | Luc | 1 |
| Luc | Marc | 1 | Marc | John | 1 |
+--------------------------------+-----------+-----------+
So I'll be interested only with the tuple in which
Table1.Friend1 < > Table2.Friend2
and Table1.Friend1 < > Table2.Friend2 not exist in RelationShip
Insert into Table Relationship Values (Table1.Friend1,
Table2.Friend2,
Table1.GradeOfRelationship + Table2.GradeOfRelationship)
So now there is the main problem, the recursive function that after every insert on table Friends will do this.
And the other problem is that i need to insert into GradeOfRelationship the minimum path between friends.
If there is not any relationship between two people i must insert into table Relationship the couple of friends with grade 0
I trying with SQL Server but at the moment i don't have any idea to how do this statement, i think that i must use some recursive function but i don't know how....i tried to write CTE but it don't work like i want
With Table1 as (Select Friend1,Friend2, 0 as Level
from Friend
Union All
Select F1.Friend1, Table1.Friend2, Level+1 as FriendshipLevel
From Friend F1 join table1 on F1.Friend2=table1.Friend1
where F1.Friend1 < > Table1.Friend2
)
Select *
From Table1
This SQL query didn't work well because it start to all possible computation with friends so 0 to infinite so it go on no termination.
Thanks a lot for your cooperation and looking forward into your answer
This is the SQL Schema of both table (Table Relationship is empty because it ll populate after the enter in action of the trigger)
CREATE TABLE Friends
(`Friend1` varchar(50), `Friend2` varchar(50))
;
INSERT INTO Friends
(`Friend1`, `Friend2`)
VALUES
('Luc', 'Marc'),
('Marc', 'Luc'),
('John', 'Marc'),
('Marc', 'John'),
('Alex', 'Marc'),
('Marc', 'Alex')
;
CREATE TABLE Relationship
(`Friend1` varchar(50), `Friend2` varchar(50),`GradeOfRelationShip`
int )
;
Best Regards
Related
TABLE 1: Data sent to vendor
| MemberID | FirstName | LastName | Etc |
| :------: | :-------: | :------: | :-: |
| 1 | John | Smith | Etc |
| 2 | Jane | Doe | Etc |
| 3 | Dan | Laren | Etc |
TABLE 2: Data returned from vendor
| MemberID | FirstName | LastName | Etc |
| :------: | :-------: | :------: | :-: |
| 1 | John | Smith | Etc |
| 2 | Jane | Doe | Etc |
| 3 | Dan | Laren | Etc |
We send data to a vendor which is used for their matching algorithm and they return the data with new information. The members are matched with a MemberID data element. How would I write a query which shows me which MemberIDs we sent to the vendor but the vendor didn't return?
NOT EXITS would be my first choice here.
Example
SELECT *
FROM Table1 A
WHERE NOT EXISTS (SELECT 1
FROM Table2 B
WHERE A.MemberID = B.MemberID )
SELECT MemberID
FROM Table1
WHERE MemberID NOT IN (SELECT MemberID FROM Table2)
Using EXCEPT is one option.
SELECT sent.[MemberID] FROM Tbl1_SentToVendor sent
EXCEPT
SELECT recv.[MemberID] FROM Tbl2_ReturnedFromVendor recv
This is just on MemberID, but the "EXCEPT" syntax can also support additional columns (e.g., in case you want to filter out data that may be the same as what you already have.)
I have 2 different tables. I need to get a name from the TMK table in table 1 as below, and I need to bring the total number from my 2nd table. I can't write join. can u help me
TMK Table;
| tmkName |
| George |
| Jacob |
flowNewStatus Table;
|statusId|
| 1 |
| 2 |
if george has number 1 status i want this join
| tmkName | |statusId|
| George | | 1 |
Before getting to possible SQL queries... from the tables you show you'd need an additional table that associates the person to status, a join table. Essentially a TMK_status table:
TMK_status table
| personID | statusID |
|----------|----------|
| 1 | 1 |
| 2 | 3 |
| 3 | 1 |
Alternatively, the statusID could be stored as a column of TMK thus,
TMK table
| personID | tmkName | statusID |
|----------|----------|----------|
| 1 | George | 1 |
| 2 | Jacob | 3 |
If by "I can't write join", you mean you don't know how, check this answer: What is the difference between "INNER JOIN" and "OUTER JOIN"? - you will need an inner join.
If, on on the other hand, you mean you can't use join statements, then you could write a subselect statement. There could be other solutions but they depend on how you decide to join/relate the 2 tables.
Having exhausted my limited knowledge of tsql I hope someone may be able to help?
I have a table which holds relationship data such as ID, name, relationship and reciprocal relationship type, start and finish date etc.
Each row contains the Reciprocal ID for the other side of the relationship. See below.
From that I would like to present rows where the ID is equal to 1234 and 1236.
Thank you in advance for help with this.
Paul
+------+-------+------------+------------+----------+------------+---------+
| ID | Name | Start | Finish | Type | Recip Type | RecipID |
+------+-------+------------+------------+----------+------------+---------+
| 1234 | Joe | 01/05/2018 | | Father | Daughter | 1235 |
+------+-------+------------+------------+----------+------------+---------+
| 1235 | Emily | 01/05/2018 | | Daughter | Father | 1234 |
+------+-------+------------+------------+----------+------------+---------+
| 1236 | Susan | 01/09/2017 | 01/05/2018 | Visitor | Patient | 1237 |
+------+-------+------------+------------+----------+------------+---------+
| 1237 | Harry | 01/09/2017 | 01/05/2018 | Patient | Visitor | 1236 |
+------+-------+------------+------------+----------+------------+---------+
Are you looking for or:
select t.*
from t
where id in (1234, 1236) or recipid in (1234, 1236);
Perhaps this would do what you intend:
select t1.*
from table t1
where exists (select 1 from table t2 where t1.id = t2.recipid);
However, same could be also achieve via self join
I don't know much Microsoft Access, but I need it to solve this issue:
Let's suppose that I have two tables:
Table A:
Zipcode Start | ZipCode End | Etc1 | Etc2
==============================================================
20000-000 | 29999-999 | Sample data 1 | Another Sample 1
30000-000 | 39999-999 | Sample data 2 | Another Sample 2
40000-000 | 49999-999 | Sample data 3 | Another Sample 3
Table B:
NAME | ZipCode | Etc1 | Etc2
=============================================
John Doe | 31564-888 | |
Johnny | 22559-010 | |
James | 44411-000 | |
How can I compare the Zipcodes on table B with the specified ranges on table A? And return the "Etc1" and "Etc2" that matches it?
Thank you ALL!!
You can do like this:
Select
TableB.Name,
TableB.ZipCode,
TableA.Etc1,
TableA.Etc2
From
TableA,
TableB
Where
TableB.ZipCode Between TableA.[ZipCode Start] And TableA.[ZipCode End]
Each user/person could know one or more languages.
All I can think is a table like
+----------+------+-----+------------+-----+-----+-----+-------+
| PersonID | Java | PHP | Javascript | C++ | C | CSS | HTML |
+----------+------+-----+------------+-----+-----+-----+-------+
| 1 | Yes | Yes | No | Yes | No | Yes | No |
| 2 | No | Yes | Yes | No | Yes | No | No |
| 3 | Yes | No | Yes | Yes | Yes | Yes | No |
+----------+------+-----+------------+-----+-----+-----+-------+
Considering I'm going to need at least 100 columns for all the languages, is it normal to have that many columns? Something tells me this is the wrong approach.
Thank you very much and sorry about my english!
I would suggest you to create three tables.
One table contains the information of the Person like his Name etc.
Second table contains two columns LanguageId and Language name.
+------------+-----------+
| LanguageID | Name |
+------------+-----------+
| 1 | Javascript|
| 2 | C |
| 3 | C++ |
+------------+-----------+
Third table contains the Id, PersonId, LanguageID. In this table you can join the above two tables record.
+---+----------+------------+
|ID | PersonID | LanguageID |
+---+----------+------------+
|1 | 1 | 1 |
|2 | 2 | 2 |
|3 | 3 | 3 |
+---+----------+------------+
Reasons to support my answer:
In future if you want to add any new language in your table then it
would be easier to add that in the main table.
You can join the two tables easily and get the result
A little improvement we can do over Rahul Tripathi response is to remove the "Known" column. You need only two tables for this case. One containing PersonId and LanguageId the person knows. The second table is for the languages only.
You know what languages one person knows by joining both tables. For example if you need to know the list of known languages you can do:
SELECT p.PersonId, l.Name
FROM Person p INNER JOIN Language l ON (p.LanguageId = l.LanguageId)
WHERE (p.PersonId = theIdYouNeedToKnow)