1

我在 SQL Server 2005 中工作,创建一个存储过程来从我的数据库中提取付款人/账单数据。

存储过程创建一个临时表以传回应用程序 (VB.NET)。第一部分将期间余额表中的每条记录提取到临时表中,然后确定该期间余额的“年龄”(当前、逾期 30 天、逾期 60 天等)。这工作正常。

我现在需要做的是确定当前期间的收费天数。因此,我从年龄为“CUR”的临时表中提取所有记录。天数存储在我的数据库中的一个单独的表中,称为费用。一个期间可能有多个收费记录。在这些情况下,我想要该期间所有天数的总和。

例如,一个人在 3 月 1 日 - 3 月 31 日期间可能有 4 条单独的收费记录:第一个记录有 3 天,第二个记录有 6 天,第三个记录有 1 天,第四个记录有 5 天。我在临时表中想要的只是此人在此期间的一条记录,天数为 15。

目前,该程序正在为 3 人(102 人中)生成正确的天数。82 人有 NULL 天。有几个人是正确的,但绝对不是其中的 82 个。其余的人似乎有随机增加的日子。天数不能超过当月的天数,但有些人将天数列设置为数百甚至数千的数字。

这与我昨天发布的问题有关,但与报告不同。

我将不胜感激您能提供的任何帮助。

CREATE PROCEDURE [AgedPayorReport]    
    @VCurrStart     DATETIME
AS
BEGIN
    SET NOCOUNT ON;

    BEGIN TRY

        CREATE TABLE #t_temp
            (                    
                bal                     DECIMAL(11, 2), 
                period                  DATETIME,
                account_code            INT,
                payor_code              INT,
                plan_code               INT,        
                age                     VARCHAR(10),
                census_days             INT
            )          

        DECLARE @VCurrEnd           DATETIME
        DECLARE @V30Start           DATETIME
        DECLARE @V60Start           DATETIME
        DECLARE @V90Start           DATETIME
        DECLARE @V120Start          DATETIME
        DECLARE @V150Start          DATETIME
        DECLARE @V180Start          DATETIME
        DECLARE @V210Start          DATETIME
        DECLARE @V240Start          DATETIME

SET @VCurrEnd = DATEADD(DAY,-1,(DATEADD(MONTH,1,@VCurrStart)))
SET @V30Start = DATEADD(month,-1,@VCurrStart)
SET @V60Start = DATEADD(month,-2,@VCurrStart)
SET @V90Start = DATEADD(month,-3,@VCurrStart)
SET @V120Start = DATEADD(month,-4,@VCurrStart)
SET @V150Start = DATEADD(month,-5,@VCurrStart)
SET @V180Start = DATEADD(month,-6,@VCurrStart)
SET @V210Start = DATEADD(month,-7,@VCurrStart)
SET @V240Start = DATEADD(month,-8,@VCurrStart)

INSERT INTO #t_temp
            (                    
                bal,
                period,             
                age
            )            
SELECT    
                b.ledger_bal,
                b.period,               
                (SELECT CASE 
                WHEN period > @VCurrStart THEN 'ADV'
                WHEN period = @VCurrStart THEN 'CUR'
                WHEN period = @V30Start THEN '30'
                WHEN period = @V60Start THEN '60'
                WHEN period = @V90Start THEN '90'
                WHEN period = @V120Start THEN '120'
                WHEN period = @V150Start THEN '150'
                WHEN period = @V180Start THEN '180'
                WHEN period = @V210Start THEN '210'
                WHEN period = @V240Start THEN '240'
                WHEN period < @V240Start THEN '270'
                END)
            FROM balance b WITH(NOLOCK)         


SELECT *
INTO #t_temp_CUR
FROM #t_temp
WHERE age = 'CUR'


UPDATE t
SET t.census_days = (charge.sum_days)
FROM #t_temp_CUR t
INNER JOIN 
    (
        SELECT account_code, SUM(days) AS sum_days
        FROM charge WITH(NOLOCK)
        GROUP BY account_code
    ) as charge
    ON charge.account_code = t.account_code
