Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql SSR中的平均值_Sql_Sql Server_Reporting Services_Ssrs 2012 - Fatal编程技术网

Sql SSR中的平均值

Sql SSR中的平均值,sql,sql-server,reporting-services,ssrs-2012,Sql,Sql Server,Reporting Services,Ssrs 2012,我的报告中的avg子查询有一些问题。我试图获得count(distinct(d.orderno))值和avg(count(distinct(d.orderno)))值,这样我就可以比较计数和平均值之间的百分比,但这根本不起作用。请看一下我的代码: SELECT d.packingoperator, d.packingunit, datepart(hh, d.datetimepacked) as hourPacked, avg(count(distinct(d.orderno))) as tar

我的报告中的avg子查询有一些问题。我试图获得
count(distinct(d.orderno))
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)

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

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)

你是不是在每个包含“sql”的标签上都加了标签?看到只有四个标签,我不理解你的担心。这个问题属于他们的范畴,我为什么不呢@uuerdomysql和sqlserver通常有非常不同的答案。MySQL没有CTE和MSSQL没有内置的组_CONCAT是两件立即浮现在脑海中的事情。出于某种原因,您的示例中的样式让我想起了MySql,但是
nolock
的存在表明了MSSQL。