从SQL

时间:2018-01-24 17:40:28

标签: sql-server

我确信其他人不得不做类似的事情,但我很难找到一个我需要的例子。

我有一个可能随时间变化的阵列。我正在尝试使用该数组在我的团队之间平均分配我的帐户组合。我无法直接更改SQL数据库,因此我必须通过XML提取并重新上传才能完成此任务。

这是伪代码(请原谅我编写奇怪的伪代码):

    Declare @Collector as array
    Declare @LoopedValue as varchar (60)
    set    @Collector [Team1, Team2, Team3]

    Set @LoopedValue to index 0 of array

    For each row returned    

    SELECT

    '<LOAN UpdateFlag= "1" LoanNumber= "'+la.loan_number+'" CollectionOfficerNumber= "'+@LoopedValue+'"> </LOAN>'

    FROM loanacct la

    where
    la.portfolio_code_id = 1
    AND status_code_no = 0
    AND la.acctrefno not IN (SELECT las.acctrefno from loanacct_statuses AS las where las.status_code_no IN  (11, 12, 13, 14, 17, 19, 20, 22, 26, 28, 35, 36, 38, 39, 62)
        )
        --Excludes accounts with Status Codes 11=Repossessed; 12=Pending Repo; 13=Bankruptcy Ch 7; 14=Charge-off; 17=Unsecured bad debt; 19=Clear to sell; 20=Bankruptcy Ch. 13; 22=Repo Sold; 26:Voluntary repossession; 28=Move to inventory; 35=Deficiency; 36=Unsecured Balance; 38=Discharged Ch 7 BK; 39=Discharged Ch13 BK: 62=Pre-Bankruptcy

Set @LoopedValue  to @LoopedValue+1. 
If @LoopedValue > 2 set @LoopedValue to 0 Else end
end

    ORDER by la.days_past_due desc

我的问题是我将在@LoopedValue部分放入什么来实现第1列= Team1,第2列= Team2,Column3 = Team3,Column4 = Team1等结果?

我实际上不想在团队中分配随机值,因为这可能会造成帐户难度的不平衡。

1 个答案:

答案 0 :(得分:0)

根据我的评论建议的代码,使用ROW_NUMBER()和模数为团队创建循环索引。

-- Given table of loans =================================
    CREATE TABLE #Loan ( 
        Loan_Number int
        , Loan_Name varchar(30) 
        , Days_Past_Due int
        );

    INSERT INTO #Loan VALUES
        (23, 'First loan', 23),
        (34, 'alksdjfl', 12 ),
        (123, 'aswerjlj', 17 ),
        (324, 'asdasdf', 52 ),
        (8126, 'slaksjdrjlj', 80 ),
        (323, 'asdfasldfkj', 123 ),
        (224, 'alsdfal', 4 ),
        (2342, 'asldkfasldkj', 5 ),
        (451, 'asdlfjkasldjf', 67 )
        ;
-- =======================================================

-- Create and load a table with teams indexed starting at 1
Create Table #Team (TeamIX int, Team_Name varchar(50))
Insert into #Team 
    SELECT ROW_Number() OVER ( ORDER BY ( SELECT NULL) )  TeamIX
            , Team_Name from ( VALUES ( 'Team1' ), ('Team2'), ('Team3' ) ) TT(Team_Name) ;

-- Get the number of teams to use as the modulus divisor
DECLARE @TeamMax int; 
SELECT @TeamMax = COUNT(*) FROM #Team;


-- Create a temp table #FORXML of loans joined with teams by
-- allocating loan to team using modulus of row number
SELECT
 L.Loan_Number
 , L.TeamIX 
 , L.Loan_Name
 , T.Team_Name
 , L.Days_Past_Due
 INTO #FORXML
    FROM ( 
            SELECT ( 
                        -- TeamIX provides a cycling set of team numbers 1,2,3 etc up to the max team number
                        ROW_Number() OVER ( ORDER BY (SELECT Days_Past_Due ) DESC ) - 1 ) % @TeamMax + 1 TeamIX
                    , Loan_Number
                    , Loan_Name 
                    , Days_Past_Due
            FROM #LOAN 
        ) L
    JOIN #Team T
        ON T.TeamIX = L.TeamIX
    ;

-- Declare the xml variable to receive the generated XML
DECLARE @XML XML;

-- Generate the XML from the #FORXML table
SET @XML = '<LOANS>' + (
        SELECT Loan_Number [@LoanNumber]
                , TeamIX [@CollectionOfficerNumber] 
                , Loan_Name [@LoanName]
                , Team_Name [@TeamName]
                , Days_Past_Due [@DaysPastDue]
                FROM #FORXML FOR XML PATH ('LOAN') 
        ) + '</LOANS>';

    -- Report the XML for testing
    SELECT @XML XML;

-- CLEAN UP TEMP TABLES
Drop Table #Team;
Drop Table #Loan;
Drop Table #FORXML;

哪个代码给出了以下结果:

<LOANS>
  <LOAN LoanNumber="323" CollectionOfficerNumber="1" LoanName="asdfasldfkj" TeamName="Team1" DaysPastDue="123" />
  <LOAN LoanNumber="8126" CollectionOfficerNumber="2" LoanName="slaksjdrjlj" TeamName="Team2" DaysPastDue="80" />
  <LOAN LoanNumber="451" CollectionOfficerNumber="3" LoanName="asdlfjkasldjf" TeamName="Team3" DaysPastDue="67" />
  <LOAN LoanNumber="324" CollectionOfficerNumber="1" LoanName="asdasdf" TeamName="Team1" DaysPastDue="52" />
  <LOAN LoanNumber="23" CollectionOfficerNumber="2" LoanName="First loan" TeamName="Team2" DaysPastDue="23" />
  <LOAN LoanNumber="123" CollectionOfficerNumber="3" LoanName="aswerjlj" TeamName="Team3" DaysPastDue="17" />
  <LOAN LoanNumber="34" CollectionOfficerNumber="1" LoanName="alksdjfl" TeamName="Team1" DaysPastDue="12" />
  <LOAN LoanNumber="2342" CollectionOfficerNumber="2" LoanName="asldkfasldkj" TeamName="Team2" DaysPastDue="5" />
  <LOAN LoanNumber="224" CollectionOfficerNumber="3" LoanName="alsdfal" TeamName="Team3" DaysPastDue="4" />
</LOANS>