除了基本的语法问题戈登指出,你似乎混淆了count()
和sum()
聚合函数。您可以同时执行这两个操作,但从描述中您可以看到托盘的总数,而不是组类型的条目数。
你似乎想要更多的东西像这样,通过使用基于你的原始描述伪数据:
-- CTE for dummy data
with tally_tran_mstr (loginid, shiftdate, shiftnumber, pri_grp_cd, palletqty) as
(
select 'Steve', date '2017-06-15', 1, 'PUT', 40 from dual
union all select 'Steve', date '2017-06-15', 1, 'PUT', 80 from dual
union all select 'Steve', date '2017-06-15', 1, 'PUT', 45 from dual
union all select 'Steve', date '2017-06-15', 1, '???', 7 from dual
union all select 'Steve', date '2017-06-15', 1, '???', 5 from dual
union all select 'Steve', date '2017-06-15', 1, '???', 3 from dual
union all select 'Steve', date '2017-06-15', 1, '???', 1 from dual
)
-- end of CTE for dummy data
select loginid, shiftnumber, shiftdate,
count(*) as totalcount,
count(case when pri_grp_cd = 'PUT' then 1 end) as activecount,
round(100 * count(case when pri_grp_cd = 'PUT' then 1 end)/count(*), 2)
as pctactivecount,
sum(palletqty) as totalqty,
sum(case when pri_grp_cd = 'PUT' then palletqty else 0 end) as activeqty,
round(100 * sum(case when pri_grp_cd = 'PUT' then palletqty else 0 end)/
sum(palletqty), 2) as pctactiveqty
from tally_tran_mstr
group by loginid, shiftnumber, shiftdate;
LOGIN SHIFTNUMBER SHIFTDATE TOTALCOUNT ACTIVECOUNT PCTACTIVECOUNT TOTALQTY ACTIVEQTY PCTACTIVEQTY
----- ----------- ---------- ---------- ----------- -------------- ---------- ---------- ------------
Steve 1 2017-06-15 7 3 42.86 181 165 91.16
我已经采取palletqty
和group
出选择列表中的,因为如果他们在那里,他们有将被包含在分组条款中,这将不会在正确的级别进行计数和求和。
这应该会给你一行输出每个人每天/他们有记录的移动。
如果你要排除的结果行,其中的比例不符合某些阈值,那么你可以添加一个having
条款:
...
group by loginid, shiftnumber, shiftdate
having sum(case when pri_grp_cd = 'PUT' then palletqty else 0 end)/
sum(palletqty) > 0.8;
您可能希望避免重蹈一些条款;可以计数/总和/组移动到一个子查询(内联视图),然后筛选用where
子句代替having
子句:
select loginid, shiftnumber, shiftdate, totalcount, activecount,
round(100 * activecount/totalcount, 2) as pctactivecount,
totalqty, activeqty, round(100 * activeqty/totalqty, 2) as pctactiveqty
from (
select loginid, shiftnumber, shiftdate,
count(*) as totalcount,
count(case when pri_grp_cd = 'PUT' then 1 end) as activecount,
sum(palletqty) as totalqty,
sum(case when pri_grp_cd = 'PUT' then palletqty else 0 end) as activeqty
from tally_tran_mstr
group by loginid, shiftnumber, shiftdate
)
where activeqty/totalqty > 0.8;
Oracle数据库产生的Oracle错误。我添加了相应的标签。 –
如果您将表格结构,样本数据以及该数据的预期结果显示为格式文本,而不仅仅是模糊描述,这将有所帮助。您在选择列表和分组列表中加入了“PalletQTY”,但也包含了这一点,这并没有什么意义。如果'Group'和'PRI_GRP_CD'确实是同一列,那么它就不是很明显;也许与“PalletQTY”和“FULL_PLLT_QTY”一样。 –
这不是格式化文本。请参阅[this](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557# 285557),[this](https://stackoverflow.com/help/mcve)和[this](https://stackoverflow.com/help/formatting)。 –