我正在尝试创建一个SQL Select,它将查找表#Parts中的所有行,其中#Parts.on_order列中的值与实际的订单数量不匹配。这是一个例子。三个表:零件,订单和接收。
IF OBJECT_ID(N'tempdb..#Parts') IS NOT NULL
BEGIN
DROP TABLE #Parts
END
IF OBJECT_ID(N'tempdb..#orders') IS NOT NULL
BEGIN
DROP TABLE #orders
END
IF OBJECT_ID(N'tempdb..#received') IS NOT NULL
BEGIN
DROP TABLE #received
END
create table #Parts (part_no char(25), part_pk int, on_order decimal(12,2))
insert into #Parts (part_no, part_pk, on_order) values ('ABC1', 1, 19)
insert into #Parts (part_no, part_pk, on_order) values ('ABC2', 2, 2)
insert into #Parts (part_no, part_pk, on_order) values ('ABC3', 3, 0)
create table #orders (po_order_no int, part_no char(25), part_pk int, qty_ordered decimal(12,2))
insert into #orders (po_order_no, part_no, part_pk, qty_ordered) values (1, 'ABC2', 2, 10)
insert into #orders (po_order_no, part_no, part_pk, qty_ordered) values (2, 'ABC2', 2, 1)
insert into #orders (po_order_no, part_no, part_pk, qty_ordered) values
(3, 'ABC2', 2, 4 )
insert into #orders (po_order_no, part_no, part_pk, qty_ordered) values (4, 'ABC1', 1, 20)
insert into #orders (po_order_no, part_no, part_pk, qty_ordered) values (5, 'ABC1', 1, 6)
insert into #orders (po_order_no, part_no, part_pk, qty_ordered) values (6, 'ABC3', 3, 7)
create table #received (po_order_no int, qty_received decimal(12,2))
insert into #received (po_order_no, qty_received) values (2, 1)
insert into #received (po_order_no, qty_received) values (3, 2)
insert into #received (po_order_no, qty_received) values (4, 3)
insert into #received (po_order_no, qty_received) values (4, 2)
insert into #received (po_order_no, qty_received) values (5, 2)
insert into #received (po_order_no, qty_received) values (6, 7)
我厌倦了以下SQL Select,但它甚至在ORDERED列中给出了错误的值:
select
MAX(#Parts.part_no), SUM(#received.qty_received) as RECEIVED,
SUM(#orders.qty_ordered) as ORDERED
from
#received
join
#orders on #orders.po_order_no = #received.po_order_no
join
#Parts on #Parts.part_pk = #orders.part_pk
group by
#Parts.part_pk, #orders.po_order_no
我错过了什么?
答案 0 :(得分:2)
您需要考虑这样一个事实,即收到的表可以有多个与一个订单相关联的记录。因此,您必须在将qty_received数据与orders表连接之前对其进行汇总,以消除重复订单的聚合:
SELECT
MAX(p.part_no)
,SUM(r.qty_received) as RECEIVED
,SUM(o.qty_ordered) as ORDERED
,SUM(o.qty_ordered) - SUM(r.qty_received) AS [DIFFERENCE]
FROM #parts AS p
JOIN #orders AS o on o.part_pk = p.part_pk
JOIN
( SELECT
po_order_no
,SUM(qty_received) AS qty_received
FROM #received
GROUP BY po_order_no
) AS r on r.po_order_no = o.po_order_no
GROUP BY p.part_pk, o.po_order_no
答案 1 :(得分:1)
创建表:
--add # to create a temp table
CREATE TABLE Parts (
part_no CHAR(25)
,part_pk INT
,on_order DECIMAL(12, 2)
);
insert into Parts (part_no, part_pk, on_order) values
('ABC1', 1, 19),
('ABC2', 2, 2),
('ABC3', 3, 0)
--add # to create a temp table
CREATE TABLE orders (
po_order_no INT
,part_no CHAR(25)
,part_pk INT
,qty_ordered DECIMAL(12, 2)
);
insert into orders (po_order_no, part_no, part_pk, qty_ordered) values
(1, 'ABC2', 2, 10),
(2, 'ABC2', 2, 1),
(3, 'ABC2', 2, 4),
(4, 'ABC1', 1, 20),
(5, 'ABC1', 1, 6),
(6, 'ABC3', 3, 7)
--add # to create a temp table
CREATE TABLE received (
po_order_no INT
,qty_received DECIMAL(12, 2)
);
insert into received (po_order_no, qty_received) values
(2, 1),
(3, 2),
(4, 3),
(4, 2),
(5, 2),
(6, 7)
SELECT STATEMENT:
SELECT p.part_no
,SUM(r.qty_received) AS RECEIVED
,SUM(o.qty_ordered) AS ORDERED
,SUM(o.qty_ordered) - SUM(r.qty_received) AS Total_Difference
FROM (
SELECT po_order_no
,sum(qty_received) AS qty_received
FROM received
GROUP BY po_order_no
) r
INNER JOIN orders o ON o.po_order_no = r.po_order_no
INNER JOIN parts p ON p.part_pk = o.part_pk
GROUP BY p.part_no, p.on_order --just need to group by part_no, on_order
--HAVING SUM(r.qty_received) <> SUM(o.qty_ordered) --optional filter
ORDER BY p.part_no
<强>结果:强>
+---------+----------+---------+------------------+
| part_no | RECEIVED | ORDERED | Total_Difference |
+---------+----------+---------+------------------+
| ABC1 | 7 | 26 | 19 |
| ABC2 | 3 | 5 | 2 |
| ABC3 | 7 | 7 | 0 |
+---------+----------+---------+------------------+
http://api.rubyonrails.org/classes/Rails/Application.html#method-i-config_for