根据" Group"自动生成数字在GroupByExpression RadGrid中并在RadGrid中显示它

时间:2015-09-17 05:33:38

标签: sql sql-server tsql

我在实施以下要求方面遇到了麻烦。

当前RadGrid:以下是我使用的RadGrid GroupByExpressions 显示/显示与"业务部门"组合的数据列。

RadGrid第2列( InvoiceLineNo )和第3列( InvoiceNo )中,我使用存储过程自动生成数字。
代表" InvoiceLineNo"列,自动生成的编号为:01,02,03,04,05,06,07,08 ....... n
for" InvoiceNo"列,自动生成否:15100001,151002,25100003 ........ n
其中,15岁是"年"和100001是"正在运行的数字"

enter image description here

要求是:我想要显示" InvoiceLineNo"列数据以组为单位。

示例:

第一"业务部门"集团(即SUNWAY LEISURE SDN BHD(CARNIVAL)), InvoiceLineNo应为:01,02,03,04,05,06,07,08

for 2nd" Business Unit"集团(即SUNWAY MALL PARKING SDN BHD), InvoiceLineNo应为:01,02,03,04,05,06,07,08

同样,我想要显示" InvoiceNo"列数据为分组。

示例:

第一"业务部门"集团(即SUNWAY LEISURE SDN BHD(CARNIVAL)), InvoiceNo应为:15100001,15100001,15100001,15100001,15100001,15100001,15100001,15100001

for 2nd" Business Unit"集团(即SUNWAY MALL PARKING SDN BHD), InvoiceNo应为:15100002,15100002,15100002,15100002,15100002,15100002,15100002,15100002

" InvoiceNo"列数据对于不同的"业务单位"始终是唯一的。
我希望输出如下快照:

enter image description here

我可以顺序自动生成数字,但我没有得到如何基于Group自动生成2列值并将其显示为 那。

请帮助我实现它。请回复。

提前致谢。

修改

以下是我用于在RadGrid的第2栏中生成自动生成的数字的存储过程:

ALTER PROCEDURE [dbo].[SDM_Assign_RunningNo]
    -- Add the parameters for the stored procedure here
    @TableName as nvarchar(50),
    @NewID as nvarchar(50) OUTPUT
    AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;


       DECLARE @rn_year as nvarchar(50)

       --Get Year From table            
       SELECT @rn_year =RNYear FROM dbo.SDM_Tran_RunningNo
       WHERE RNYear= YEAR(GetDate())

       --get last 2 digits of year
       Declare @2digit_rn_year as nvarchar(50)

       SELECT @2digit_rn_year = RNYear % 100 FROM dbo.SDM_Tran_RunningNo
       WHERE RNYear= YEAR(GetDate())


        IF @TableName='SDM_Tran_GenerateInvoice_No'
    BEGIN   
        SELECT @NewID=Next_InvoiceNo FROM dbo.SDM_Tran_RunningNo
        WHERE RNYear=@rn_year

        UPDATE dbo.SDM_Tran_RunningNo
        SET Next_InvoiceNo=Next_InvoiceNo+1  
        WHERE RNYear=@rn_year

        SET @NewID = @2digit_rn_year +'1'+RIGHT('000000' + CAST(@NewID as varchar(10)), 5)
    END

    ELSE IF @TableName='SDM_Tran_GenerateInvoice_LineNo'
    BEGIN
        SELECT @NewID=Next_InvoiceLineNo FROM dbo.SDM_Tran_RunningNo
        WHERE RNYear=@rn_year

        UPDATE dbo.SDM_Tran_RunningNo
        SET Next_InvoiceLineNo=Next_InvoiceLineNo+1  
        WHERE RNYear=@rn_year

        SET @NewID = RIGHT('000000' + CAST(@NewID as varchar(10)), 2)
    END

END

然后将2列值插入表中(如下所示)(使用存储过程), 在RadGrid中显示它:

    DECLARE @InvoiceNo as nvarchar(50)
