0

我正在尝试更新 SSRS 中的报告。该查询是由另一位同事使用存储过程构建的,该同事已离开项目并且无法再联系。他为所有批次使用了一个内存表,以便添加一些聚合。

作为输出的列是:

  • 批号
  • 转换日期
  • 源系统
  • 公司类型
  • MAX(B.FooterLines) A​​S FooterTotalLines
  • MAX(SRC.TotalLines) 作为 SourceTotalLines
  • MAX(BalancingLinesTable.BalancingLines) A​​S 平衡线
  • SUM(STG.TotalLines) 作为 StagingTotalLines
  • (CAST(STG.Company AS int) * 1000000) + B.BatchID AS CompanyCode

但是我需要添加一个名为平衡线的额外列。我为它开发了查询并且它有效:

SELECT COUNT(0) as "Balancing lines"
FROM [Source].[Staging].[TransactionLine] T
INNER JOIN 
[Source].[Staging].HeaderLine H
ON T.HeaderID = H.HeaderID
where H.BatchID  = 1234* and H.SourceSystemInstance = 'ABC' and AccountNumber = '98765'
*1234 should be variable -> B.BatchID and also the SourseSystemInstance 'ABC'->     B.SourceSystem 

但是,我同事写的存储过程对我来说仍然很陌生。你知道如何将我的工作查询“添加”到他的工作存储过程中吗?

为清楚起见,使用的表格:

  • @批次 B
  • 标题行 H
  • 页脚线 FL
  • 'INNER JOIN 表' BalancingLinesTable
  • 'INNER JOIN 表' SRC
  • 'LEFT JOIN 表' STG

存储过程:

USE [Source]

ALTER PROCEDURE  [dbo].[usp_GetSomeCounts] (
@StartAt datetime
)
AS BEGIN 
SET NOCOUNT ON;

-- Set a default for start date if none is specified 
SELECT @StartAt = ISNULL(@StartAt, DATEADD(dd, -7, GETDATE()))

-- Use an in-memory table for all batches for the specified period 
DECLARE @batches TABLE(BatchID int, TransformDate datetime, HeaderID nvarchar(36), CompanyType nvarchar(30), SourceSystem nvarchar(4), FooterLines int)

-- Fill in-memory table batches
INSERT INTO @batches (
     BatchID
     ,TransformDate
    ,HeaderID
    ,CompanyType
    ,SourceSystem
    ,FooterLines
)
    SELECT
         H.BatchID
        ,H.TransformDate
        ,H.HeaderID
        ,CompanyTypeID
        ,H.SourceSystemInstance
        ,ISNULL(FL.TotalTransactionLines, 0)
    FROM
        Staging.HeaderLine H WITH (NOLOCK)
    INNER JOIN
        Staging.FooterLine FL WITH (NOLOCK)
    ON
        H.HeaderID = FL.HeaderID
    WHERE
        H.BatchDate >= @StartAt
    ORDER BY
        H.BatchID

/*  Using in-memory table 'batches', count all valid transactions in both Source and Staging
    databases. Include the footer totals */
SELECT
      B.BatchID
    , B.TransformDate
    , B.SourceSystem
    , B.CompanyType
    , MAX(B.FooterLines) AS FooterTotalLines
    , MAX(SRC.TotalLines) AS SourceTotalLines
    , MAX(BalancingLinesTable.BalancingLines) AS BalancingLines
    , SUM(STG.TotalLines) AS StagingTotalLines
    , (CAST(STG.Company AS int) * 1000000) + B.BatchID AS CompanyCode
FROM
    @batches B

INNER JOIN (
    SELECT 
        B.BatchID
        ,B.HeaderID
        ,COUNT(0) as BalancingLines 
    FROM 
    @batches B

-- this 'inner join table' BalancingLinesTable is what I added
    INNER JOIN
        Staging.TransactionLine T WITH (NOLOCK)
        ON B.HeaderID = T.HeaderID
    INNER JOIN
        Staging.HeaderLine H WITH (NOLOCK)
        ON T.HeaderID = H.HeaderID  
    WHERE H.BatchID = B.BatchID AND H.SourceSystemInstance = B.SourceSystem AND AccountNumber = 399990
    GROUP BY
         B.BatchID
        ,B.HeaderID
     ) BalancingLinesTable ON B.BatchID = BalancingLinesTable.BatchID

INNER JOIN (
    SELECT
         B.BatchID
        ,B.HeaderID
        ,COUNT(0) AS TotalLines
    FROM
        @batches B
    INNER JOIN
        Staging.TransactionLine T WITH (NOLOCK)
    ON
        B.HeaderID = T.HeaderID
    WHERE
        T.LineStatus = 'N'
    GROUP BY
         B.BatchID
        ,B.HeaderID ) SRC ON B.BatchID = SRC.BatchID

LEFT JOIN (
    SELECT
        B.BatchID
        ,B.HeaderID
        ,MT.Company
        ,COUNT(0) AS TotalLines
    FROM
        @batches B
    INNER JOIN
        [Production].[dbo].[Mutated_Transactions] MT WITH (NOLOCK)
    ON
        B.HeaderID = MT.HeaderID
    WHERE
        MT.RowHasError = 'N'
    GROUP BY
         B.BatchID
        ,B.HeaderID
        ,MT.Company) STG ON SRC.BatchID = STG.BatchID AND SRC.HeaderID = STG.HeaderID

GROUP BY
     B.BatchID
    ,B.TransformDate
    ,B.SourceSystem
    ,B.CompanyType
    ,STG.Company

ORDER BY
     B.TransformDate desc
    ,B.BatchID

结尾

4

1 回答 1

0

哇.. 将 accoutnumber = 98765 更改为 '98765' 后,我摆脱了错误。但是结果输出仍然是空的。看来我忘了在 SSRS 列中添加 [BalancingLines]。是的,我知道..

于 2013-05-21T12:13:39.990 回答