Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

SQL Server CTE kiri luar bergabung

Jika tidak diketahui ada berapa level dalam hierarki?

Kemudian tantangan tersebut sering dilakukan melalui CTE Rekursif.

Contoh Cuplikan:

--
-- Using table variables for testing reasons
--
declare @customertest table (cid int primary key, upid int);
declare @conftest table (cid int, confname varchar(6) default 'budget', confvalue int);
--
-- Sample data
--
insert into @customertest (cid, upid) values 
(1,0), (2,1), (3,1), (4,2), (5,2), (6,3), 
(7,5), (8,5), (9,8), (10,9);
insert into @conftest (cid, confvalue) values 
(1,1000), (2,700), (3,300), (4,100), (5,200), (6,300);

-- The customer that has his own budget, or not.
declare @customerID int = 10;

;with RCTE AS 
(
  --
  -- the recursive CTE starts from here. The seed records, as one could call it.
  --
  select cup.cid as orig_cid, 0 as lvl, cup.cid, cup.upid, budget.confvalue
  from @customertest as cup
  left join @conftest budget on (budget.cid = cup.cid and budget.confname = 'budget')
  where cup.cid = @customerID -- This is where we limit on the customer

  union all

  --
  -- This is where the Recursive CTE loops till it finds nothing new
  --
  select RCTE.orig_cid, RCTE.lvl+1, cup.cid, cup.upid, budget.confvalue
  from RCTE
  join @customertest as cup on (cup.cid = RCTE.upid)
  outer apply (select b.confvalue from @conftest b where b.cid = cup.cid and b.confname = 'budget') as budget
  where RCTE.confvalue is null -- Loop till a budget is found
)
select 
 orig_cid as cid, 
 confvalue
from RCTE
where confvalue is not null;    

Hasil :

cid confvalue
--- ---------
 10       200

Btw, CTE Rekursif menggunakan OUTER APPLY karena MS SQL Server tidak mengizinkan LEFT OUTER JOIN digunakan di sana.

Dan jika sudah pasti ada kedalaman maksimal 1 level untuk upid dengan budget?
Maka tinggal join kiri saja dan penggabungan akan dilakukan.

Misalnya:

select cup.cid, coalesce(cBudget.confvalue, upBudget.confvalue) as confvalue
from @customertest as cup
left join @conftest cBudget on (cBudget.cid = cup.cid and cBudget.confname = 'budget')
left join @conftest upBudget on (upBudget.cid = cup.upid and upBudget.confname = 'budget')
where cup.cid = 8;


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana memulai pekerjaan agen SQL Server secara otomatis

  2. Izinkan karakter khusus SQL Server 2008

  3. SQL Query:Perlu urutan berdasarkan hitungan, sebagian besar harus di atas, sisanya mengikuti

  4. Menavigasi Log Kesalahan SQL Server

  5. 5 Sumber Daya Hebat untuk Membantu Anda Mengguncang Peran Pemantauan Basis Data Anda