我在SQL Server 2005中工作,创建了一个存储过程来从我的数据库中提取付款人/计费数据。
存储过程创建临时表以传递回应用程序(VB.NET)。第一部分将期间余额表中的每条记录拉入临时表,然后确定该期间余额的“年龄”(当前,逾期30天,逾期60天等)。这很好。
我现在需要做的是确定当前期间的收费天数。所以我从临时表中提取所有年龄为'CUR'的记录。天数存储在我的数据库中名为charge的单独表中。一个时期可能有多个费用记录。在那些情况下,我想要那段时间的所有日子的总和。
例如,3月1日至3月31日期间的一个人可能有4个单独的收费记录:第一个记录有3天,第二个记录有6天,第三个记录有1天,第四个记录有5天。在我的临时表中我想要的只是这个时期这个人的一条记录,日期值为15。目前,该程序为3人(102人中)生成了正确的天数。 82人每天都有NULL。有一些人认为这是正确的,但绝对不是82人。剩下的人似乎有几天随机增加。天数不能超过该月的天数,但有些人将天数列设置为数百甚至数千的数字。
这与the question I posted yesterday有关,但与报告不同。
感谢您提供任何帮助。
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
答案 0 :(得分:0)
我终于明白了。
我创建了另一个名为#t_days的表,其中只包含链接到收费表所需的字段以及日期。我仍然在#t_temp_CUR中选择当前的#t_temp记录,然后将 链接到我的费用表,以在#t_days中插入各个费用记录。然后我运行一个更新语句并从#t_days中提取census_days的SUM。现在,这为每条记录生成了正确的天数。
谢谢!
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