0

我有以下 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 分组。

我怎样才能做到这一点?

4

2 回答 2

0

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

于 2012-11-30T06:59:24.437 回答
0

选择列表中的所有非聚合列都必须列在 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 查找,而不是在不同的查询中以手写方式(并且以各种方式)写出表。

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

于 2012-11-30T07:22:24.693 回答