SQL - 组合两个选择语句

时间:2014-10-26 20:31:18

标签: sql

网站有点新,我还是SQL新手,我正在尝试在服务器命令操作中为RedPrairie(仓库管理系统)编写查询。我发现我有权访问,编写查询,其中一个自定义DDA效率低,因为它结合了分阶段处理单元和分阶段权重,但它并没有分离已计划用于出站和未出口的内容。

出于这个原因,我试图提出一个查询,并在组合查询时遇到很多麻烦。我的查询将使用相同的表,但我希望它将计划内容与未计划内容的列分开。我试图做到以下几点:

[SELECT addr.host_ext_id destination,
    (SELECT COUNT(DISTINCT iv.lodnum)
       FROM inventory_view iv,
            locmst lm,
            aremst am,
            shipment_line sl,
            shipment sh,
            adrmst addr,
            uc_invlod uil,
            uc_load_grp ulg
      WHERE lm.wh_id = 'MWCCD'
                AND iv.lodnum=uil.lodnum
                AND am.arecod = lm.arecod
                AND am.wh_id = lm.wh_id
                AND lm.stoloc = iv.stoloc
                AND lm.wh_id = iv.wh_id
                AND addr.host_ext_id = ulg.dest_code
                AND ulg.client_id IS NOT NULL
                AND addr.adr_id = sh.rt_adr_id
                AND sl.ship_line_id = iv.ship_line_id
                AND sh.ship_id = sl.ship_id
                AND sh.stop_id IS NULL
                AND am.fwiflg = 1
                AND am.stgflg = 1) AS Unplanned_Hu,
    (SELECT SUM(DISTINCT uil.weight_kg) 
       FROM inventory_view iv,
            locmst lm,
            aremst am,
            shipment_line sl,
            shipment sh,
            adrmst addr,
            uc_invlod uil,
            uc_load_grp ulg
      WHERE lm.wh_id = 'MWCCD'
            AND iv.lodnum=uil.lodnum
            AND am.arecod = lm.arecod
            AND am.wh_id = lm.wh_id
            AND lm.stoloc = iv.stoloc
            AND lm.wh_id = iv.wh_id
            AND addr.host_ext_id = ulg.dest_code
            AND ulg.client_id IS NOT NULL
            AND addr.adr_id = sh.rt_adr_id
            AND sl.ship_line_id = iv.ship_line_id
            AND sh.ship_id = sl.ship_id
            AND sh.stop_id IS NULL
            AND am.fwiflg = 1
            AND am.stgflg = 1) AS Unplanned_Weight_KG, 
(SELECT COUNT(DISTINCT iv.lodnum)
       FROM inventory_view iv,
            locmst lm,
            aremst am,
            shipment_line sl,
            shipment sh,
            adrmst addr,
            uc_invlod uil,
            uc_load_grp ulg
       WHERE lm.wh_id = 'MWCCD'
                    AND iv.lodnum=uil.lodnum
                    AND am.arecod = lm.arecod
                    AND am.wh_id = lm.wh_id
                    AND lm.stoloc = iv.stoloc
                    AND lm.wh_id = iv.wh_id
                    AND addr.host_ext_id = ulg.dest_code
                    AND ulg.client_id IS NOT NULL
                    AND addr.adr_id = sh.rt_adr_id
                    AND sl.ship_line_id = iv.ship_line_id
                    AND sh.ship_id = sl.ship_id
                    AND sh.stop_id IS NOT NULL
                    AND am.fwiflg = 1
                    AND am.stgflg = 1) AS Planned_HU,
    (SELECT SUM(DISTINCT uil.weight_kg)
       FROM inventory_view iv,
            locmst lm,
            aremst am,
            shipment_line sl,
            shipment sh,
            adrmst addr,
            uc_invlod uil,
            uc_load_grp ulg 
      WHERE lm.wh_id = 'MWCCD'
                AND iv.lodnum=uil.lodnum
                AND am.arecod = lm.arecod
                AND am.wh_id = lm.wh_id
                AND lm.stoloc = iv.stoloc
                AND lm.wh_id = iv.wh_id
                AND addr.host_ext_id = ulg.dest_code
                AND ulg.client_id IS NOT NULL
                AND addr.adr_id = sh.rt_adr_id
                AND sl.ship_line_id = iv.ship_line_id
                AND sh.ship_id = sl.ship_id
                AND sh.stop_id IS NOT NULL
                AND am.fwiflg = 1
                AND am.stgflg = 1) AS Planned_Weight_KG
 FROM inventory_view iv,
    locmst lm,
    aremst am,
    shipment_line sl,
    shipment sh,
    adrmst addr,
    uc_invlod uil,
    uc_load_grp ulg        
 WHERE lm.wh_id = 'MWCCD'
        AND iv.lodnum=uil.lodnum
        AND am.arecod = lm.arecod
        AND am.wh_id = lm.wh_id
        AND lm.stoloc = iv.stoloc
        AND lm.wh_id = iv.wh_id
        AND addr.host_ext_id = ulg.dest_code
        AND ulg.client_id IS NOT NULL
        AND addr.adr_id = sh.rt_adr_id
        AND sl.ship_line_id = iv.ship_line_id
        AND sh.ship_id = sl.ship_id
        AND am.fwiflg = 1
        AND am.stgflg = 1
GROUP BY addr.host_ext_id]

这个问题是虽然它按照我想要的方式将列分开,但无论addr.host_ext_id如何,它都会错误地计算它。对于每个ad​​dr.host_ext_id,计划/未计划的HU和权重的数量应该是唯一的。

任何帮助和花费的时间都将受到高度赞赏!另外,我想要注意的是,我确实尝试使用UNION ALL更简单,并且将列组合在一起。

1 个答案:

答案 0 :(得分:1)

adrmst表的主查询中的别名更改为子查询中使用的不同内容,例如addr1。然后,您可以通过添加到每个子查询条件,将addr1.host_ext_id与每个子查询addr.host_ext_id连接起来:

  AND addr.host_ext_id = addr1.host_ext_id

我认为这种修正必须有所帮助。

还考虑使用JOIN语法重写查询,正如Gordon Linoff所建议的那样。这使它更具可读性。