sql查询从两个表合并数据

时间:2018-06-01 02:11:58

标签: sql oracle

我正在尝试编写查询(针对Oracle)。我有三张桌子:

sale
====================================
sale_id | part_id | amt | cost | qty

quote
==============================
quote_id | part_id | amt | qty

part
================
part_id | number

我需要编写一个查询,按类似于此(非功能)查询的部件号对数字进行分组:

select p.number 

sum(s.amt * s.qty) as sales_amt_total,
sum(s.qty) as sales_qty_total,
count(s.sale_id) as sales_count,
sum(s.qty * s.cost) as cost_total,

sum(q.amt * q.qty) as quotes_amt_total,
sum(q.qty) as quotes_qty_total,
count(q.quote_id) as quotes_count

from parts p
inner join quotes q on q.part_id = p.part_id
inner join sales s on s.part_id = p.part_id

group by p.number

having sales_amt_total < ?
and sales_amt_total > ?
and sales_qty_total < ?
and sales_qty_total > ?
and sales_count < ?
and sales_count > ?
and cost_total < ?
and cost_total > ?

and quotes_amt_total < ?
and quotes_amt_total > ?
and quotes_qty_total < ?
and quotes_qty_total > ?
and quotes_count < ?
and quotes_count > ?

;

产生如下结果:

number | sales_amt_total | sales_qty_total | sales_count | cost_total | ...
---------------------------------------------------------------------------
    P1 |         9999999 |         9999999 |     9999999 |    9999999 | ...
    P2 |         9999999 |         9999999 |     9999999 |    9999999 | ...
    P3 |         9999999 |         9999999 |     9999999 |    9999999 | ...

但是,inner join quotesinner join sales会产生数字膨胀,因为它每次都在计算每场比赛。

我可以将这些分成两个查询:

select p.number 

sum(s.amt * s.qty) as sales_amt_total,
sum(s.qty) as sales_qty_total,
count(s.sale_id) as sales_count,
sum(s.qty * s.cost) as cost_total,

from parts p
inner join sales s on s.part_id = p.part_id

having sales_amt_total < ?
and sales_amt_total > ?
and sales_qty_total < ?
and sales_qty_total > ?
and sales_count < ?
and sales_count > ?
and cost_total < ?
and cost_total > ?
;

select p.number 

sum(q.amt * q.qty) as quotes_amt_total,
sum(q.qty) as quotes_qty_total,
count(q.quote_id) as quotes_count

from parts p
inner join quotes q on q.part_id = p.part_id

group by p.number

having quotes_amt_total < ?
and quotes_amt_total > ?
and quotes_qty_total < ?
and quotes_qty_total > ?
and quotes_count < ?
and quotes_count > ?

;

但我无法将having子句应用于每个表格。

您将如何创建此查询?

1 个答案:

答案 0 :(得分:1)

如下所示:

WITH ctePART_QUOTES AS
       (SELECT qq.PART_ID,
               COUNT(*) AS QUOTES_COUNT
               SUM(qq.AMT * qq.QTY) AS QUOTES_AMT_TOTAL,
               SUM(qq.QTY) as QUOTES_QTY_TOTAL
          FROM QUOTES qq
          GROUP BY qq.PART_ID),
     ctePART_SALES AS
       (SELECT ss.PART_ID,
               COUNT(*) AS SALES_COUNT,
               SUM(ss.AMT * ss.QTY) AS SALES_AMT_TOTAL,
               SUM(ss.QTY) AS SALES_QTY_TOTAL,
               COUNT(*) AS SALES_COUNT,
               SUM(ss.QTY * ss.COST) AS COST_TOTAL
          FROM SALES ss
          GROUP BY ss.PART_ID)
SELECT p.NUMBER,
       s.SALES_AMT_TOTAL,
       s.SALES_QTY_TOTAL,
       s.SALES_COUNT,
       s.COST_TOTAL,
       q.QUOTES_AMT_TOTAL,
       q.QUOTES_QTY_TOTAL,
       q.QUOTES_COUNT
  FROM PARTS p
  LEFT OUTER JOIN ctePART_QUOTES q
    ON q.PART_ID = p.PART_ID
  LEFT OUTER JOIN ctePART_SALES s
    ON s.PART_ID = p.PART_ID

添加所有HAVING内容,您应该好好去。

祝你好运。