如何将单独列组中的两列与行组总数进行比较以获得百分比?行在[trx_dt]上分组。直接和间接列在[labor_type]上分组并过滤以分别显示直接和间接人工。目标是每天总计以及百分比和实际值表示的总体总数。
这就是报告现在没有百分比的情况
编辑:2015年7月13日:我可以使用
获取总行数的百分比=Sum(Fields!lbr_hrs.Value)/Sum(Fields!lbr_hrs.Value, "DataSet1")
有没有办法隔离列组的总值以与行组的总值进行比较?
编辑7/16/15回应第1条评论: 在第一行中,我想要4.83和3.17作为8的百分比。这将给我每日百分比。
以下是查询:
SELECT 'P' AS RecordType,CASE WHEN mctrxhst_sql.wc != 'INDIRECT' then 'DIRECT' else 'INDIRECT' END AS labor_type, mctrxhst_sql.trx_dt,mctrxhst_sql.ord_no,
mctrxhst_sql.oper_no, mctrxhst_sql.desc_1, mctrxhst_sql.lbr_hrs, mccrwhst_sql.emp_no, mccrwhst_sql.lbr_grd, sfordfil_sql.item_no
FROM mctrxhst_sql
LEFT OUTER JOIN sfordfil_sql ON sfordfil_sql.ord_no=mctrxhst_sql.ord_no
LEFT OUTER JOIN hzctrfil_sql ON hzctrfil_sql.dept=mctrxhst_sql.dept AND hzctrfil_sql.wc=mctrxhst_sql.wc
LEFT OUTER JOIN hzcdefil_sql ON hzcdefil_sql.hz_cd=mctrxhst_sql.dept AND hzcdefil_sql.cd_type='DP'
LEFT OUTER JOIN mccrwhst_sql ON mccrwhst_sql.job_no=mctrxhst_sql.job_no AND mccrwhst_sql.ord_no=mctrxhst_sql.ord_no AND mccrwhst_sql.path_no=mctrxhst_sql.path_no
AND mccrwhst_sql.oper_no=mctrxhst_sql.oper_no AND mccrwhst_sql.oper_seq_no=mctrxhst_sql.oper_seq_no AND mccrwhst_sql.pln_act_cd=mctrxhst_sql.pln_act_cd
AND mccrwhst_sql.trx_dt=mctrxhst_sql.trx_dt AND mccrwhst_sql.control_no = mctrxhst_sql.control_no
CROSS JOIN compfile_sql
WHERE mctrxhst_sql.pln_act_cd = '1' AND mctrxhst_sql.rec_type='O'
AND mctrxhst_sql.trx_dt BETWEEN @from AND @to
AND mctrxhst_sql.dept = 'MFG'
AND mccrwhst_sql.emp_no IN (@Employee)
UNION
SELECT 'U' AS RecordType, CASE WHEN mctrxfil_sql.wc != 'INDIRECT' then 'DIRECT' else 'INDIRECT' END AS labor_type, mctrxfil_sql.trx_dt, mctrxfil_sql.ord_no, mctrxfil_sql.oper_no, mctrxfil_sql.desc_1,mctrxfil_sql.lbr_hrs, mccrwfil_sql.emp_no, mccrwfil_sql.lbr_grd, sfordfil_sql.item_no
FROM mctrxfil_sql
LEFT OUTER JOIN sfordfil_sql ON sfordfil_sql.ord_no=mctrxfil_sql.ord_no
LEFT OUTER JOIN hzctrfil_sql ON hzctrfil_sql.dept=mctrxfil_sql.dept AND hzctrfil_sql.wc=mctrxfil_sql.wc
LEFT OUTER JOIN hzcdefil_sql ON hzcdefil_sql.hz_cd=mctrxfil_sql.dept AND hzcdefil_sql.cd_type='DP'
LEFT OUTER JOIN mccrwfil_sql ON mccrwfil_sql.job_no=mctrxfil_sql.job_no AND mccrwfil_sql.ord_no=mctrxfil_sql.ord_no AND mccrwfil_sql.path_no=mctrxfil_sql.path_no
AND mccrwfil_sql.oper_no=mctrxfil_sql.oper_no AND mccrwfil_sql.oper_seq_no=mctrxfil_sql.oper_seq_no AND mccrwfil_sql.pln_act_cd=mctrxfil_sql.pln_act_cd
AND mccrwfil_sql.trx_dt=mctrxfil_sql.trx_dt AND mccrwfil_sql.control_no = mctrxfil_sql.control_no
CROSS JOIN compfile_sql
WHERE mctrxfil_sql.pln_act_cd = '1' AND mctrxfil_sql.rec_type = 'O'
AND mctrxfil_sql.trx_dt BETWEEN @from AND @to
AND mctrxfil_sql.dept = 'MFG'
AND mccrwfil_sql.emp_no IN (@Employee)
答案 0 :(得分:1)
我已经在这里找到了SQL解决方案,因为我不知道你所拥有的所有版本的版本是否很难给出一个有效的答案。我的基础是SQL2008(不是R2)所以它可以在那个或更高的上面工作。
这种方法意味着报告必须被撤销,但它应该非常直接,因为大部分工作都是在SQL端完成的。
在这个例子中,我刚刚构建了一个临时表(#t),在其中转储了一些值,然后对其运行查询以获得结果。执行计算的主要部分是两个SELECT UNION'd Together一部分按员工/日期计算数据,下一部分执行员工总计。
如果你决定实现这个,那么你可以用你的查询替换对 dbo。#t 的引用,但是我个人会把你的查询结果和转储正如我在示例中所做的那样将它们放入临时表中,它只是让它更清晰(恕我直言)。
首先,检查此SQL Fiddle的输出,并确保它按预期工作 http://sqlfiddle.com/#!3/9eecb7/2070/0
假设它看起来不错(忽略日期格式,我在英国)然后执行以下操作,请原谅我,如果这有点愚蠢,但我不知道你的经验水平。
在你的报告中,通过将整个事物包装在SELECT .. INTO中然后立即添加执行计算的位来更改查询,就像这样......
SELECT o.* INTO #t
FROM
(
**your entire original query here**
) as o
SELECT DISTINCT -- this part does the emp/date totals
t.emp_no, t.trx_dt
, SUM(case t.labor_type WHEN 'INDIRECT' THEN t.lbr_hrs ELSE 0 END) over(PARTITION BY t.emp_no, t.trx_dt) AS InDirect -- sums indirect hours
, SUM(case t.labor_type WHEN 'DIRECT' THEN t.lbr_hrs ELSE 0 END) over(PARTITION BY t.emp_no, t.trx_dt) AS Direct -- sums direct hours
, SUM(case t.labor_type WHEN 'INDIRECT' THEN t.lbr_hrs ELSE 0 END) over(PARTITION BY t.emp_no, t.trx_dt) / SUM(t.lbr_hrs) over(PARTITION BY t.emp_no, t.trx_dt) AS InDirectShare -- sums indirect hours then divides by total
, SUM(case t.labor_type WHEN 'DIRECT' THEN t.lbr_hrs ELSE 0 END) over(PARTITION BY t.emp_no, t.trx_dt) / SUM(t.lbr_hrs) over(PARTITION BY t.emp_no, t.trx_dt) AS DirectShare -- sums direct hours then divides by total
FROM dbo.#t t
UNION ALL
SELECT DISTINCT -- this section is as above but with the date partitioning removed
t.emp_no, NULL
, SUM(case t.labor_type WHEN 'INDIRECT' THEN t.lbr_hrs ELSE 0 END) over(PARTITION BY t.emp_no) AS InDirect
, SUM(case t.labor_type WHEN 'DIRECT' THEN t.lbr_hrs ELSE 0 END) over(PARTITION BY t.emp_no) AS Direct
, SUM(case t.labor_type WHEN 'INDIRECT' THEN t.lbr_hrs ELSE 0 END) over(PARTITION BY t.emp_no) / SUM(t.lbr_hrs) over(PARTITION BY t.emp_no) AS InDirectShare
, SUM(case t.labor_type WHEN 'DIRECT' THEN t.lbr_hrs ELSE 0 END) over(PARTITION BY t.emp_no) / SUM(t.lbr_hrs) over(PARTITION BY t.emp_no) AS DirectShare
FROM dbo.#t t
您可能希望在最后一位添加排序列,以帮助您对报告中的结果进行排序。我没有在此示例中包含该内容,但可能只是第一部分中的0 AS SortOrder
和第二部分中的1 as SortOrder
。然后,您可以按SortOrder对日期
希望这有帮助,如果有任何不清楚的地方让我知道,我会尽力解释......