--SP to generate new Invoice No 
EXEC dbo.SDM_Assign_RunningNo
@TableName='SDM_Tran_GenerateInvoice_No',
@NewID = @InvoiceNo OUTPUT

DECLARE @InvoiceLineNo as nvarchar(50)
--SP to generate new Invoice Line No 
EXEC dbo.SDM_Assign_RunningNo
@TableName='SDM_Tran_GenerateInvoice_LineNo',
@NewID = @InvoiceLineNo OUTPUT

INSERT INTO SDM_Tran_GenerateInvoice
VALUES (@InvoiceID,
@SPfoID, 
@InvoiceLineNo, @InvoiceNo, @InvoiceType, 
@BillingIDfoID, @BusinessUnit, @DirectCost,
@Status, GETDATE(), @AccountCode)

1 个答案:

答案 0 :(得分:0)

这是一个概念......您可能需要对其进行修改以满足您的要求。

    DECLARE @Temp TABLE ( 
InvoiceID nvarchar(50), 
SPfoID nvarchar(50), 
InvoiceLineNo nvarchar(50), 
InvoiceNo nvarchar(50), 
InvoiceType nvarchar(50), 
BillingIDfoID nvarchar(50), 
BusinessUnit nvarchar(2000),
DirectCost nvarchar(2000),
Status nvarchar(10),
Date datetime,
AccountCode nvarchar(1000)
) 

DECLARE @Temp1 TABLE ( 
OrderID INT IDENTITY, -- Added This so It Will follow this new Identity Row 
InvoiceID nvarchar(50), 
SPfoID nvarchar(50), 
InvoiceLineNo nvarchar(50), 
InvoiceNo nvarchar(50), 
InvoiceType nvarchar(50), 
BillingIDfoID nvarchar(50), 
BusinessUnit nvarchar(2000),
DirectCost nvarchar(2000),
Status nvarchar(10),
Date datetime,
AccountCode nvarchar(1000)
) 

DECLARE @CompanyValue nvarchar(2000) = '' --BusinessUnit datatype
DECLARE @Counter nvarchar(50) = '0' --InvoiceNo datatype
DECLARE @InvoiceLine INT = 1 
DECLARE @Year INT = YEAR(GETDATE()) 
DECLARE @ShortYear VARCHAR(2) = SUBSTRING(CONVERT(VARCHAR(4), @Year), 3, 2) 

EXEC dbo._RunningNo
    @TableName='Invoice',
    @NewID = InvoiceID --OUTPUT

INSERT INTO @Temp (InvoiceID, SPfoID, InvoiceType, BillingIDfoID, BusinessUnit, DirectCost, Status, Date, AccountCode)
SELECT InvoiceID, SPfoID, InvoiceType, BillingIDfoID, BusinessUnit, DirectCost, Status, Date, AccountCode FROM [MainTable] ORDER BY BusinessUnit

INSERT INTO @Temp1 
SELECT * FROM @Temp ORDER BY BusinessUnit 

SELECT * FROM @Temp 
SELECT * FROM @Temp1 -- before update

--Update @Temp1 table
UPDATE @Temp1 
SET 
@Counter = InvoiceNo = CASE WHEN @CompanyValue = '' OR @CompanyValue = BusinessUnit THEN (CONVERT(VARCHAR(100), CONVERT(INT,@Counter) + 1)) ELSE '1' END, 
@InvoiceLine = CASE WHEN @CompanyValue = '' OR @CompanyValue = BusinessUnit THEN @InvoiceLine ELSE @InvoiceLine + 1 END, 
@CompanyValue = BusinessUnit, 
InvoiceLineNo = @ShortYear + '10000' + CONVERT(VARCHAR(3), @InvoiceLine) 

SELECT * FROM @Temp1 --after update

--Update main table
UPDATE g
SET  g.InvoiceLineNo = t.InvoiceLineNo,
     g.InvoiceNo = t.InvoiceNo
FROM  SDM_Tran_GenerateInvoice g
INNER JOIN @Temp1 t 
ON g.InvoiceID = t.InvoiceID


Select * from [MainTable]
ORDER BY BusinessUnit;