从具有不同WHERE语句的多个表中选择项目

时间:2014-07-15 13:29:07

标签: sql

我想将这两个陈述合并为一个:

SELECT o.SITE            AS "Site", 
       COUNT(o.ORDER_NO) AS "No. Orders", 
FROM   ORDERS o 
WHERE  o.DATE_CREATED BETWEEN '01-JUL-13' AND '1-JUL-14' 
GROUP  BY o.SITE; 

SELECT o.SITE                       AS "Site", 
       COUNT(oi.LINE_CODE)          AS "No. Order Lines", 
       COUNT(DISTINCT oi.LINE_CODE) AS "No. Order Lines (UNIQUE)" 
FROM   ORDER_ITEMS oi, 
       ORDERS o 
WHERE  oi.ORDER_NO = o.ORDER_NO 
       AND oi.ORDER_TYPE = o.ORDER_TYPE 
       AND oi.SITE = o.SITE 
       AND o.DESPATCHED_ON_DATE BETWEEN '01-JUL-13' AND '01-JUL-14' 
GROUP  BY o.SITE; 

当添加到底部查询时,“No Orders”会发生什么,还会使用额外的3 where语句。有没有什么办法我可以指定我只希望“编号订单”使用where子句之间的日期,就像在顶部查询中一样?

3 个答案:

答案 0 :(得分:1)

您可以使用子选择,使其功能与第一次查询相同。

SELECT o.SITE                       AS "Site", 
       COUNT(oi.LINE_CODE)          AS "No. Order Lines", 
       COUNT(DISTINCT oi.LINE_CODE) AS "No. Order Lines (UNIQUE)",
       (SELECT COUNT(o2.ORDER_NO)
         FROM   ORDERS o2 
         WHERE  o2.site = o.site and o2.DATE_CREATED BETWEEN '01-JUL-13' AND '1-JUL-14' 
         GROUP  BY o2.SITE)  AS "No. Orders" 
FROM   ORDER_ITEMS oi, 
   ORDERS o 
WHERE  oi.ORDER_NO = o.ORDER_NO 
   AND oi.ORDER_TYPE = o.ORDER_TYPE 
   AND oi.SITE = o.SITE 
   AND o.DESPATCHED_ON_DATE BETWEEN '01-JUL-13' AND '01-JUL-14' 
GROUP  BY o.SITE;

答案 1 :(得分:0)

你想要那样的东西吗?

select o.site as "Site",
       count(oi.line_code) as "No. Order Lines",
       count(distinct oi.line_code) as "No. Order Lines (UNIQUE)",
       count(o.order_no) as "No. Orders"
from order_items oi, orders o
where oi.order_no = o.order_no
  and oi.order_type = o.order_type
  and oi.site = o.site
  and o.date_created between '01-JUL-13'
                         and '01-JUL-14'
group by o.site;

如果没有,请说明您的问题

这和Jchao的相同,但它更清晰,更易于维护:

    SELECT above.site as "Site",
           line_c1 as "No. Order Lines",
           line_c2 as "No. Order Lines (UNIQUE)",
           order_no as "No. Orders"
    FROM  
       (SELECT o.SITE AS site, 
               COUNT(o.ORDER_NO) AS order_no, 
          FROM ORDERS o 
         WHERE o.DATE_CREATED BETWEEN '01-JUL-13' AND '1-JUL-14' 
      GROUP BY o.SITE) above
      (SELECT o.SITE AS site, 
              COUNT(oi.LINE_CODE) AS line_c1, 
              COUNT(DISTINCT oi.LINE_CODE) AS line_c2 
         FROM ORDER_ITEMS oi, 
              ORDERS o 
        WHERE oi.ORDER_NO = o.ORDER_NO 
          AND oi.ORDER_TYPE = o.ORDER_TYPE 
          AND oi.SITE = o.SITE 
          AND o.DESPATCHED_ON_DATE BETWEEN '01-JUL-13' AND '01-JUL-14'
          AND o.DATE_CREATED BETWEEN '01-JUL-13' AND '1-JUL-14'
     GROUP BY o.SITE) bottom
WHERE above.site = bottom.site

答案 2 :(得分:0)

整蛊,我不确定我是否清楚这些细微差别,而且我没有测试它的示例数据(尽管它在SQL 2008中编译):

SELECT o.SITE                       AS "Site", 
       COUNT(distinct case
                        when o.DATE_CREATED BETWEEN '01-JUL-13' AND '1-JUL-14'
                          then o.DATE_CREATED
                        else null
                      end)          AS "No. Orders", 
       COUNT(oi.LINE_CODE)          AS "No. Order Lines", 
       COUNT(DISTINCT oi.LINE_CODE) AS "No. Order Lines (UNIQUE)" 
FROM   ORDERS o 
LEFT OUTER JOIN  ORDER_ITEMS oi
ON     oi.ORDER_NO = o.ORDER_NO 
  AND  oi.ORDER_TYPE = o.ORDER_TYPE 
  AND  oi.SITE = o.SITE 
  AND  o.DESPATCHED_ON_DATE BETWEEN '01-JUL-13' AND '01-JUL-14' 
GROUP  BY o.SITE; 

故障:

  • 首先,从ORDERS获取所有行
  • LEFT OUTER JOIN到(a)从ORDER_ITEMS获取所有相关行(根据ON子句),其中(b)获取所有项,即使 ORDERS.DATE_CREATED是不在指定日期之间,与第二次查询中的日期相匹配
  • 嵌套CASE语句中的COUNT DISTINCT然后按DATE_CREATED过滤掉ORDERS,无论通过左外连接找到(或不是)任何内容

各种乱七八糟的桌面扫描,我无法测试它。你可能最好坚持两个查询。