i have a table with follow fields
UserID UserName ReportingUserID
1 Vinoth Null
2 Kumar 1
3 Raja 1
4 Ragu 2
5 Vignesh 2
6 Yoga 3
7 Yogesh 2
8 Eswar 4
9 Esakki 3
. ... .
. .... .
if i gave UserID as 1 then a query will display all users reporting to him will be displayed
if i gave USeRID as 1
Then result will be
UserID UserName ReportingUserID
2 Kumar 1
3 Raja 1
4 Ragu 2
5 Vignesh 2
6 Yoga 3
7 Yogesh 2
8 Eswar 4
9 Esakki 3
. ... .
. .... .
Have a look at using a recursive CTE.
A common table expression (CTE) can be thought of as a temporary
result set that is defined within the execution scope of a single
SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is
similar to a derived table in that it is not stored as an object and
lasts only for the duration of the query. Unlike a derived table, a
CTE can be self-referencing and can be referenced multiple times in
the same query.
Something like
DECLARE #Table TABLE(
UserID INT,
UserName VARCHAR(20),
ReportingUserID INT
)
INSERT INTO #Table VALUES
(1,'Vinoth',Null),
(2,'Kumar',1),
(3,'Raja',1),
(4,'Ragu',2),
(5,'Vignesh',2),
(6,'Yoga',3),
(7,'Yogesh',2),
(8,'Eswar',4),
(9,'Esakki',3)
DECLARE #UserID INT = 1
;WITH ReportingUsers AS (
SELECT *
FROM #Table
WHERE ReportingUserID = #UserID
UNION ALL
SELECT t.*
FROM #Table t INNER JOIN
ReportingUsers ru ON ru.UserID = t.ReportingUserID
)
SELECT *
FROM ReportingUsers
Related
I have a table that tracks movement of data, it looks like:
From_Id
To_Id
NULL
1
1
2
NULL
3
3
4
4
5
4
6
5
7
6
10
2
8
8
9
5
9
9
NULL
10
NULL
I want to structure and store every possible paths (e.g 1,2,8,9) and im unsure about the best possible way to do this with SQL.
I started off with a simple while loop, but got a problem at id=4 where it gets 2 new paths. I tried finding a solution which uses a tree structure, but I couldnt find anything that fits my case.
What is a good solution here?
Edit:
Im using microsoft sql server
desired output:
From_Id
To_Id
path
NULL
1
1,
1
2
1,2,
NULL
3
3,
3
4
3,4,
4
5
3,4,5,
4
6
3,4,6,
5
7
3,4,5,7,
6
10
3,4,6,10,
2
8
1,2,8,
8
9
1,2,8,9,
5
9
3,4,5,9,
9
NULL
NULL
10
NULL
NULL
I tried this from How to call a recursive function in sql server
DECLARE #TABLE2 TABLE(
From_Id INT,
To_id INT
)
INSERT INTO #TABLE2 SELECT NULL,1
INSERT INTO #TABLE2 SELECT 1,2
INSERT INTO #TABLE2 SELECT NULL,3
INSERT INTO #TABLE2 SELECT 3,4
INSERT INTO #TABLE2 SELECT 4,5
INSERT INTO #TABLE2 SELECT 4,6
INSERT INTO #TABLE2 SELECT 5,7
INSERT INTO #TABLE2 SELECT 6,10
INSERT INTO #TABLE2 SELECT 2,8
INSERT INTO #TABLE2 SELECT 8,9
INSERT INTO #TABLE2 SELECT 5,9
INSERT INTO #TABLE2 SELECT 9,NULL
INSERT INTO #TABLE2 SELECT 10,NULL
;WITH Recursives AS (
SELECT *,
CAST(To_id AS VARCHAR(MAX)) + ',' ID_Path
FROM #TABLE2
WHERE From_Id IS NULL
UNION ALL
SELECT t.*,
r.ID_Path + CAST(t.To_id AS VARCHAR(MAX)) + ','
FROM #TABLE2 t INNER JOIN
Recursives r ON t.From_Id = r.To_id
)
SELECT *
FROM Recursives
Which gave me an extra row (it gets much larger when i have 10k + rows)
From_Id
To_Id
ID_Path
NULL
1
1,
1
2
1,2,
NULL
3
3,
3
4
3,4,
4
5
3,4,5,
4
6
3,4,6,
5
7
3,4,5,7,
6
10
3,4,6,10
2
8
1,2,8,
8
9
1,2,8,9,
5
9
3,4,5,9,
9
NULL
NULL
9
NULL
NULL
10
NULL
NULL
Create a many-to-many relationship. A new table will be created that will only store id pairs if they exist. In the case of id 4, 2 separate records will be created. Of course, if you want to display records from a new array, you can also use the select command.
The problem arises if the From_Id and To_Id fields come from the same table. In this case, an intermediate table should be created. I will use a simple one here.
An example of a social networking site. We have a user tracking feature. In this way, we create 2 instances of the follower of the user and observe. We cannot create a direct mana-to-many relationship because this cannot directly refer to the same table. We create 2 additional tables. One will save users as followers and the other as followed. It is between these tables that we create a many-to-many relationship.
I'd like to create a view using multiple select statements that outputs multiple record sets.
For example.
CREATE VIEW DBO.EXAMPLE1
AS
SELECT * FROM BADGE WHERE BADGE.STATUS =1
SELECT * FROM EMP JOIN BADGE ON EMP.ID = BADGE.EMPID WHERE BADGE.STATUS =1
GO
I want output as follows:
ID EMPID STATUS
1 1 1
5 5 1
7 7 1
11 11 1
12 12 1
ID LASTNAME FIRSTNAME
1 Lake Lisa
4 Mattil Umar
5 Thottiyil Khalid
87 Lal NULL
7 shaikh Nabil
A view can't return multiple record sets. Only a stored procedure can do that.
I have table RD with only one column:
-----
rd_id
-----
3
2
6
7
8
I have table DL with two columns, there is hierarchy on stored on this table:
----------------------
dl_id dl_parent
----------------------
1 2
2 Null
3 Null
4 6
6 7
7 8
8 Null
Now the problem is how to get the hierarchy out from DL table using the member of RD table.
The result will be:
--------------
rd_id dl_id
--------------
3 3
2 2
6 6
6 7
6 8
7 7
8 8
8 8
I've been toiling with this problem from Friday and still can't get crack of it.
I know that I can use Common Table Expression to traverse the recursive from one value (like example create one function with input 6 and produce 6,7,8).
but I don't know how to use multiple value (rd_id).
Have some ideas?
This produces the correct results. Data setup:
declare #RD table (rd_id int not null)
insert into #RD(rd_id) values
(3),
(2),
(6),
(7),
(8)
declare #DL table (dl_id int not null,dl_parent int null)
insert into #DL(dl_id,dl_parent) values
(1,2),
(2,Null),
(3,Null),
(4,6),
(6,7),
(7,8),
(8,Null)
And the query:
;with AllValues as (
select rd_id,rd_id as dl_id from #RD
union all
select rd_id,dl_parent
from AllValues av
inner join
#DL dl
on
av.dl_id = dl.dl_id
where
dl.dl_parent is not null
)
select * from AllValues
Result:
rd_id dl_id
----------- -----------
3 3
2 2
6 6
7 7
8 8
7 8
6 7
6 8
Explanation:
In the anchor of the CTE, we simply select rd_id from the #RD table twice - since your sample implies that every input row should produce an output row with the same value in both columns.
We then join to the #DL table for any matching parent rows we can find, based on the second column. If we find a parent, then we produce a new row, substituting the parent value into the second column. This continues until no new rows are produced.
You have to use on-the-fly views to create a recursive query like this:
with n as (
select dl_id, dl_id as ancestor
from dbo.dl
union all
select np1.dl_id, n.ancestor
from dl as np1 , n
where n.dl_id = np1.dl_parent)
select * from n
where dl_id in (select rd_id from rd)
order by dl_id, ancestor
I have this table:
Id Kind
1 MODEL
1 MOTOR
2 MODEL
2 MOTOR
3 MOTOR
4 MODEL
And I want to insert into anothe table:
IdModel IdMotor
1 1
1 2
1 3
2 1
2 2
2 3
4 1
4 2
4 3
I know how to do it with cursors, but it's indeed very slow. I've tried with union but it looks like today is not my best day!
I also know this can be done in SQL 2005 with pivot, but I have to do it with SQL Server 2000.
Any Transact-SQL guru out there with a good and quick query?
Thanks in advance!
Looks like this will work:
INSERT Table2
SELECT model.id, motor.id
FROM
Table model,
Table motor
WHERE
model.Kind = 'MODEL'
and motor.Kind = 'MOTOR'
INSERT INTO AnotherTable
SELECT [IdModel]
, [IdMotor]
FROM (
SELECT [IdModel] = ID
FROM ATable
WHERE Kind = 'MODEL'
) md
CROSS APPLY
(
SELECT [IdMotor] = ID
FROM ATable
WHERE Kind = 'MOTOR'
) mt
I have three table in database.
Hostel
hostel_id int,
hosteltype_id int,
hostelname varchar(100)
address varchar(800)
hosteltypes
hosteltype_id int,
Hosteltypename varchar(100)
hostelrooms
room_id int,
hostel_id int,
Room_no int,
available_beds int
reserver int
data in Hostel
1 1 hostel1 address1
2 1 hostel2 address2
3 2 hostel3 address3
4 2 hostel4 address4
in hosteltype
1 boyshostel
2 ladieshostel
in hostelroom
1 1 101 4 4
2 1 102 4 2
3 1 103 4 4
4 2 100 4 4
5 2 101 4 1
6 3 101 4 4
I can select rows using command.
select Hostel.hostel_id, Hostel.hostelname, Hostel.address, hosteltypes.Hosteltypename,
from Hostel,hosteltypes
where Hostel.hosteltype_id=hosteltypes.hosteltype_id
and hostel_id = (
select distinct hostelrooms.hostel_id
from hostelrooms
where hostelrooms.hostel_id=Hostel.hostel_id and hostelrooms.hostelrooms>hostelrooms.reserver
)
i want data similar like
1 hostel1 address1 boyshostel
2 hostel2 address2 boyshostel
how can a create sql command similar to above using join statement which returns specific hostelid,hostelname,hosteltype where the room available.
You're missing the from clause in your SQL. It should be like:
SELECT Hostel.hostel_id, Hostel.hostelname, Hostel.address, hosteltypes.Hosteltypename
FROM Hostel
JOIN hosteltypes ON ( hosteltypes.hosteltype_id = Hostel.hosteltype_id )
JOIN hostelrooms ON ( hostelrooms.hostel_id = Hostelhostel_id )
Assuming you're trying to get a distinct list of hostels with entries in the hostelrooms table, then this should work..
select distinct (Hostel.hostel_id, Hostel.hostelname, Hostel.address, )hosteltypes.Hosteltypename,
from Hostel,hosteltypes,hostelrooms
where Hostel.hosteltype_id=hosteltypes.hosteltype_id
and hostel_id = hostelrooms.hostel_id