INNER JOIN charge c WITH(NOLOCK)
ON c.account_code = t.account_code
AND c.payor_code = t.payor_code
AND c.plan_code = t.plan_code
WHERE c.start_date >= @VCurrStart
AND c.end_date <= @VCurrEnd
AND c.type = 'C'
AND c.void <> 1
AND c.days <> 0
AND c.days IS NOT NULL


END TRY
    BEGIN CATCH

    END CATCH
END

编辑添加示例数据:

现有期间余额表:

account_code    payor_code      plan_code       period      ledger_bal  period_bal_code
---------------------------------------------------------------------------------------------------------------
9421            493             224             2012-07-01  191.82      30813
9420            6833            527             2012-07-01  767.28      30810
9419            6533            510             2012-07-01  1400.00     30806
9418            6533            510             2012-07-01  1400.00     30803
9417            493             224             2012-07-01  959.10      30800
9416            2193            340             2012-07-01  244.17      30794
9416            8468            584             2012-07-01  1370.00     30795
9416            8935            433             2012-07-01  250.00      30798
9415            493             224             2012-07-01  1150.92     30791
9414            6158            495             2012-07-01  2916.00     30788
9413            6833            527             2012-07-01  6385.61     30785
9412            6034            474             2012-07-01  8488.16     30781
9412            8930            433             2012-07-01  800.00      30783
9411            493             224             2012-07-01  7840.18     30777
9410            8468            584             2012-07-01  6156.00     30776

费用表中的一些对应记录:

account_code    payor_code      plan_code   charge_code start_date  end_date    quantity    gross_amount    net_amount      days    void
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
9413            6833            527         5070631     2012-07-21  2012-07-31  1           2508.00         6385.61         11      0
9411            493             224         5070755     2012-07-16  2012-07-29  1           3192.00         6972.28         14      0
9411            493             224         5070757     2012-07-30  2012-07-31  1           456.00          867.90          2       0
9416            8468            584         5071869     2012-07-27  2012-07-31  1           1140.00         1620.00         5       0
9418            6533            510         5072255     2012-07-28  2012-07-31  1           912.00          1400.00         4       0
9417            493             224         5073818     2012-07-27  2012-07-31  1           1140.00         959.10          5       0
9410            8468            584         5075878     2012-07-12  2012-07-30  1           4332.00         6156.00         19      0
9420            6833            527         5076857     2012-07-28  2012-07-31  1           912.00          767.28          4       0
9412            6034            474         5076991     2012-07-16  2012-07-16  1           228.00          580.51          1       0
9412            6034            474         5076994     2012-07-17  2012-07-29  1           2964.00         7546.63         13      0
9412            6034            474         5076997     2012-07-30  2012-07-31  1           456.00          1161.02         2       0
9421            493             224         5077918     2012-07-31  2012-07-31  1           228.00          191.82          1       0
9414            6158            495         5079045     2012-07-23  2012-07-31  1           2052.00         2916.00         9       0
9419            6533            510         5079366     2012-07-28  2012-07-31  1           912.00          1400.00         4       0
9415            493             224         5079418     2012-07-26  2012-07-31  1           1368.00         1150.92         6       0
9410            8468            584         5084838     2012-07-12  2012-07-30  1           -4332.00        -6156.00        19      0
9410            8468            584         5085112     2012-07-12  2012-07-30  1           4332.00         6156.00         19      0
9413            6833            527         5085371     2012-08-01  2012-08-03  1           684.00          1741.53         3       0
9413            6833            527         5085373     2012-08-04  2012-08-09  1           1368.00         1150.92         6       0
9413            6833            527         5085375     2012-08-10  2012-08-31  1           5016.00         4220.04         22      0

所以考虑#9411。当前存储过程产生:

ledger_bal  period      account_code    payor_code      plan_code       age     census_days
------------------------------------------------------------------------------------------------
7840.18     2012-07-01  9411            493             224             CUR         70

但我希望它产生:

ledger_bal  period      account_code    payor_code      plan_code       age     census_days
    ------------------------------------------------------------------------------------------------
7840.18     2012-07-01  9411            493             224             CUR         16
4

1 回答 1

0

我终于弄明白了。

