选择不包括在列表中的字段

时间:2012-11-30 06:30:43

标签: database oracle group-by

我有以下oracle查询:

SELECT distinct  DM.name,
    ND.param5 ,
    ND.param4 ,  
      DECODE(ND.param3, 'TRS01','Mark for Return to warehouse',
'TRS02','Waiting for distributor approval on return',
'TRS03','Waiting for Service Provider approval on return',
'TRS04','Return to Distributor',
'TRS05','Return to Warehouse',
'TRS06','Mark for Return to Distributor',
'TRS08','Quarantined',
'TRS10' ,'New',
'TRS11','Waiting for Distributor acceptance',
'TRS12' ,'Moved to distributor warehouse',
'TRS13','Waiting for reseller acceptance',
'TRS14','Moved to reseller warehouse',
'TRS15','In-Use', 
'TRS16','Returned',
'TRS17','Deattached',
'TRS18','Sold'),   NS.name ,
    COUNT( distinct ND.NUMBERDETAILID) serialcount,ND.param3,'ALL',
   FROM ndetail ND,
    nbatch NB,
    nstatus NS,
    dynamicmaster DM,(select his.createdate,h.numberdetailid,his.numberstatusid,his.param3,his.actualnumber 
from ndetailhistory his,
(select max(createdate) createdate,numberdetailid 
from ndetailhistory 
group by numberdetailid) h 
where h.numberdetailid = his.numberdetailid
and h.createdate = his.createdate) NDH
  WHERE ND.numberbatchid = NB.numberbatchid
  AND NS.numberstatusid  = ND.numberstatusid
  and DM.DYNAMICMASTERID = NB.WAREHOUSEID
  and NDH.actualnumber = ND.actualnumber
  and ND.NUMBERDETAILID = NDH.NUMBERDETAILID
  and NB.numbergroupid = (select numbergroupid from ngroup where alias = 'TEST')
  AND (case when 'ALL'='ALL' THEN '1' ELSE  ND.PARAM3 END ) =(case when 'ALL'='ALL' THEN '1' ELSE  'ALL' END )
  GROUP BY DM.name,ND.param5,ND.param4,ND.param3,NS.name
order by 1,3,2

我想在选择列表中创建,但不想与createdate分组。

我怎样才能实现这个目标?

2 个答案:

答案 0 :(得分:0)

使用GROUP BY时,SELECT列表中的字段必须位于GROUP BY语句中或聚合函数中。因此,如果您不想将其包含在GROUP BY中,只需将 MAX(createdate)添加到SELECT列表中。

答案 1 :(得分:0)

select-list中的所有非聚合列必须列在GROUP BY子句中。主选择列表中的DECODE(还)没有别名,这使得它过分困难。

SELECT DISTINCT DM.name,
       ND.param5,
       ND.param4,  
       DECODE(ND.param3, 'TRS01','Mark for Return to warehouse',
                         'TRS02','Waiting for distributor approval on return',
                         'TRS03','Waiting for Service Provider approval on return',
                         'TRS04','Return to Distributor',
                         'TRS05','Return to Warehouse',
                         'TRS06','Mark for Return to Distributor',
                         'TRS08','Quarantined',
                         'TRS10' ,'New',
                         'TRS11','Waiting for Distributor acceptance',
                         'TRS12' ,'Moved to distributor warehouse',
                         'TRS13','Waiting for reseller acceptance',
                         'TRS14','Moved to reseller warehouse',
                         'TRS15','In-Use', 
                         'TRS16','Returned',
                         'TRS17','Deattached',
                         'TRS18','Sold')
       decoded_param3,
       NS.name,
       COUNT(DISTINCT ND.NUMBERDETAILID) serialcount,
       ND.param3,
       'ALL' all_code
  FROM ndetail ND,
       nbatch NB,
       nstatus NS,
       dynamicmaster DM,
       (SELECT his.createdate, h.numberdetailid, his.numberstatusid, his.param3,
               his.actualnumber 
          FROM ndetailhistory his,
               (SELECT MAX(createdate) createdate, numberdetailid 
                  FROM ndetailhistory 
                 GROUP BY numberdetailid) h 
         WHERE h.numberdetailid = his.numberdetailid
           AND h.createdate = his.createdate) NDH
 WHERE ND.numberbatchid   = NB.numberbatchid
   AND NS.numberstatusid  = ND.numberstatusid
   AND DM.DYNAMICMASTERID = NB.WAREHOUSEID
   AND NDH.actualnumber   = ND.actualnumber
   AND ND.NUMBERDETAILID  = NDH.NUMBERDETAILID
   AND NB.numbergroupid  = (SELECT numbergroupid FROM ngroup WHERE alias = 'TEST')
   AND (CASE WHEN 'ALL'='ALL' THEN '1' ELSE ND.PARAM3 END) =
       (CASE WHEN 'ALL'='ALL' THEN '1' ELSE 'ALL'     END)
 GROUP BY DM.name, ND.param5, ND.param4, decoded_param3, NS.name, ND.param3, all_code
 ORDER BY 1,3,2

您应该了解这些称为“表格”的神奇事物,它们允许您存储DECODE操作的信息,例如数据库中的ND.param3查找,而不是在不同的查询中将表格纵向(和不同地)写出来。 / p>

您还应该了解显式JOIN查询并使用它们。