2017-03-17 85 views
0

我知道我可以通過將「從賬戶」和「賬戶」存儲到變量或其他東西來完成某種循環,但我在尋找更簡單的方法。SQL查詢 - 顯示主賬戶和所有二級賬戶

MainAccount表顯然包含1個帳號,它也存儲在AccountsInterval表中。在該表中,有一個範圍(從帳戶到帳戶)。如果沒有循環,我很難找到一種方法來獲取每個主要帳戶的所有輔助帳戶。有更容易的方法嗎?

CREATE TABLE #MainAccounts(id INT IDENTITY(1,1) PRIMARY KEY, MainAccount NVARCHAR(20)) 
    INSERT INTO #MainAccounts(MainAccount) VALUES('41000') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('41010') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('41011') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('41999') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('42000') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('42010') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('42015') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('42020') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('42030') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('42080') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('42310') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('42999') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('43999') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('48000') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('48100') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('48199') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('48200') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('48210') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('48220') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('48299') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('48999') 
    INSERT INTO #MainAccounts(MainAccount) VALUES('49999') 

CREATE TABLE #AccountsInterval(id INT IDENTITY(1,1) PRIMARY KEY, MainAccount NVARCHAR(20), FromAccount NVARCHAR(20), ToAccount NVARCHAR(20)) 
INSERT INTO #AccountsInterval(MainAccount, FromAccount, ToAccount) VALUES('41999', '41000', '41999') 
INSERT INTO #AccountsInterval(MainAccount, FromAccount, ToAccount) VALUES('42999', '42000', '42999') 
INSERT INTO #AccountsInterval(MainAccount, FromAccount, ToAccount) VALUES('43999', '41000', '43999') 
INSERT INTO #AccountsInterval(MainAccount, FromAccount, ToAccount) VALUES('48199', '48000', '48199') 
INSERT INTO #AccountsInterval(MainAccount, FromAccount, ToAccount) VALUES('48299', '48200', '48299') 
INSERT INTO #AccountsInterval(MainAccount, FromAccount, ToAccount) VALUES('48999', '48000', '48999') 
INSERT INTO #AccountsInterval(MainAccount, FromAccount, ToAccount) VALUES('49999', '41000', '49999') 

如果我們使用示例帳戶; 41999,42999,43999 ...我們應該得到下面的結果。

Main Secondary 
41999 41000 
41999 41010 
41999 41011 
41999 41999 
42999 42000 
42999 42010 
42999 42015 
42999 42020 
42999 42030 
42999 42080 
42999 42310 
42999 42999 
43999 41000 
43999 41010 
43999 41011 
43999 41999 
43999 42000 
43999 42010 
43999 42015 
43999 42020 
43999 42030 
43999 42080 
43999 42310 
43999 42999 
43999 43999 

我試過多個查詢,子查詢,我沒有得到任何地方。

回答

1
select ai.MainAccount as "Main", mi.MainAccount as "Secondary" 
from #AccountsInterval ai 
join #MainAccounts mi on mi.MainAccount >= ai.FromAccount and mi.MainAccount <= ai.ToAccount 

...或...或者

select ai.MainAccount as "Main", mi.MainAccount as "Secondary" 
from #AccountsInterval ai 
cross join #MainAccounts mi 
where mi.MainAccount >= ai.FromAccount and mi.MainAccount <= ai.ToAccount 
+0

感謝。長週五。交叉加入。不錯。 – manderson

1

這應該做你在一個查詢想要的東西:

Select M.MainAccount As Main, S.MainAccount As Secondary 
From #MainAccounts  M 
Join #AccountsInterval I On M.MainAccount = I.MainAccount 
Join #MainAccounts  S On Convert(Int, S.MainAccount) Between Convert(Int, I.FromAccount) 
                   And  Convert(Int, I.ToAccount) 
Order By Main, Secondary 

繼的問題你的例子中,我們可以將結果限制爲僅4199942999,並43999

Select M.MainAccount As Main, S.MainAccount As Secondary 
From #MainAccounts  M 
Join #AccountsInterval I On M.MainAccount = I.MainAccount 
Join #MainAccounts  S On Convert(Int, S.MainAccount) Between Convert(Int, I.FromAccount) 
                   And  Convert(Int, I.ToAccount) 
Where M.MainAccount In ('41999', '42999', '43999') 
Order By Main, Secondary 

Main Secondary 
41999 41000 
41999 41010 
41999 41011 
41999 41999 
42999 42000 
42999 42010 
42999 42015 
42999 42020 
42999 42030 
42999 42080 
42999 42310 
42999 42999 
43999 41000 
43999 41010 
43999 41011 
43999 41999 
43999 42000 
43999 42010 
43999 42015 
43999 42020 
43999 42030 
43999 42080 
43999 42310 
43999 42999 
43999 43999