Sql 如何仅从主选择中可用的数据进行嵌套选择拉取
我正在SSRS report Builder 2016中创建报告 我有一组数据,其中包含一组机构以及每个机构每月的账单金额。对于TotalBilled,每个代理都可以有多个值。我想找到每个机构的总账单价值中位数 我在查询中计算中值,但它从所有代理中的所有TotalBilled中提取,而不仅仅是我在参数中设置的与代理相关的TotalBilled 以下是我的数据集查询:Sql 如何仅从主选择中可用的数据进行嵌套选择拉取,sql,sql-server,tsql,reporting-services,Sql,Sql Server,Tsql,Reporting Services,我正在SSRS report Builder 2016中创建报告 我有一组数据,其中包含一组机构以及每个机构每月的账单金额。对于TotalBilled,每个代理都可以有多个值。我想找到每个机构的总账单价值中位数 我在查询中计算中值,但它从所有代理中的所有TotalBilled中提取,而不仅仅是我在参数中设置的与代理相关的TotalBilled 以下是我的数据集查询: DECLARE @Cnt int = (SELECT COUNT(TotalBilled) FROM InvoiceLine)
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)
更改meidan值计算的正确方法是什么,使其受参数的影响,而不仅仅是从整个数据库中提取
编辑:
此外,将连接添加到中值计算中也不会产生中值的预期值。以下是我目前正在尝试的:
(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)
我相信您希望将行限制添加到子选择中,如下所示
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)
在两个子选项中添加a和a.InvoiceID=l.InvoiceID不是您想要的吗?谢谢,我不确定如何正确地将该连接添加到选项中。但它看起来确实应该起作用。好的,这看起来不错。TotalBilled的计数是否正确,或者我需要更改如何计算此总数?声明@Cnt int=SELECT COUNTTotalBilled FROM InvoiceLine需要更改为每行的子选择谢谢您的帮助!这对我来说是无价的!