I have two Access tables, one regarding persons and the other regarding relatives. The tables relation looks like this.
tbl_Person
PersonID
Name
Age
1001
Abraham
12
1002
Bill
13
1003
Catty
14
1004
Derby
15
tbl_Relative
RelativeID
Name
Age
2001
Zack
42
2002
Yan
43
2003
Xavier
44
tbl_Relation
RelationID
PersonID
RelativeID
Relation
9999
2001
1001
Father
9998
2002
1002
Aunt
9997
2003
1003
Uncle
9996
2001
1004
Father
I would like to select a relative (or person) and have an SQL query find persons (or relatives).
E.g. If I select Yan (2002), the SQL query will discover Bill as the Person related to Yan.
Alternatively if I select Derby (1004), the query will discover Zack as the relative, as well as Abraham, who is also related to Zack.
In essence:
Search : Derby, query finds Zack (father) and Abraham
Search : Yan, query finds only Bill
I can join (union) the two tables to form a combined table, like
Select PersonID, Name, Age, "Person" as category From tbl_Person
UNION
Select RelativeID, Name, Age, "Relative" as category From tbl_Relative
Related
I am using MS SQL Server.
I have an employee table and I need to relocate some of my employees such that they work together. Basically I want to shift my department and it's specialist in 1 place. So if IT Dept has java specialists working from different locations they need to be bought to 1 place
I need to know the list where in 1 department have same specialists based on 2 locations
My employee table has 4 columns
ID
Department
Specialization
Location
Id
Dept
Specialist
Location
100
IT
Java Lang
London
110
IT
Java Lang
London
125
IT
Java Lang
Paris
128
IT
Java Lang
Paris
130
IT
Java Lang
Paris
135
IT
Python
India
140
IT
Python
India
145
Accounts
Audit
London
150
Accounts
Audit
London
150
Accounts
Audit
London
Output
IT
Java
Paris
IT
Java
London
Please help and pardon me the table format was not getting copied
You can use a windowed count for this
SELECT
t.Dept,
t.Specialist,
t.Location
FROM (
SELECT
t.Dept,
t.Specialist,
t.Location,
COUNT(*) OVER (PARTITION BY t.Dept, t.Specialist) count
FROM YourTable t
GROUP BY
t.Dept,
t.Specialist,
t.Location
) t
WHERE t.count > 1;
db<>fiddle
I am trying to query in MS SQL and I can not resolve it. I have a table employees:
Id Name Surname FatherName MotherName WifeName Pincode isChild
-- ------- ------- ---------- ---------- -------- ------- -------
1 John Green James Sue null 101011 1
2 Michael Sloan Barry Lilly null 101011 1
3 Sally Green Andrew Molly Jemi 101011 1
4 Barry Sloan Soul Paul Lilly 101011 0
5 James Green Ned White Sue 101011 0
I want a query that selects rows where the father name and mother name of child matches with name and wife name. For the example table, where I want to return the result of rows where father and mother name matches the name and wife name column. For eg. id=1, where John's father name James and mother name Sue matches with id 5 which returns James as first name and Sue as wife name. So my query should return (this is my expected result)
Id Name Surname FatherName MotherName WifeName Pincode isChild
-- ------- ------- ---------- ---------- -------- ------- -------
5 James Green Ned White Sue 101011 0
4 Barry Sloan Soul Paul Lilly 101011 0
I tried with the below query but it checks for James only. How to change my query so that it checks all the names and returns the expected result.
select * FROM employees
where first_name like '%James%'
and wife_name like '%Sue%'
and pincode=101011;
Any tips on this will be really helpful. I am new to joins, need help on writing self join to get the result.
…
select *
from thetable as p -- the parent/father
where exists -- with one child at least
(
select *
from thetable as c
where c.fathername = p.name
and c.mothername = p.wifename
-- lastname?
)
Too long for a comment, but also not intended as a slam against what you are working with. Please take as constructive criticism.
Aside from VERY POOR DESIGN on the table content, getting that corrected before you get too deep into whatever you are working should be done first. A more typical design might be having a table of people. Now, to get the relationships you could do a couple ways. One is that on each individual person's record, you add 2 additional IDs. FatherID, MotherID. These IDs would join directly back to the child vs hard strings to match against. Take a surname like Smith or Jones. Then, look at the many instances of a "John Smith" may exist, yes a lot, and lower probability of finding a matching wife's name of Sue, Mary or whatever else name. But even that could lead to multiple possibilities. Yes, you are adding a PIN, but even a computer can generate a random pin of 1234.
By having the IDs, there is NO ambiguity of who the relationship is with.
If the data were slightly altered to something like
Id Name Surname FatherID MotherID SpouseID
-- ------- ------- ---------- ---------- --------
1 John Green 5 6 null
2 Michael Sloan 4 3 null
3 Lilly Sloan null null 4
4 Barry Sloan null null 3
5 James Green 9 10 6
6 Sue Green 7 8 5
7 Bill Jones null null 8
8 Martha Jones null null 7
9 Brian Green null null 10
10 Beth Smith-Green null null 9
So, in this modified example, you can see right away that ID#1 John Green has parents of Father (ID#5) is James and Mother (ID#6) is Sue. But even from this, James is a child to Father (ID#9) Brian and Mother (ID#10) Beth. This scenario is showing to a grand-parent level capacity and that each of James and Sue are also children but to their respective parents. Sue's parents of the Jones surname.
For Michael Sloan, parents of #4 Barry, and #3 Lilly.
And I additionally added a spouse ID. This prevents redundancy of people's names copied all over. Then you can query based on the child's parent's respective IDs to find out vs a hopeful name LIKE guess.
So, even though not solving a relatively simple query, fixing the underlying foundation of your database and is relations will, long-term, help ease your querying in the future.
Try this:
SELECT
T2.*
FROM Employee T1
JOIN Employee T2 ON T2.Name = T1.FatherName
AND T2.WifeName = T1.MotherName
I have three tables as below:
First Table Second Table Third Table
Name PIN Id City City_id
David 1948 1 Roma 3
Susan 1245 2 Berlin 2
Jack 1578 3 New York 3
Hans 1247 2
Rose 8745 1
I want to merge first and second table according to third table. Result will be: Person
Name PIN City
David 1948 New York
Susan 1245 Berlin
Jack 1578 New York
Hans 1247 Berlin
Rose 8745 Roma
Firsty I can merge second and third table and then merge the result table with first table but I want to solve this problem without a medium table. How can I handle this? How can I combine first table's rows in sequence with a specified row in second table according to third table?
You would need a fourth table, PersonCity, with PersonID and CityID to link together. Think of relational databases like a grid (spreadsheet, roads). If you're going North and the street you want to get on is parallel (think |^| |^|) you're gonna need to use a different road that links the two. Currently, you have no such path.
The short answer is that your tables are not adequate for the task, what you need is along the lines of:
Table_1 Table_2 Table_3
Id Name PIN Id City Name_id City_id
1 David 1948 1 Roma 1 3
2 Susan 1245 2 Berlin 2 2
3 Jack 1578 3 New York 3 3
4 Hans 1247 4 2
5 Rose 8745 5 1
Then you can do your query as follow:
SELECT T1.Name, T1.PIN, T2.City
FROM Table_1 T1 LEFT JOIN Table_3 T2 ON T1.Id = T3.Name_id
LEFT JOIN Table_2 ON T3.City_id = T2.Id
ORDER BY T1.Name
Or you could ORDER BY City, name
I have good news and bad news.
The good news, Given the tables the way they were originally specified, in Oracle, this will give you something that looks like what you are asking:
---
--- Pay attention, This looks right but it is not!
---
select name,pin,city from
( select name,pin,rownum rn from first ) a,
( select city,id from second) b,
( select id,rownum rn from third ) c
where
a.rn=c.rn AND
b.id=c.id;
NAME PIN CITY
-------------------- ---- --------------------
Rose 8745 Roma
Susan 1245 Berlin
Hans 1247 Berlin
David 1948 New York
Jack 1578 New York
The bad news is this does not really work and is cheating. You will get results but they may not be what you would expect and they won't necessarily be consistent.
The database orders records in its own order. If you don't specify an order by clause, you get what they give you, which may not be what you want. This is cheating because Oracle does not REALLY support using rownum in this way because you can't bet on what you will get. This won't work in most other databases.
The only correct way is what #daShier gave, where you have to add something, say, ID, to allow connecting the rows in the order you want.
Below is my SQL Server 2012 query example. How do I add an extra column “StaffNo” to show an incremental integer?
This int always starts with 1, it doesn’t need to be matching with the staff name. for example “Joe” in this query running shows 1 in staff No, in next query running he could be 2 or 3 or any other number.
Same user always appear same staff No. Different user appear different No.
The number must be sequential and the increment is 1.
Because Staff is more than 100, so don’t write the query like “select case when staff = ‘Joe’ then 1 End”.
my query:
Staff CaseNumber
Joe 5880
Joe 4489
Joe 2235
Emily 7790
Emily 8813
expected result:
Staff CaseNumber StaffNo
Joe 5880 1
Joe 4489 1
Joe 2235 1
Emily 7790 2
Emily 8813 2
Use DENSE_RANK over the entire table, without a partiton, and order by the staff member's name.
SELECT
Staff,
CaseNumber,
DENSE_RANK() OVER (ORDER BY Staff) StaffNo
FROM yourTable;
Demo here:
Rextester
I have a table named PROJECT that looks something like this
SQL> SELECT * FROM PROJECT;
P# PTITLE SPONSOR D# BUDGET
------ ----------------- ----------------------- ---- ----------
1001 Computation Microsoft 1 25000
1002 Study methods Education committee 3 15000
1003 Racing car Cloud Pty Ltd 3 225000
1004 Football Football club 5 35000
1005 Swimming Education committee 5 125000
And from this table, I would like to list the name of sponsors, and the total budget that each sponsor provided.
I am working with Oracle SQL and am having some difficulty with the syntax to select the data from this table.
Thanks in advance!
Simple aggregation should suffice
SELECT SPONSOR,SUM(BUDGET)
FROM PROJECT
GROUP BY SPONSOR