为特定列中的每个值返回单行

时间:2014-09-09 23:17:08

标签: sql oracle group-by distinct

我知道这可能是一个可以忽略的问题,但无论我在GROUP BY和/或DISTINCT周围使用什么逻辑,我似乎都无法实现这一点。我需要的是为每个CODE返回一行,它是上个月最近的CREATED_DATE。一个月内每个CODE可以有多个CREATED_DATE。正如我所说,它可能会使用DISTINCT或GROUPING,但我似乎无法使用任何东西。也许它只是早期= /

SELECT PRJ.CODE, 
                 PRJ.ID, 
                 RAGRPT.CREATED_DATE, 
                 RAGRPT.NAME,
                 RAGRPT.COP_REPORT_UPDATE OVERALL_HEALTH_COMMENT,
                  (CASE
                        WHEN RAGRPT.COP_SCHEDULE_STATUS + RAGRPT.COP_SCOPE_STATUS + RAGRPT.COP_COST_EFT_STATUS + RAGRPT.RMS_RESOURCE_STATUS BETWEEN 40 AND 50 THEN 'GREEN'
                        WHEN RAGRPT.COP_SCHEDULE_STATUS + RAGRPT.COP_SCOPE_STATUS + RAGRPT.COP_COST_EFT_STATUS + RAGRPT.RMS_RESOURCE_STATUS BETWEEN 60 AND 70 THEN 'YELLOW'
                        WHEN RAGRPT.COP_SCHEDULE_STATUS + RAGRPT.COP_SCOPE_STATUS + RAGRPT.COP_COST_EFT_STATUS + RAGRPT.RMS_RESOURCE_STATUS BETWEEN 80 AND 120 THEN 'RED'
                        ELSE ' '
                  END) OVERALL_STATUS,
                  RAGRPT.COP_SCHEDULE_EXP SCHEDULE_EXPLANATION,
                  (CASE 
                        WHEN RAGRPT.COP_SCHEDULE_STATUS = 10 THEN 'GREEN'
                        WHEN RAGRPT.COP_SCHEDULE_STATUS = 20 THEN 'YELLOW'
                        WHEN RAGRPT.COP_SCHEDULE_STATUS = 30 THEN 'RED'
                        ELSE ' '
                  END) SCHEDULE_STATUS,
                  RAGRPT.COP_SCOPE_EXP SCOPE_EXPLANATION,
                  (CASE 
                        WHEN RAGRPT.COP_SCOPE_STATUS = 10 THEN 'GREEN'
                        WHEN RAGRPT.COP_SCOPE_STATUS = 20 THEN 'YELLOW'
                        WHEN RAGRPT.COP_SCOPE_STATUS = 30 THEN 'RED'
                        ELSE ' '
                  END) SCOPE_STATUS,
                  RAGRPT.COP_EFFORT_EXP COST_EXPLANATION,
                  (CASE 
                        WHEN RAGRPT.COP_COST_EFT_STATUS = 10 THEN 'GREEN'
                        WHEN RAGRPT.COP_COST_EFT_STATUS = 20 THEN 'YELLOW'
                        WHEN RAGRPT.COP_COST_EFT_STATUS = 30 THEN 'RED'
                        ELSE ' '
                  END) COST_STATUS,
                  RAGRPT.RMS_RESOURCE_EXP RESOURCE_EXPLANATION,
                  (CASE 
                        WHEN RAGRPT.RMS_RESOURCE_STATUS = 10 THEN 'GREEN'
                        WHEN RAGRPT.RMS_RESOURCE_STATUS = 20 THEN 'YELLOW'
                        WHEN RAGRPT.RMS_RESOURCE_STATUS = 30 THEN 'RED'
                        ELSE ' '
                  END) RESOURCE_STATUS

    FROM  ODF_CA_COP_PRJ_STATUSRPT RAGRPT, INV_INVESTMENTS PRJ
    WHERE PRJ.ID = RAGRPT.ODF_PARENT_ID (+)
          AND RAGRPT.CREATED_DATE BETWEEN Trunc(SYSDATE, 'DD') - 28 AND SYSDATE
    ORDER BY PRJ.CODE, RAGRPT.CREATED_DATE DESC

2 个答案:

答案 0 :(得分:1)

您不想要group by。您想使用row_number()。以下是查询的结构:

SELECT *
FROM (SELECT <whatever>,
             ROW_NUMBER() OVER (PARTITION BY PRJ_CODE ORDER BY RAGRPT.CREATED_DATE DESC) as seqnum
      FROM  INV_INVESTMENTS PRJ LEFT JOIN
            ODF_CA_COP_PRJ_STATUSRPT RAGRPT, 
            ON PRJ.ID = RAGRPT.ODF_PARENT_ID
      WHERE RAGRPT.CREATED_DATE BETWEEN Trunc(SYSDATE, 'DD') - 28 AND SYSDATE
     ) t
WHERE seqnum = 1;

答案 1 :(得分:1)

我的示例将 GROUP BY

SELECT code, MAX(thedate) 
FROM Table1
WHERE thedate between add_months(trunc(sysdate,'mm'),-1) and
      last_day(add_months(trunc(sysdate,'mm'),-1))
GROUP BY code

SQL Fiddle

我将last month理解为previous calendar month并且无耻this answer借用了 Oracle代码。