How to retrieve data solely by foreign key in MySQL? - mysql

The mission: Visualize the patients that were treated by surgeons.
Table: patients
id_patient
name
last_name
Table: doctors
id_doctor
name
last_name
speciality ("surgeon" is one of the possibilities)
Table: visits
id_visit
id_patient_fk (foreign key)
id_doctor_fk (foreign key)
With the id_doctor_fk pointing to id_doctor, I have to see if that doctor is a surgeon, right? Is this possible? I gave up, so I ask.

If you want patients who have seen a surgeon, you can use exists:
select p.*
from patients p
where exists (select 1
from visits v join
doctors d
on d.id_doctor = v.id_doctor_pk
where v.id_patient_fk = p.id_patient and
d.specialty = 'surgeon'
);

i created an example for your question . i hope this is help you:
CREATE TABLE #patients2 (
id_patient int,
[name] VARCHAR(100),
last_name VARCHAR(100)
);
CREATE TABLE #doctors2 (
id_doctor int,
[name] VARCHAR(100),
last_name VARCHAR(100),
speciality VARCHAR(100),
);
CREATE TABLE #dvisits2 (
id_visit int,
id_patient_fk int,
id_doctor_fk int
);
--drop table #table1
INSERT INTO #patients2
(id_patient, [name], last_name)
VALUES
(1,'patient1','last_name1'),
(2,'patient2','last_name2'),
(3,'patient3','last_name3'),
(4,'patient4','last_name4'),
(5,'patient5','last_name5'),
(6,'patient6','last_name6'),
(7,'patient7','last_name7'),
(8,'patient8','last_name8');
INSERT INTO #doctors2
(id_doctor, [name], last_name, speciality )
VALUES
(1,'doctor1','last_name1','surgeon'),
(2,'doctor2','last_name2','not surgeon'),
(3,'doctor3','last_name3','surgeon'),
(4,'doctor4','last_name4','not surgeon'),
(5,'doctor5','last_name5','surgeon'),
(6,'doctor6','last_name6','surgeon'),
(7,'doctor7','last_name7','not surgeon'),
(8,'doctor8','last_name8','surgeon');
INSERT INTO #dvisits2
(id_visit, id_doctor_fk,id_patient_fk)
VALUES
(1,1,1),
(2,2,2),
(3,3,3),
(4,4,4),
(5,5,5),
(6,6,6),
(7,7,7),
(8,8,8);
select * from #patients2 p
join #dvisits2 dv on p.id_patient = dv.id_patient_fk
join #doctors2 doc on dv.id_doctor_fk = doc.id_doctor
where doc.speciality = 'surgeon'
result =
id_patient [name] last_name id_visit id_patient_fk id_doctor_fk speciality id_doctor [name] last_name
1 patient1 last_name1 1 1 1 surgeon 1 doctor1 last_name1
1 patient1 last_name1 1 1 1 surgeon 1 doctor1 last_name1
3 patient3 last_name3 3 3 3 surgeon 3 doctor3 last_name3
3 patient3 last_name3 3 3 3 surgeon 3 doctor3 last_name3
5 patient5 last_name5 5 5 5 surgeon 5 doctor5 last_name5
6 patient6 last_name6 6 6 6 surgeon 6 doctor6 last_name6
8 patient8 last_name8 8 8 8 surgeon 8 doctor8 last_name8

Related

How to select differences using three tables?