我创建了另一个名为 #t_days 的表,其中仅包含链接到费用表所需的字段以及天数。我仍然将当前的#t_temp 记录选择到#t_temp_CUR 中,然后将其链接我的收费表以在#t_days 中插入各个收费记录。然后我运行更新语句并从#t_days 中提取 census_days 的总和。这现在为每条记录生成正确的天数。

谢谢!

CREATE PROCEDURE [AgedPayorReport]    
    @VCurrStart     DATETIME
AS
BEGIN
    SET NOCOUNT ON;

    BEGIN TRY

        CREATE TABLE #t_temp
            (                    
                bal                     DECIMAL(11, 2), 
                period                  DATETIME,
                account_code            INT,
                payor_code              INT,
                plan_code               INT,        
                age                     VARCHAR(10),
                census_days             INT
            )

    CREATE TABLE #t_days
            (               
                account_code            INT,
                payor_code              INT,
                plan_code               INT,                
                census_days             INT             
            )

        DECLARE @VCurrEnd           DATETIME
        DECLARE @V30Start           DATETIME
        DECLARE @V60Start           DATETIME
        DECLARE @V90Start           DATETIME
        DECLARE @V120Start          DATETIME
        DECLARE @V150Start          DATETIME
        DECLARE @V180Start          DATETIME
        DECLARE @V210Start          DATETIME
        DECLARE @V240Start          DATETIME

SET @VCurrEnd = DATEADD(DAY,-1,(DATEADD(MONTH,1,@VCurrStart)))
SET @V30Start = DATEADD(month,-1,@VCurrStart)
SET @V60Start = DATEADD(month,-2,@VCurrStart)
SET @V90Start = DATEADD(month,-3,@VCurrStart)
SET @V120Start = DATEADD(month,-4,@VCurrStart)
SET @V150Start = DATEADD(month,-5,@VCurrStart)
SET @V180Start = DATEADD(month,-6,@VCurrStart)
SET @V210Start = DATEADD(month,-7,@VCurrStart)
SET @V240Start = DATEADD(month,-8,@VCurrStart)

INSERT INTO #t_temp
            (                    
                bal,
                period,             
                account_code,
                payor_code,
                plan_code,
                age
            )            
SELECT    
                b.ledger_bal,
                b.period, 
                b.account_code,
                b.payor_code,
                b.plan_code,
                (SELECT CASE 
                WHEN period > @VCurrStart THEN 'ADV'
                WHEN period = @VCurrStart THEN 'CUR'
                WHEN period = @V30Start THEN '30'
                WHEN period = @V60Start THEN '60'
                WHEN period = @V90Start THEN '90'
                WHEN period = @V120Start THEN '120'
                WHEN period = @V150Start THEN '150'
                WHEN period = @V180Start THEN '180'
                WHEN period = @V210Start THEN '210'
                WHEN period = @V240Start THEN '240'
                WHEN period < @V240Start THEN '270'
                END)
            FROM balance b WITH(NOLOCK)         


SELECT *
INTO #t_temp_CUR
FROM #t_temp
WHERE age = 'CUR'


INSERT INTO #t_days
            (               
                account_code,
                payor_code,             
                plan_code,
                census_days
            )
SELECT 
t.account_code, 
t.payor_code, 
t.plan_code,
c.days
FROM #t_temp_CUR t
INNER JOIN br549.charge c WITH(NOLOCK)
ON t.account_code = c.account_code
AND t.payor_code = c.payor_code
AND t.plan_code = c.plan_code
WHERE c.type = 'C'
AND c.advance = ''
AND c.void <> 1
AND c.start_date >= @vkVCurrStart
AND c.end_date <= @vkVCurrEnd


UPDATE #t_temp
SET census_days = 
(SELECT SUM(census_days)
FROM #t_days
WHERE #t_days.account_code = #t_temp.account_code
AND #t_days.payor_code = #t_temp.payor_code
AND #t_days.plan_code = #t_temp.plan_code
AND #t_days.period = #t_temp.period)

SELECT * FROM #t_temp


END TRY
    BEGIN CATCH

    END CATCH
END
于 2013-04-16T16:35:22.027 回答