SSRS的平均值

时间:2017-05-31 19:22:31

标签: sql sql-server reporting-services ssrs-2012

我在报告中遇到了一些avg子查询的问题。我正在尝试获取count(distinct(d.orderno)) AND avg(count(distinct(d.orderno)))值,以便我可以比较计数和平均值之间的百分比,但它根本就不是工作。请看一下我的代码:

SELECT

d.packingoperator,
d.packingunit,
datepart(hh, d.datetimepacked) as hourPacked,
avg(count(distinct(d.orderno))) as targetrate,
count(distinct(d.orderno)) as orderspacked,
    (select count(distinct(d1.orderno)) 
    from mck_hvs.oldorderdetails d1 with (nolock)
        where d1.refrigerate != 'N'
        and convert(date, d1.datetimepacked) = convert(date, @date)
        and d1.packingoperator = d.packingoperator
    and datepart(hh, d1.datetimepacked) = datepart(hh,d.datetimepacked)) as coldcount

FROM

mck_hvs.oldorderdetails d with (nolock)

WHERE

convert(date, d.datetimepacked) = convert(date, @date)

GROUP BY

d.packingoperator, 
datepart(hh, d.datetimepacked),
d.packingunit

ORDER BY

d.packingoperator, 
datepart(hh, d.datetimepacked)

我也尝试过这个选项:

SELECT

d.packingoperator,
d.packingunit,
datepart(hh, d.datetimepacked) as hourPacked,
count(distinct(d.orderno)) as orderspacked,
    (select count(distinct(d1.orderno)) 
    from mck_hvs.oldorderdetails d1 with (nolock)
        where d1.refrigerate != 'N'
        and convert(date, d1.datetimepacked) = convert(date, @date)
        and d1.packingoperator = d.packingoperator
    and datepart(hh, d1.datetimepacked) = datepart(hh,d.datetimepacked)) as coldcount,
        (select avg(target) from (
        select count(distinct(d2.orderno)) as target
        from mck_hvs.oldorderdetails d2 with( nolock )
        where convert(date, d2.datetimepacked) = convert(date, @date)
        and d2.packingoperator = d.packingoperator
        and datepart(hh, d2.datetimepacked) = datepart(hh, d.datetimepacked)) as targetrate


FROM

mck_hvs.oldorderdetails d with (nolock)

WHERE

convert(date, d.datetimepacked) = convert(date, @date)

GROUP BY

d.packingoperator, 
datepart(hh, d.datetimepacked),
d.packingunit

ORDER BY

d.packingoperator, 
datepart(hh, d.datetimepacked)

1 个答案:

答案 0 :(得分:0)

您是否考虑过将某些子聚合分解为交叉应用?请看下面。这是非常不优雅的代码,但它可能能让你走上正确的轨道。

SELECT
    d.packingoperator,
    d.packingunit,
    DATEPART(hh, d.datetimepacked) as hourPacked,
    AVG(ct.orderNoCt) as targetrate,
    COUNT(dt.orderNoDt) as orderspacked,
        (
        SELECT 
            COUNT(DISTINCT(d1.orderno)) 
        FROM mck_hvs.oldorderdetails d1 with (NOLOCK)
            where d1.refrigerate != 'N'
            and CONVERT(date, d1.datetimepacked) = CONVERT(date, @date)
            and d1.packingoperator = d.packingoperator
        and DATEPART(hh, d1.datetimepacked) = DATEPART(hh,d.datetimepacked)
        ) as coldcount
FROM
mck_hvs.oldorderdetails d with (nolock)
CROSS APPLY
  (
    SELECT
         COUNT(DISTINCT d2.orderNo) as orderNoCt
    FROM mck_hvs.oldorderdetails d2 with (nolock)
    WHERE 
         d2.packingoperator = d.packingoperator
     AND d2.packingunit = d.packingunit
     and DATEPART(hh, d2.datetimepacked) = DATEPART(hh, d.datetimepacked)
  ) ct
CROSS APPLY
  (
    SELECT
        DISTINCT d3.orderNo as orderNoDt
    FROM mck_hvs.oldorderdetails d3 with (nolock)
    WHERE 
         d3.packingoperator = d.packingoperator
     AND d3.packingunit = d.packingunit
     and DATEPART(hh, d3.datetimepacked) = DATEPART(hh, d.datetimepacked)
  ) dt

WHERE
    CONVERT(DATE, d.datetimepacked) = CONVERT(DATE, @date)
GROUP BY
    d.packingoperator
   ,DATEPART(hh, d.datetimepacked)
   ,d.packingunit
ORDER BY
     d.packingoperator 
    ,DATEPART(hh, d.datetimepacked)