Sql FROM到TO中的序列号

时间:2015-06-17 10:19:35

标签: sql-server

这是我的主表结构。我必须在第二个表中以这种格式生成序列号 表名 - #T

SRL  CustomerCode      TotalIssue        No         TotalAmount 
1   C05515001001            16           100          1600
2   C05515001002            2            100          200
3   C05515001003            2            100          200
4   C05515001004            1            100          100
5   C05515001005            9            100          900

第二个表的结构是这样的

表名 - #TBLMember

SRL CustomerCode  TotalIssue  DistinctFrom   DistinctTo  No    Amount
 1  C05515001001    16         1543391        1543391    100    1600
 2  C05515001002    2          1543392        1543392    100    200
 3  C05515001003    2          1543393        1543393    100    200
 4  C05515001004    1          1543394        1543394    100    100
 5  C05515001005    9          1543395        1543395    100    900

DistinctFrom的默认值= 1543391                  DistinctTo = 1543391

最初第二个表是空白的,所以我使用此查询获得这些值

declare @DistinctiveNoFrom bigint=(select isnull(MAX(DistinctiveNoFrom),(1543390)) from #TBLMember)

declare @DistinctiveNoTo bigint=(select isnull(MAX(DistinctiveNoTo),(1543390)) from #TBLMember)

然后我插入此表: -

insert into #TBLMember(CustomerCode,TotalSharesIssue,No ,TotalAmount )
select c.customerCode,c.TotalIssue,@No,@No*c.TotalIssue
FROM #T c

但结果不是预期的。我想要这样的结果

SRL CustomerCode    TotalIssue  DistincFrom DistinctTo  No  TotalAmount
1   C05515001001        16       1543391    1543407     100   1600
2   C05515001002         2       1543407    1543409     100    200
3   C05515001003         2       1543409    1543411     100    200
4   C05515001004         1       1543411    1543412     100    100
5   C05515001005         9       1543412    1543421     100    900

请帮帮我,我受够了。 提前谢谢。

3 个答案:

答案 0 :(得分:0)

使用OUTER APPLY OR SUM OVER(),具体取决于您的SQL Server版本。

外部申请

declare @DistinctiveNoFrom bigint=(select isnull(MAX(DistinctiveNoFrom),(1543390)) from #TBLMember)

insert into #TBLMember(CustomerCode,TotalSharesIssue,No ,TotalAmount )
select c.customerCode,c.TotalIssue, @DistinctiveNoFrom +ISNULL(c2.prevIssue,0), @DistinctiveNoFrom + ISNULL(c2.prevIssue,0) + c.TotalIssue, 100 as No,c.TotalIssue*100
FROM #T c
OUTER APPLY(
SELECT SUM(TotalIssue) prevIssue
FROM #T c2 
WHERE c2.customerCode < c.customerCode) as c2

SUM()OVER(ORDER BY)与SQL Server 2012一起使用

declare @DistinctiveNoFrom bigint=(select isnull(MAX(DistinctiveNoFrom),(1543390)) from #TBLMember)

insert into #TBLMember(CustomerCode,TotalSharesIssue,No ,TotalAmount )
select c.customerCode,c.TotalIssue, @DistinctiveNoFrom + SUM(TotalIssue) OVER(ORDER BY customerCode ASC ROWS PRECEDING), @DistinctiveNoFrom + SUM(TotalIssue) OVER(ORDER BY customerCode ASC), 100 as No,c.TotalIssue*100
FROM #T c

答案 1 :(得分:0)

您可以使用以下递归公用表表达式查询,假设SRL是序列号,没有间隙

;WITH CTE
AS
(
    SELECT TOP 1 SRL,
        CustomerCode,
        TotalIssue,
        DistinctFrom,
        DistinctFrom + TotalIssue AS DistinctTo,
        No,
        Amount
    FROM TblMember

    UNION ALL

    SELECT TblMember.SRL,
        TblMember.CustomerCode,
        TblMember.TotalIssue,
        CTE.DistinctTo AS DistinctFrom,
        CTE.DistinctTo + TblMember.TotalIssue AS DistinctTo,
        TblMember.No,
        TblMember.Amount
    FROM CTE
    JOIN TblMember ON CTE.SRL + 1 = TblMember.SRL

)
SELECT * FROM CTE;

答案 2 :(得分:0)

 declare @membershipno bigint=(select isnull(MAX(MemberShipNo),(821931))  from #TBL_MemberShareCertificate)
 declare @DistinctiveNoFrom bigint=(select isnull(MAX(DistinctiveNoFrom),(1543390)) from #TBL_MemberShareCertificate)
 declare @DistinctiveNoTo bigint=(select isnull(MAX(DistinctiveNoTo),(1543390)) from #TBL_MemberShareCertificate)
 declare @CertificateNo bigint=(select isnull(MAX(CertificateNo),(822004)) from #TBL_MemberShareCertificate)
 declare @NominalValue_of_Shares float=100

;WITH CTE2
 AS
(
SELECT  C.SRL,C.Code,
cast(c.membershipno as BIGINT)membershipno,
 c.CertificateNo,
 C.TotalSharesIssue,
 (@DistinctiveNoFrom+1) as DistinctiveNoFrom,
 CAST(((@DistinctiveNoFrom+1)+c.TotalSharesIssue) 
 as BIGINT)AS DistinctiveNoTo
,c.NominalValue_of_Shares
,c.TotalAmount,c.MemberCeasedDate,c.Remarks
FROM #CUST C WHere C.SRL=1
UNION ALL
SELECT A.SRL,
    A.Code,cast(A.membershipno as BIGINT)membershipno,A.CertificateNo,
    A.TotalSharesIssue,
    B.DistinctiveNoTo+1 as DistinctiveNoFrom ,
    CAST((B.DistinctiveNoTo+1 + A.TotalSharesIssue) AS BIGINT)as DistinctiveNoTo,
    A.NominalValue_of_Shares,
    A.TotalAmount,A.MemberCeasedDate,A.Remarks
FROM #CUST A
INNER JOIN CTE2 B ON A.SRL  = B.SRL+1
)

INSERT INTO #TBL_MemberShareCertificate(SRL,MemberShipNo,CustomerCode,
    TotalSharesIssue,  DistinctiveNoFrom,DistinctiveNoTo,
     NominalValue_of_Shares,TotalAmount,CertificateNo,
     MemberCeasedDate,Remarks)
   SELECT C.SRL,C.membershipno,C.Code,C.TotalSharesIssue,
   C.DistinctiveNoFrom,C.DistinctiveNoTo,C.NominalValue_of_Shares,
 C.TotalAmount,C.CertificateNo,C.MemberCeasedDate,C.Remarks 
   FROM CTE2 C  OPTION(maxrecursion 0);

 **
  • 假设已创建#cust表。

**