I need to run a script in order to fix some rows from my table company_menu.
However, I can't build this query to get these registers.
I build the schema in this link: http://sqlfiddle.com/#!9/5ab86b
Below I show the expected result.
companies
id
name
1
company 1
2
company 2
3
company 3
menu_items
id
name
1
home
2
charts
3
users
4
projects
company_menu
id
company_id
menu_item_id
1
1
1
2
1
2
3
1
3
4
1
4
5
2
1
6
2
3
This is a result that I expected:
id
company_id
menu_item_id
1
2
2
2
2
4
3
3
1
4
3
2
5
3
3
6
3
4
CREATE TABLE companies(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50)
);
CREATE TABLE menu_items(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50)
);
CREATE TABLE company_menu(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
company_id INT,
menu_item_id INT,
FOREIGN KEY(company_id) REFERENCES companies(id),
FOREIGN KEY(menu_item_id) REFERENCES menu_items(id)
);
INSERT INTO companies (name) VALUES ("Company 1"),("Company 2"),("Company 3");
INSERT INTO menu_items (name) VALUES ("home"),("charts"),("users"),("projects");
INSERT INTO company_menu (company_id, menu_item_id) VALUES (1, 1),(1, 2),(1,3),(1,4);
INSERT INTO company_menu (company_id, menu_item_id) VALUES (2, 1),(2,3);
Two ways I can think of. Don't know which is more efficient. Both start with a full compannies-menu_items join to get all possible combinations, and then cut out the existing:
WHERE NOT EXISTS
select c.id company_id, m.id menu_item_id
from companies c
join menu_items m
where not exists (
select * from company_menu where company_id = c.id and menu_item_id = m.id
);
LEFT JOIN + IS NULL:
select c.id company_id, m.id menu_item_id
from companies c
join menu_items m
left join company_menu cm on cm.company_id = c.id and cm.menu_item_id = m.id
where cm.id is null;
Both are sortable on any company or menu_item column.
http://sqlfiddle.com/#!9/5ab86b/11

MySQL 5.0 hierarchical recursive search

I have a table like this
CREATE TABLE IF NOT EXISTS `categories` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(150) NOT NULL,
`parent_id` INT(11) NULL,
`slug` VARCHAR(150) NOT NULL,
PRIMARY KEY (`id`),
)
This is an example of the table as how it is currently:
id name parent_id slug
----------------------------------------------------
1 Books NULL books
2 Anthology 1 anthology
3 Classic 1 classic
4 Drama 1 drama
5 Fable 1 fable
6 Aesop 5 aesop
7 Bidpai 5 bidpai
8 Stephen King 2 stephen-king
9 Magazines NULL magazines
10 Lifestyle 9 lifestyle
11 Wellness 9 wellness
12 Spa 11 spa
Note: i am on an old version of MySQL (5.0) so i cannot use recursive functions unfortunately
I want to select all parents and include them into the search result as well to get a nice overview of all the categories with the correct slugs in my application like this:
id name id_route slug
----------------------------------------------------
1 Books 1 books
2 Anthology 1,2 books/anthology
3 Classic 1,3 books/classic
4 Drama 1,4 books/drama
5 Fable 1,5 books/fable
6 Aesop 1,5,6 books/fable/aesop
7 Bidpai 1,5,7 books/fable/bidpai
8 Stephen King 1,2,8 books/anthology/stephen-king
9 Magazines 9 magazines
10 Lifestyle 9,10 magazines/lifestyle
11 Wellness 9,11 magazines/wellness
12 Spa 9,11,2 magazines/wellness/spa
Is it possible to retrieve a result like this in the first place? Because i am breaking my head around this at the moment. Any help or a push in the right direction would be amazing!
CREATE PROCEDURE GetTree()
BEGIN
CREATE TABLE tmp_cat LIKE categories;
ALTER TABLE tmp_cat ADD COLUMN path TEXT;
INSERT INTO tmp_cat (id, name, parent_id, slug, path)
SELECT id, name, parent_id, slug, name
FROM categories
WHERE parent_id IS NULL;
REPEAT
INSERT IGNORE INTO tmp_cat (id, name, parent_id, slug, path)
SELECT categories.id, categories.name, categories.parent_id, categories.slug, CONCAT(tmp_cat.path, ',', categories.name)
FROM categories
JOIN tmp_cat ON tmp_cat.id = categories.parent_id;
UNTIL ROW_COUNT() = 0
END REPEAT;
SELECT * FROM tmp_cat;
DROP TABLE tmp_cat;
END
fiddle
The following solution will work as far as you do NOT have more than three levels of hierarchy:
SELECT IF(level3_id IS NOT NULL, level3_id, IF(level2_id IS NOT NULL, level2_id, level1_id)) AS id,
IF(level3_name IS NOT NULL, level3_name, IF(level2_name IS NOT NULL, level2_name, level1_name)) AS name,
CONCAT_WS(",", level1_id, level2_id,level3_id) AS id_route,
CONCAT_WS("/", level1_slug, level2_slug,level3_slug) AS slug
FROM (
SELECT level1.id AS level1_id, level1.name AS level1_name, level1.slug AS level1_slug,
level2.id AS level2_id, level2.name AS level2_name, level2.slug AS level2_slug,
level3.id AS level3_id, level3.name AS level3_name, level3.slug AS level3_slug
FROM categories AS level1
RIGHT JOIN categories AS level2 ON level1.id = level2.parent_id
RIGHT JOIN categories AS level3 ON level2.id = level3.parent_id
) AS levels
ORDER BY id;
Sadly such solution will not scale up well. Recursive CTE is the better solution so you should consider upgrading.

