Oracle sql if条件或select语句中的group by

时间:2013-06-24 07:42:39

标签: sql oracle

我的表名是ProductDirection。它包括这些列。

  1. 操作代码
  2. messagecode
  3. productno
  4. messages_info
  5. 的StatusCode
  6. insertdate
  7. 我有一个批处理程序,这个程序最大化。一个产品一天3次。如果产品的操作成功批次,当天批次对该产品不起作用。所以程序在成功的同时工作并且插入记录失败或成功。

    我的记录是:

    ProductNo  StatusCode  MessagesInfo     InsertDate
    ---------------------------------------------------------   
    1236895     0          Fail For xxx     01.01.2013 10:00:00
    1236895     0          Fail For yyy     01.01.2013 15:00:00
    1236895     1          Success          01.01.2013 19:00:00
    1236895     0          Fail For xxx     15.01.2013 10:00:00
    1236895     0          Fail For yyy     15.01.2013 15:00:00
    1236895     0          Fail For zzz     15.01.2013 19:00:00
    

    我想要这个条件。如果它有可用的成功记录,我想只在一天内得到这个记录,如果所有记录都是一天的结果。所以我的记录看起来像这样。

    ProductNo  StatusCode MessagesInfo    InsertDate
    ------------------------------------------------------    
    1236895     1         Success         01.01.2013 19:00:00 
    1236895     0         Fail For xxx    15.01.2013 10:00:00
    1236895     0         Fail For yyy    15.01.2013 15:00:00
    1236895     0         Fail For zzz    15.01.2013 19:00:00
    

    我该如何解决?

    SELECT *
      FROM ProductDirection p
     WHERE p.operationcode = 6
       AND p.messagecode = 2
       AND trunc(p.insertdate) BETWEEN '01.01.2013' AND '21.06.2013';
    

2 个答案:

答案 0 :(得分:1)

试试这个。

编辑:

SELECT *
  FROM productdirection
 WHERE (productno, TRUNC (insertdate)) IN (
                                        SELECT   productno,
                                                 TRUNC (insertdate)
                                            FROM productdirection
                                        GROUP BY productno,
                                                 TRUNC (insertdate)
                                          HAVING MAX (statuscode) = 0)
    OR statuscode = 1;

sqlfiddle here

根据需要添加任何其他where子句。

答案 1 :(得分:0)

WITH ProductDirection AS
(
SELECT 1236895 ProductNo, 0 StatusCode, 'Fail For xxx' MessagesInfo, TO_DATE('01.01.2013 10:00:00', 'DD.MM.YYYY HH24:MI:SS') InsertDate FROM DUAL UNION ALL
SELECT 1236895, 0, 'Fail For yyy', TO_DATE('01.01.2013 15:00:00', 'DD.MM.YYYY HH24:MI:SS') FROM DUAL UNION ALL
SELECT 1236895, 1, 'Success',      TO_DATE('01.01.2013 19:00:00', 'DD.MM.YYYY HH24:MI:SS') FROM DUAL UNION ALL
SELECT 1236895, 0, 'Fail For xxx', TO_DATE('15.01.2013 10:00:00', 'DD.MM.YYYY HH24:MI:SS') FROM DUAL UNION ALL
SELECT 1236895, 0, 'Fail For yyy', TO_DATE('15.01.2013 15:00:00', 'DD.MM.YYYY HH24:MI:SS') FROM DUAL UNION ALL
SELECT 1236895, 0, 'Fail For zzz', TO_DATE('15.01.2013 19:00:00', 'DD.MM.YYYY HH24:MI:SS') FROM DUAL
)
SELECT *
   FROM ProductDirection
  WHERE insertdate >=   (
                            SELECT  MAX(InsertDate)
                            FROM    ProductDirection
                            WHERE   MessagesInfo = 'Success'
                            AND     InsertDate >= TO_DATE('01.01.2013', 'DD.MM.YYYY')
                        )
  ORDER BY insertdate;
/*
1236895 1   Success         2013-01-01 19:00:00
1236895 0   Fail For xxx    2013-01-15 10:00:00
1236895 0   Fail For yyy    2013-01-15 15:00:00
1236895 0   Fail For zzz    2013-01-15 19:00:00
*/