表别名/查询格式 - 返回错误计数

时间:2014-06-17 15:01:20

标签: sql aliases

我需要进行以下查询并将其转换为使用同步存档数据库中的行。 (一切都相同,只是为超过一定年龄的行的表名添加arc_前缀)

我已经尝试了别名(可能不正确 - 但是这么认为),确保我不会模糊地使用共享相同字段等的表中的列...但是,它返回了“一半”计数比预期的要高。因此,我假设它没有正确地键入表并多次计数。

我非常感谢关于如何使用适当的别名来返回准确和正确的计数的一些简单帮助/指导。

我非常感谢您提前提供任何帮助。谢谢。

注意:我为以下查询的格式化道歉。不知道为什么会这样做。

SELECT
   p_chep_customers.receiver_id,
   to_char(dispatch_dte,'YYYYMMDD') dispatch_dte,
   ship_id,
   car_move_id,
   sum(half) halfs,
   sum(full) fulls,
   sum(quarter) quarters,
   (SUM(half) + SUM(full) + SUM(quarter)) as "Total Pallets",
   adrnam,
   adrln1,
   adrcty,
   adrstc,
   adrpsz
FROM
   p_chep_customers,
   (
    SELECT
          dispatch_dte,
          car_move_id,
          ship_id,
          adrnam,
          adrln1,
          adrcty,
          adrstc,
          adrpsz,
          decode(max_ftpcod,'1-2-2',sum_untqty,0) half,
          decode(max_ftpcod,'1-4-4',sum_untqty,0) quarter,
          count(lodnum) full
     FROM
     (
     SELECT
            invlod.lodnum,
            trlr.dispatch_dte,
            car_move.car_move_id,
            shipment.ship_id,
            adrmst.adrnam,
            adrmst.adrln1,
            adrmst.adrcty,
            adrmst.adrstc,
            adrmst.adrpsz,
            max(invdtl.ftpcod) max_ftpcod,
            count(invdtl.dtlnum) dtlnum_count,
            sum(invdtl.untqty) sum_untqty
      FROM
            trlr,
            car_move,
            stop,
            shipment,
            shipment_line,
            ord_line,
            ord,
            invlod,
            invsub,
            invdtl,
            p_peco_items,
            adrmst
      WHERE
            trlr.trlr_id = car_move.trlr_id
            and car_move.car_move_id = stop.car_move_id
            and stop.stop_id = shipment.stop_id
            and shipment.ship_id = shipment_line.ship_id
            and shipment.wh_id = shipment_line.wh_id
            and shipment_line.ordnum = ord_line.ordnum
            and shipment_line.ordlin = ord_line.ordlin
            and shipment_line.ordsln = ord_line.ordsln
            and shipment_line.wh_id = ord_line.wh_id
            and shipment_line.client_id = ord_line.client_id
            and ord_line.ordnum = ord.ordnum
            and ord_line.wh_id = ord.wh_id
            and ord_line.client_id = ord.client_id
            and shipment.rt_adr_id = adrmst.adr_id (+)
            and shipment.host_client_id = adrmst.client_id (+)
            and shipment_line.ship_line_id = invdtl.ship_line_id
            and invdtl.subnum = invsub.subnum
            and invsub.lodnum = invlod.lodnum
            and invdtl.prtnum = p_peco_items.prtnum (+)
            and p_peco_items.prtnum is null
            and trlr.dispatch_dte is not null
            and to_char(trlr.dispatch_dte,'YYYY-MM-DD') between '2014-05-05' and '2014-06-06'
            and adrmst.ctry_name = 'CAN'
       GROUP BY
            invlod.lodnum,
            trlr.dispatch_dte,
            car_move.car_move_id,
            shipment.ship_id,
            adrmst.adrnam,
            adrmst.adrln1,
            adrmst.adrcty,
            adrmst.adrstc,
            adrmst.adrpsz
       )
    GROUP BY
       dispatch_dte,
       ship_id,
       adrnam,
       adrln1,
       adrcty,
       adrstc,
       adrpsz,
       car_move_id,
       max_ftpcod,
       sum_untqty,
       lodnum
  )
WHERE
 substr(adrln1,1,20) = substr(p_chep_customers.receiver_address (+),1,20)
GROUP BY
 p_chep_customers.receiver_id,
 dispatch_dte,
 ship_id,
 car_move_id,
 adrnam,
 adrln1,
 adrcty,
 adrstc,
 adrpsz
ORDER BY
 2,1,3,4,5 

0 个答案:

没有答案