【问题标题】:SQL query - Show main account and all secondary accountsSQL 查询 - 显示主帐户和所有辅助帐户
【发布时间】:2017-08-09 13:03:18
【问题描述】:

我知道我可以通过将“from Account”和“to Account”存储到变量或其他东西中来通过某种循环来完成这项工作......但我正在寻找一种更简单的方法。

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

我尝试了多个查询、子查询,但都没有成功。

【问题讨论】:

    标签: sql-server tsql sql-server-2005


    【解决方案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
    

    【讨论】:

    • 谢谢。长五。交叉加入。不错。
    【解决方案2】:

    这应该在一个查询中完成您想要的:

    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
    

    按照您在问题中的示例,我们可以将结果限制为 419994299943999

    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
    

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      相关资源
      最近更新 更多