【问题标题】:How to make a nested select pull from only data available in the main select如何仅从主选择中可用的数据中进行嵌套选择
【发布时间】:2019-04-29 15:32:19
【问题描述】:

我正在 SSRS Report Builder 2016 中创建报告。

我有一组数据,其中包含一组代理机构以及每个月向每个代理机构收费的金额 (TotalBilled)。每个代理商都可以有多个 TotalBilled 值。我想找到每个代理商的 TotalBilled 中值。

我在查询中计算了中值,但它从所有机构中的所有 TotalBilled 中提取,而不仅仅是与我在参数中设置的机构相关的 TotalBilled。

这是我的数据集查询:

DECLARE @Cnt int = (SELECT COUNT(TotalBilled) FROM InvoiceLine)

select 
     a.AgencyID
     a.startdate,
     a.enddate,
     [TotalBilled],

     ((SELECT TOP 1 TotalBilled
     FROM   (
         SELECT  TOP (@Cnt/2) TotalBilled
         FROM    InvoiceLine I
         WHERE   TotalBilled is NOT NULL
         ORDER BY TotalBilled ASC
         ) AS I
     ORDER BY TotalBilled DESC) +
     (
     SELECT TOP 1 TotalBilled
     FROM   (
         SELECT  TOP (@Cnt/2) TotalBilled
         FROM    InvoiceLine I
         WHERE   TotalBilled is NOT NULL
         ORDER BY TotalBilled DESC
         ) AS I
     ORDER BY TotalBilled ASC)) / 2 AS MedianTotalBilled

from Agency a
left join invoiceLine invl
    on a.InvoiceID = invl.InvoiceID
where (a.agencyid in (@agency))
and (a.startdate >= @startdate and a.enddate <= @enddate)

什么是改变我的迈丹值计算的正确方法,使其受我的参数影响,而不仅仅是从整个数据库中提取?

编辑:

还将连接添加到中位数计算中并没有带来中位数的预期值。这是我目前正在尝试的:

(SELECT TOP 1 TotalBilled
     FROM   (
         SELECT  TOP (@Cnt/2) TotalBilled
         left join invoiceLine invl
              on a.InvoiceID = invl.InvoiceID
         where (a.agencyid in (@agency))
         and (a.startdate >= @startdate and a.enddate <= @enddate)
         and  TotalBilled is NOT NULL
         ORDER BY TotalBilled ASC
     ) AS I
 ORDER BY TotalBilled DESC)

【问题讨论】:

  • 在两个子选择中添加 a 和 a.InvoiceID = l.InvoiceID 不是您要找的吗?
  • 谢谢,我不确定如何将该连接正确添加到选择中。但它似乎应该工作。

标签: sql sql-server tsql reporting-services


【解决方案1】:

我相信您想将行限制添加到子选择中,如下所示

DECLARE @Cnt int = (SELECT COUNT(TotalBilled) FROM InvoiceLine)
    select 
         a.AgencyID
         a.startdate,
         a.enddate,
         [TotalBilled],

         ((SELECT TOP 1 TotalBilled
         FROM   (
             SELECT  TOP (@Cnt/2) TotalBilled
             FROM    InvoiceLine I
             WHERE   TotalBilled is NOT NULL
             and  a.InvoiceID = I.InvoiceID --new
             ORDER BY TotalBilled
             ) AS I
         ORDER BY TotalBilled DESC) +
         (
         SELECT TOP 1 TotalBilled
         FROM   (
             SELECT  TOP (@Cnt/2) TotalBilled
             FROM    InvoiceLine I
             WHERE   TotalBilled is NOT NULL
             and  a.InvoiceID = I.InvoiceID --new
             ORDER BY TotalBilled DESC
             ) AS I
         ORDER BY TotalBilled ASC)) / 2 AS MedianTotalBilled

    from Agency a
    left join invoiceLine invl
        on a.InvoiceID = invl.InvoiceID
    where (a.agencyid in (@agency))
    and (a.startdate >= @startdate and a.enddate <= @enddate)

编辑: 行数的选择器也需要限制

select 
     a.AgencyID
     a.startdate,
     a.enddate,
     [TotalBilled],



    ((SELECT TOP 1 TotalBilled
     FROM   (
         SELECT  TOP (
             (
                SELECT  count(*)
                FROM    InvoiceLine I
                WHERE   TotalBilled is NOT NULL
                and  a.InvoiceID = I.InvoiceID 
             )


             /2) TotalBilled
         FROM    InvoiceLine I
         WHERE   TotalBilled is NOT NULL
         and  a.InvoiceID = I.InvoiceID --new
         ORDER BY TotalBilled
         ) AS I
     ORDER BY TotalBilled DESC)

from Agency a
left join invoiceLine invl
    on a.InvoiceID = invl.InvoiceID
where (a.agencyid in (@agency))
and (a.startdate >= @startdate and a.enddate <= @enddate)

【讨论】:

  • 好的,这看起来不错。 TotalBilled 的计数是否正确,或者我需要更改如何计算总数?
  • DECLARE @Cnt int = (SELECT COUNT(TotalBilled) FROM InvoiceLine) 将需要更改为每行的子选择
  • 感谢您的帮助!它对我来说非常宝贵!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多