declare @t table ([source] varchar(1),direct varchar(1),mile int)
insert into @t
select 'a','b',20 union all
select 'b','c',10 union all
select 'c','d',15 union all
select 'd','e',10 union all
select 'e','f',5 union all
select 'd','f',15 union all
select 'b','d',20 union all
select 'a','e',10 union all
select 'a','f',15
--sql查詢如下:
;with liang as
select
[source] as [from],direct as [to],mile
from @t
union all
select
direct,[source],mile
from @t
liang2 as
select
[from],[to],
mile as totalmile,
cast('.'+[from]+'.'+[to]+'.' as varchar(max)) as path
from liang
union all
select
a.[from],b.[to],
a.totalmile+b.mile,
cast(a.path+b.[to]+'.' as varchar(max))
from liang2 as a
join liang as b
on case when a.path like '%.'+b.[to]+'.%'
then 1 else 0 end=0
and a.[to]=b.[from]
liang3 as
select
[from],[to],
min(totalmile) as minmile
from liang2
group by [from],[to]
select b.*
from liang3 as a
join liang2 as b
on a.[from]=b.[from]
and a.[to]=b.[to]
and a.minmile=b.totalmile
where a.[from]='a'
and a.[to]='c'
option(maxrecursion 0)
SQL語句從入門到精通之筆記大全
sql中有四種基本的dml操作 insert,select,update和delete。由於這是大多數sql使用者經常用到的,我們有必要在此對它們進行說明。sql select from customers where id between rnumber and rnumber 9 注意 以上 的...
sql 語句寫的行列轉換
以前面試老遇到乙個行列轉換的問題,今天沒事,順便記錄一下 假設有這樣一張表,如下圖,建立表就不說了,直接建或者sql語句都行 sql語句如下 第一種 select name as 姓名,max case subject when 語文 then result else 0 end as 語文,max...
在VS裡面寫SQL語句將資料插入到Oracle裡面
oracle資料庫的表的結構如下,我想在vs裡面動態的插入3列,datacat id predatacat id datacat chnname這3列,這裡只說拼接的字串,不討論執行的方法,int i 1,string datasetname 下面的字串可以直接貼上到vs裡面直接用 string s...