2017-08-15 83 views
0

我真的遇到了我正在使用的这个SQL代码的问题。我一直在抱怨,但并没有真正获得答案。我试图将this code改编成我正在尝试做的事情,但我不断收到下面抓取的错误消息屏幕截图。使用SQL过滤来自查询的数据

SELECT LoginID, ShiftNumber, PalletQTY, Group, ShiftDate 
FROM Database 
COUNT(PalletQTY) AS TotalCount 
SUM(IF(Group='PUT',1,0)) AS ActiveCount, 
ROUND((SUM(IF(TALLY_TRAN_MSTR.PRI_GRP_CD='PUT',1,0))*100/COUNT(TALLY_TRAN_MSTR.FULL_PLLT_QTY)),2) AS PctActive 
FROM WBR_RW.TALLY_TRAN_MSTR TALLY_TRAN_MSTR 

enter image description here

现在,有很多日期,并利用每个日期的许多名字,所以我想要的代码识别。我不确定我试图修改的代码是否能够做到这一点。

对于如何使这段代码工作或找到可能有用的代码,我在这一点上非常努力。任何帮助,将不胜感激。让我知道是否需要额外的信息。

编辑 enter image description here

我想谁看法是花他的时间超过75%做一个人的数据“PUT”的具体日期。

的什么,我试图做的是(人工计算)

麦克米兰有他的名字2017年6月15日相关的3项的一个例子。其中1个是“PUT”。该日期的“PUT”项目总数为132.与该日期相关的所有内容总和为167.72/167 = 0.79。 0.79> 0.75,所以我想让他的数据显示在我的查询结果中。 马龙在6/15/2017有4个与他的名字相关的项目。其中1个是“PUT”。 “PUT”总和为4.所有4项的总和为36. 4/36 = 0.11 0.11 < 0.75,所以我想摆脱这些数据。

+1

Oracle数据库产生的Oracle错误。我添加了相应的标签。 –

+0

如果您将表格结构,样本数据以及该数据的预期结果显示为格式文本,而不仅仅是模糊描述,这将有所帮助。您在选择列表和分组列表中加入了“PalletQTY”,但也包含了这一点,这并没有什么意义。如果'Group'和'PRI_GRP_CD'确实是同一列,那么它就不是很明显;也许与“PalletQTY”和“FULL_PLLT_QTY”一样。 –

+0

这不是格式化文本。请参阅[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)。 –

回答

2

Oracle不支持if()。使用适当的Oracle语法。我想查询应该看起来更像是这样的:

SELECT LoginID, ShiftNumber, PalletQTY, "Group", ShiftDate, 
     COUNT(PalletQTY) AS TotalCount, 
     SUM(CASE WHEN "Group" = 'PUT' THEN 1 ELSE 0 END) AS ActiveCount, 
     ROUND(SUM(CASE WHEN TALLY_TRAN_MSTR.PRI_GRP_CD = 'PUT' THEN 1 ELSE 0 END)*100/
      COUNT(TALLY_TRAN_MSTR.FULL_PLLT_QTY), 2) AS PctActive 
FROM WBR_RW.TALLY_TRAN_MSTR TALLY_TRAN_MSTR 
GROUP BY LoginID, ShiftNumber, PalletQTY, "Group", ShiftDate; 
+0

我刚刚尝试过,我收到了错误消息,逐字“ORA-00923:FROM关键字找不到预期的地方” – Lampoa

+1

@Lampoa - 这是因为在'ShiftDate'和'TotalCount'后面的列表达式之间缺少逗号,在你的原创中也失踪了。 (不知道'从数据库'在那里......)。尝试编辑的代码。 –

+0

@AlexPoole嗨,谢谢!它工作,但它只是没有做我想做的事情......在这里与其他人交谈,似乎我正在寻找的事情很简单,我只是几乎没有SQL经验。 – Lampoa

0

除了基本的语法问题戈登指出,你似乎混淆了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 

我已经采取palletqtygroup出选择列表中的,因为如果他们在那里,他们有将被包含在分组条款中,这将不会在正确的级别进行计数和求和。

这应该会给你一行输出每个人每天/他们有记录的移动。

如果你要排除的结果行,其中的比例不符合某些阈值,那么你可以添加一个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;