How can I filter the information excluding some of the values in SQL?

I have the following tables:
create table Person (
id_person int,
name varchar(255),
primary key (id_person)
);
create table Journey (
id_journey int,
year int,
country varchar(255),
primary key (id_journey),
);
create table Person_Journey (
id_journey int references Journey (id_journey),
id_person int references Person (id_person),
primary key (id_journey, id_travel)
);
Imagine the data inserted into this tables are:
Table Person
id_person name
-----------------------------------------------
1 Jake
2 Kevin
3 Denis
Table Journey (and table Person_Journey):
id_person id_travel country
---------------------------------------------------
1 1 France
1 2 Germany
1 3 Portugal
2 4 UK
2 5 Germany
2 6 UK
3 7 UK
3 8 Portugal
3 9 Germany
As the previous example shows, person with id_person = 1 has travelled to Portugal, Germany and France. Person with id_person = 2 has travelled to the UK two times and to Germany. Finally, person with id_person = 3 has travelled to UK, Germany and Portugal.
I have to execute a SELECT on where I want to show the id_person and it's respective name from ONLY the persons that has travelled to the UK AND Germany.
So the output I want to get is:
id_person name
------------------------------
2 Kevin
I have tried executing this with no success:
select P.* from Person as P, Journey as J, Person_Journey as PJ
where P.id_person = PJ.id_person and J.id_journey = PJ.id_journey
and J.country in ("UK", "Germany")
group by (P.id_person)
having count(J.country) = 2;
How can I do this? I'm using MySQL
Try this:
SELECT Person.* FROM Person_Journey
INNER JOIN (
SELECT id_journey FROM Journey
WHERE
country in('Germany', 'UK')
GROUP BY id_journey
HAVING COUNT(DISTINCT country) = 2
) t
ON Person_Journey.id_journey = t.id_journey
INNER JOIN Person ON Person.id_person = Person_Journey.id_person
Well, one solution without joins could be with IN's:
SELECT * FROM Person
WHERE id_person IN (
SELECT id_person FROM Person_Journey
WHERE id_journey IN(
SELECT id_journey FROM Journey
WHERE
country in('Germany', 'UK')
GROUP BY id_journey
HAVING COUNT(DISTINCT country) = 2
)
)
Though, say in university, that joins are still better, than this ))
I would use EXISTS:
Pseudo code:
Select person names where their ID appear in a row "Germany" and in a row "UK" of the Journey table.
Which would translate to:
SELECT p.name FROM Person p
WHERE EXISTS (
SELECT 1 FROM Journey j
WHERE country='UK'
AND p.id_person = j.id_person
AND EXISTS (
SELECT 1 FROM Journey j
WHERE country='Germany'
AND p.id_person = j.id_person
)

select employee with their manager in 2 table

I have 2 tables in my database, one with employees and the second with managers. I want to select all employees under a specified manager.
Employee table
id name
1 shivam
2 ravi
Manager table
id empId managerId
1 1 2
2 2 1
And I'd like this type of result
id name managername
1 shivam ravi
2 ravi shivam
Here is a link where I ran the code https://sqltest.net/#686252
If a table was created like this:
create table employee(
id int auto_increment,
name varchar(20),
primary key (id)
);
create table manager(
id int auto_increment,
empId int,
managerId int,
primary key (id)
);
insert into employee (name) values ('shivam'), ('ravi');
insert into manager (empId,managerId) values (1,2),(2,1);
then you could get the result you want with this statement:
SELECT e.id, e.name, e2.name managername from
employee as e
join manager as m on m.empId = e.id
join employee as e2 on e2.id = m.managerId;
Here are my results:
id name managername
1 shivam ravi
2 ravi shivam

SQL Server Query or Tool to show Hierarchical Data

We have a general organizational table structure, think of it s a Tree or Pyramid Hierarchy. We basically have multiple "trees" we want to show. On for one company, one for another ETC.
Does anyone know of a good way to display this data? SQL Query would be nice, doubt it would be possible, but I wouldn't be against using some OTS tool (preferably free). I would also like to avoid some type of report. I don't need an actual solution just need to know if its possible. So if you say SQL if you can give me a 2 table example of showing a root a leave I would be happy.
Structure is pretty general
And each table is linked through a surrogate key CompanyID, CompanyGroupID, etc.
Any suggestions on ways we could display/query for this data? Last resort is to write a quick C# Windows Application...
We would like to see it in tree form:
-- 1-Company
-- / \
-- CompanyGroupA CompanyGroupB
-- / \ \
-- CompanyStoreA1 CompanyStoreA1 CompanyStoreB
-- / \ / \
--Employee A B C
In attempt to please the masses here is an example test script to populate the query.
DECLARE #Company table (id int, name varchar(40) )
INSERT #Company VALUES (1,'Living Things' )
INSERT #Company VALUES (2,'Boring Company' )
DECLARE #CompanyGroup table (id int, name varchar(40), CompanyID int)
INSERT #CompanyGroup VALUES (1,'Pets',1 )
INSERT #CompanyGroup VALUES (2,'Humans',1 )
INSERT #CompanyGroup VALUES (3,'Electronics',2 )
INSERT #CompanyGroup VALUES (4,'Food',2 )
DECLARE #CompanyStore table (id int, name varchar(40), CompanyGroupID int)
INSERT #CompanyStore VALUES (1,'PetsStoreA',1 )
INSERT #CompanyStore VALUES (2,'PetsStoreB',1 )
INSERT #CompanyStore VALUES (3,'PetsStoreC',1 )
INSERT #CompanyStore VALUES (4,'PetsStoreD', 1)
INSERT #CompanyStore VALUES (5,'HumansStore',2 )
INSERT #CompanyStore VALUES (6,'FoodStore',3 )
The final solution was pretty awesome I modified the usp_DrawTree to accept varchar vs ints because i had to make my query ids unique. I then just did a select/union all and built the parent child relationship.
select * into #TreeData from (
select ID='C' + cast(id as varchar(10)),
ParentID=null,
DataForBox=name + '(' + cast(id as varchar(10)) + ')',
ExtraInfo='',
SortColumn=name
from Company c
)
union all (
select ID='CG' + cast(id as varchar(10)),
ParentID=cg.CompanyID ,
DataForBox=name + '(' + cast(id as varchar(10)) + ')',
ExtraInfo='',
SortColumn=name
from CompanyGroup cg join Company c on c.ID=cg.CompanyID
)
//union all rest of hierarchy
)
Brad Schulz to the rescue in the form of usp_DrawTree.
you don't provide any table structure, so here is a sample of a recursive CTE processing a tree structure:
--go through a nested table supervisor - user table and display the chain
DECLARE #Contacts table (id varchar(6), first_name varchar(10), reports_to_id varchar(6))
INSERT #Contacts VALUES ('1','Jerome', NULL ) -- tree is as follows:
INSERT #Contacts VALUES ('2','Joe' ,'1') -- 1-Jerome
INSERT #Contacts VALUES ('3','Paul' ,'2') -- / \
INSERT #Contacts VALUES ('4','Jack' ,'3') -- 2-Joe 9-Bill
INSERT #Contacts VALUES ('5','Daniel','3') -- / \ \
INSERT #Contacts VALUES ('6','David' ,'2') -- 3-Paul 6-David 10-Sam
INSERT #Contacts VALUES ('7','Ian' ,'6') -- / \ / \
INSERT #Contacts VALUES ('8','Helen' ,'6') -- 4-Jack 5-Daniel 7-Ian 8-Helen
INSERT #Contacts VALUES ('9','Bill ' ,'1') --
INSERT #Contacts VALUES ('10','Sam' ,'9') --
DECLARE #Root_id char(4)
--get complete tree---------------------------------------------------
SET #Root_id=null
PRINT '#Root_id='+COALESCE(''''+#Root_id+'''','null')
;WITH StaffTree AS
(
SELECT
c.id, c.first_name, c.reports_to_id, c.reports_to_id as Manager_id, cc.first_name AS Manager_first_name, 1 AS LevelOf
FROM #Contacts c
LEFT OUTER JOIN #Contacts cc ON c.reports_to_id=cc.id
WHERE c.id=#Root_id OR (#Root_id IS NULL AND c.reports_to_id IS NULL)
UNION ALL
SELECT
s.id, s.first_name, s.reports_to_id, t.id, t.first_name, t.LevelOf+1
FROM StaffTree t
INNER JOIN #Contacts s ON t.id=s.reports_to_id
WHERE s.reports_to_id=#Root_id OR #Root_id IS NULL OR t.LevelOf>1
)
SELECT * FROM StaffTree
--get all below 2---------------------------------------------------
SET #Root_id=2
PRINT '#Root_id='+COALESCE(''''+#Root_id+'''','null')
;WITH StaffTree AS
(
SELECT
c.id, c.first_name, c.reports_to_id, c.reports_to_id as Manager_id, cc.first_name AS Manager_first_name, 1 AS LevelOf
FROM #Contacts c
LEFT OUTER JOIN #Contacts cc ON c.reports_to_id=cc.id
WHERE c.id=#Root_id OR (#Root_id IS NULL AND c.reports_to_id IS NULL)
UNION ALL
SELECT
s.id, s.first_name, s.reports_to_id, t.id, t.first_name, t.LevelOf+1
FROM StaffTree t
INNER JOIN #Contacts s ON t.id=s.reports_to_id
WHERE s.reports_to_id=#Root_id OR #Root_id IS NULL OR t.LevelOf>1
)
SELECT * FROM StaffTree
--get all below 6---------------------------------------------------
SET #Root_id=6
PRINT '#Root_id='+COALESCE(''''+#Root_id+'''','null')
;WITH StaffTree AS
(
SELECT
c.id, c.first_name, c.reports_to_id, c.reports_to_id as Manager_id, cc.first_name AS Manager_first_name, 1 AS LevelOf
FROM #Contacts c
LEFT OUTER JOIN #Contacts cc ON c.reports_to_id=cc.id
WHERE c.id=#Root_id OR (#Root_id IS NULL AND c.reports_to_id IS NULL)
UNION ALL
SELECT
s.id, s.first_name, s.reports_to_id, t.id, t.first_name, t.LevelOf+1
FROM StaffTree t
INNER JOIN #Contacts s ON t.id=s.reports_to_id
WHERE s.reports_to_id=#Root_id OR #Root_id IS NULL OR t.LevelOf>1
)
SELECT * FROM StaffTree
OUTPUT:
#Root_id=null
id first_name reports_to_id Manager_id Manager_first_name LevelOf
------ ---------- ------------- ---------- ------------------ -----------
1 Jerome NULL NULL NULL 1
2 Joe 1 1 Jerome 2
9 Bill 1 1 Jerome 2
10 Sam 9 9 Bill 3
3 Paul 2 2 Joe 3
6 David 2 2 Joe 3
7 Ian 6 6 David 4
8 Helen 6 6 David 4
4 Jack 3 3 Paul 4
5 Daniel 3 3 Paul 4
(10 row(s) affected)
#Root_id='2 '
id first_name reports_to_id Manager_id Manager_first_name LevelOf
------ ---------- ------------- ---------- ------------------ -----------
2 Joe 1 1 Jerome 1
3 Paul 2 2 Joe 2
6 David 2 2 Joe 2
7 Ian 6 6 David 3
8 Helen 6 6 David 3
4 Jack 3 3 Paul 3
5 Daniel 3 3 Paul 3
(7 row(s) affected)
#Root_id='6 '
id first_name reports_to_id Manager_id Manager_first_name LevelOf
------ ---------- ------------- ---------- ------------------ -----------
6 David 2 2 Joe 1
7 Ian 6 6 David 2
8 Helen 6 6 David 2
(3 row(s) affected)
EDIT based on OP's given tables and data:
try something like this:
SET NOCOUNT ON
DECLARE #Company table (id int, name varchar(40) )
INSERT #Company VALUES (1,'Living Things' )
INSERT #Company VALUES (2,'Boring Company' )
DECLARE #CompanyGroup table (id int, name varchar(40), CompanyID int)
INSERT #CompanyGroup VALUES (1,'Pets' ,1 )
INSERT #CompanyGroup VALUES (2,'Humans' ,1 )
INSERT #CompanyGroup VALUES (3,'Electronics' ,2 )
INSERT #CompanyGroup VALUES (4,'Food' ,2 )
DECLARE #CompanyStore table (id int, name varchar(40), CompanyGroupID int)
INSERT #CompanyStore VALUES (1,'PetsStoreA' ,1 )
INSERT #CompanyStore VALUES (2,'PetsStoreB' ,1 )
INSERT #CompanyStore VALUES (3,'PetsStoreC' ,1 )
INSERT #CompanyStore VALUES (4,'PetsStoreD' ,1)
INSERT #CompanyStore VALUES (5,'HumansStore' ,2 )
INSERT #CompanyStore VALUES (6,'FoodStore' ,3 )
--not provided by the OP, so I made it up
DECLARE #CompanyEmployees table (id int, name varchar(10), reports_to_id int, CompanyStoreID int)
INSERT #CompanyEmployees VALUES (1,'Jerome', NULL ,1) -- tree is as follows:
INSERT #CompanyEmployees VALUES (2,'Joe' ,1 ,1) -- PetsStoreA PetsStoreB PetStoreC FoodStore
INSERT #CompanyEmployees VALUES (3,'Paul' ,2 ,1) -- 1-Jerome 11-Alan 14-Ben 18-apple
INSERT #CompanyEmployees VALUES (4,'Jack' ,3 ,1) -- / \ / \ / / \
INSERT #CompanyEmployees VALUES (5,'Daniel',3 ,1) -- 2-Joe 9-Bill 12-Ally 13-Abby 15-Bill 19-pear 20-grape
INSERT #CompanyEmployees VALUES (6,'David' ,2 ,1) -- / \ \ / \ /
INSERT #CompanyEmployees VALUES (7,'Ian' ,6 ,1) -- 3-Paul 6-David 10-Sam 16-Bjorn 17-Benny 21-rasin
INSERT #CompanyEmployees VALUES (8,'Helen' ,6 ,1) -- / \ / \
INSERT #CompanyEmployees VALUES (9,'Bill ' ,1 ,1) -- 4-Jack 5-Daniel 7-Ian 8-Helen
INSERT #CompanyEmployees VALUES (10,'Sam' ,9 ,1) --
INSERT #CompanyEmployees VALUES (11,'Alan' ,NULL ,2) --to see all trees, scroll--->>
INSERT #CompanyEmployees VALUES (12,'Ally' ,11 ,2) --
INSERT #CompanyEmployees VALUES (13,'Abby' ,11 ,2) --
INSERT #CompanyEmployees VALUES (14,'Ben' ,NULL ,3) --
INSERT #CompanyEmployees VALUES (15,'Bill' ,14 ,3) --
INSERT #CompanyEmployees VALUES (16,'Bjorn',15 ,3) --
INSERT #CompanyEmployees VALUES (17,'Benny',15 ,3) --
INSERT #CompanyEmployees VALUES (18,'apple',NULL ,6) --
INSERT #CompanyEmployees VALUES (19,'pear' ,18 ,6) --
INSERT #CompanyEmployees VALUES (20,'grape',18 ,6) --
INSERT #CompanyEmployees VALUES (21,'rasin',21 ,6) --
SET NOCOUNT OFF
;WITH StaffTree AS
(
SELECT
c.id, c.name, c.reports_to_id, c.reports_to_id as Manager_id, cc.name AS Manager_name, 1 AS LevelOf, c.CompanyStoreID
FROM #CompanyEmployees c
LEFT OUTER JOIN #CompanyEmployees cc ON c.reports_to_id=cc.id
WHERE c.reports_to_id IS NULL
UNION ALL
SELECT
s.id, s.name, s.reports_to_id, t.id, t.name, t.LevelOf+1, s.CompanyStoreID
FROM StaffTree t
INNER JOIN #CompanyEmployees s ON t.id=s.reports_to_id
)
SELECT
c.id AS CompanyID, c.name AS CompanyName
,g.id AS CompanyGroupID, g.name AS CompanyName
,s.id AS CompanyStoreID, s.name AS CompanyStoreName
,t.id AS EmployeeID, t.name as EmployeeName, t.Manager_id, t.Manager_name, t.LevelOf
FROM #Company c
LEFT JOIN #CompanyGroup g ON c.id=g.CompanyID
LEFT JOIN #CompanyStore s ON g.id=s.CompanyGroupID
LEFT JOIN StaffTree t ON s.id=t.CompanyStoreID
ORDER BY c.name,g.name,s.name,s.ID,t.LevelOf,t.name
OUTPUT:
CompanyID CompanyName CompanyGroupID CompanyName CompanyStoreID CompanyStoreName EmployeeID EmployeeName Manager_id Manager_name LevelOf
--------- -------------- -------------- ----------- -------------- ---------------- ----------- ------------ ----------- ------------ -------
2 Boring Company 3 Electronics 6 FoodStore 18 apple NULL NULL 1
2 Boring Company 3 Electronics 6 FoodStore 20 grape 18 apple 2
2 Boring Company 3 Electronics 6 FoodStore 19 pear 18 apple 2
2 Boring Company 4 Food NULL NULL NULL NULL NULL NULL NULL
1 Living Things 2 Humans 5 HumansStore NULL NULL NULL NULL NULL
1 Living Things 1 Pets 1 PetsStoreA 1 Jerome NULL NULL 1
1 Living Things 1 Pets 1 PetsStoreA 9 Bill 1 Jerome 2
1 Living Things 1 Pets 1 PetsStoreA 2 Joe 1 Jerome 2
1 Living Things 1 Pets 1 PetsStoreA 6 David 2 Joe 3
1 Living Things 1 Pets 1 PetsStoreA 3 Paul 2 Joe 3
1 Living Things 1 Pets 1 PetsStoreA 10 Sam 9 Bill 3
1 Living Things 1 Pets 1 PetsStoreA 5 Daniel 3 Paul 4
1 Living Things 1 Pets 1 PetsStoreA 8 Helen 6 David 4
1 Living Things 1 Pets 1 PetsStoreA 7 Ian 6 David 4
1 Living Things 1 Pets 1 PetsStoreA 4 Jack 3 Paul 4
1 Living Things 1 Pets 2 PetsStoreB 11 Alan NULL NULL 1
1 Living Things 1 Pets 2 PetsStoreB 13 Abby 11 Alan 2
1 Living Things 1 Pets 2 PetsStoreB 12 Ally 11 Alan 2
1 Living Things 1 Pets 3 PetsStoreC 14 Ben NULL NULL 1
1 Living Things 1 Pets 3 PetsStoreC 15 Bill 14 Ben 2
1 Living Things 1 Pets 3 PetsStoreC 17 Benny 15 Bill 3
1 Living Things 1 Pets 3 PetsStoreC 16 Bjorn 15 Bill 3
1 Living Things 1 Pets 4 PetsStoreD NULL NULL NULL NULL NULL
(23 row(s) affected)
EDIT after OP's edit stating that We would like to see it in tree form.
The question is tagged sql-server-2008 and hierarchical-data, and the OP wants to do complex formatting for display f the data. However this type of processing and display is not the realm of TSQL and is a very clear example of where the application language should process and format flat data provided by a SQL query. I have provided such a query that could be used by an application to build a visual tree display. Also note that the simple tree example (no more than two children per parent) might not be very realistic and when many children exist for a single parent, the display will become difficult to construct and not pleasing to the eye.
You could use reporting services to display it back which you get with SQL 2008; if you are lucky it might be setup already -- if not its quite easy to do that. You could use the drill in features in reporting services to get allow your users to drill in and out of the data as needed very easily.
In terms of the query; does the tree grow or is it fixed? The SQL Query to get the data out from the database is quite simple though.
Select
CompanyName,
CompanyGroupName,
CompanyStoreName,
CompanyEmployeeForename,
CompanyEmployeeSurname
From tblCompanies com
left outer join tblCompanyGroups cg
on com.CompanyGroupID = cg.CompanyGroupID
Left outer Join tblCompanyStore cs
on com.CompanyID = cs.CompanyID
left outer join tblCompanyEmployees ce
on com.CompanyID = ce.CompanyName
I believe SQL Server 2008 offers a new data type to help with this scenario. Here is a link that I believe will help - http://msdn.microsoft.com/en-us/magazine/cc794278.aspx. I didn't see it in any comments, so hope this helps.