加入查询结果

时间:2013-08-29 19:41:06

标签: sql sql-server

我有一个问题,我有一张桌子,我保留了传入,而另一张是我保留传出记录,我需要做的是按partNumber对这些记录进行分组并有一个输出。

InTable

PartNo Qty Date
A       1   1/1/13
A       5   1/1/13
B       2   1/1/13

OutTable

PartNo Qty Date
A       2  1/1/13
B       1  1/1/13
C       3  1/1/13


Result Needed

Date 1/1/13

PartNo In Out Total
A      6   2    4
B      2   1    1
C      0   3    -3  

我尝试了类似这样的东西,但由于总和(数量)而导致总数,但它不会以任何其他方式起作用。

select a.PartNo, sum(b.inQty) as inQty,sum(c.outQty) as outQty, sum(b.inQty)-sum(c.outQty) as total  from 
  (Select PartNo FROM InTable
  where date= '01-01-2013'
  group by PartNo
  union
  Select PartNo  FROM OutTable
  where date= '01-01-2013'
  group by PartNo) A
  cross join
  (  
  SELECT PartNo,SUM(Qty) inQty FROM InTable
  where date= '01-01-2013'
  group by PartNo
  )B
  cross join
  ( 
  SELECT PartNo,SUM(Qty) outQty FROM OutTable
  where date= '01-01-2013'
  group by PartNo
  )c
  group by a.PartNo

我尝试加入三个查询,每个查询单独产生一些有用的东西,但问题是当我尝试加入它们时,查询会产生类似

的内容
PartNo inQty outQty total 
A       8       6     2
B       8       6     2
C       8       6     2

任何sugestions?,谢谢。

7 个答案:

答案 0 :(得分:2)

在两个派生表上使用完全外部联接:

SELECT 
    COALESCE(inTab.PartNo, outTab.PartNo) AS PartNo,
    COALESCE(inQty, 0),
    COALESCE(outQty, 0),
    COALESCE(inQty, 0) -  COALESCE(outQty, 0) AS total
FROM 
 (
   SELECT PartNo, SUM(Qty) AS inQty 
   FROM InTable
   WHERE DATE= '01-01-2013'
   GROUP BY PartNo
 ) InTab
FULL JOIN
 (
   SELECT PartNo, SUM(Qty) AS outQty 
   FROM OutTable
   WHERE DATE= '01-01-2013'
   GROUP BY PartNo
 ) OutTab
ON inTab.Partno = outTab.PartNo   

答案 1 :(得分:2)

使用UNION ALL代替FULL OUTER JOIN Fiddle demo

尝试此操作
;with cte as (
    select partNo, Qty inQty, 0 outQty, mydate
    from inTable
    union all
    select partNo, 0 inQty, qty outQty, mydate
    from outTable
)
select partNo, sum(inQty) inQty,
               sum(outQty) outQty,
               sum(inQty) - sum(outQty) total
from cte
where mydate = '01/01/2013'
group by partNo
order by partNo

结果

| PARTNO | INQTY | OUTQTY | TOTAL |
---------|-------|--------|-------|
|      A |     6 |      2 |     4 |
|      B |     2 |      1 |     1 |
|      C |     0 |      3 |    -3 |

答案 2 :(得分:1)

使用完整加入

SELECT
  Coalesce(I.PartNo,O.PartNo) AS PartNo,
  IsNull(I.Qty,0) AS [In],
  IsNull(O.Qty,0) AS [Out],
  IsNull(I.Qty,0) - IsNull(O.Qty,0) AS [Total]
FROM
  (SELECT PartNo, Sum(Qty) AS Qty FROM InTable WHERE Date = '1/1/2013' GROUP BY PartNo) I
  FULL JOIN
  (SELECT PartNo, Sum(Qty) AS Qty FROM OutTable WHERE Date = '1/1/2013' GROUP BY PartNo) O ON I.PartNo = O.PartNo;

Sql Fiddle Example

答案 3 :(得分:0)

您需要使用FULL JOIN代替CROSS JOIN。您将A加入BC返回的所有行。

select a.PartNo, sum(b.inQty) as inQty,sum(c.outQty) as outQty, sum(b.inQty)-sum(c.outQty) as total  from 
  (Select PartNo FROM InTable
  where date= '01-01-2013'
  group by PartNo
  union
  Select PartNo  FROM OutTable
  where date= '01-01-2013'
  group by PartNo) A
  FULL JOIN
  (  
  SELECT PartNo,SUM(Qty) inQty FROM InTable
  where date= '01-01-2013'
  group by PartNo
  ) B ON b.partno = a.partno
  FULL JOIN
  ( 
  SELECT PartNo,SUM(Qty) outQty FROM OutTable
  where date= '01-01-2013'
  group by PartNo
  ) c ON c.partno = a.partno
  group by a.PartNo

<强> Editted 已从LEFT JOIN更改为FULL JOIN,因为您希望看到一行,即使它只在intable中或仅在outtable中。

答案 4 :(得分:0)

with part_cte(partno,date)as
(select partno,date from Intable
Union
Select partno,date from OutTable)

,intable_cte(partno,Qty,date)AS
(select Partno,sum(Qty)qty,Date from Intable Group By Partno,Date)

, outtable_cte(partno,Qty,date)AS
(select Partno,sum(Qty)qty,Date from outtable Group By Partno,Date)

select PC.partno,ISNULL(IC.QTY,0),ISNULL(OC.qty,0),PC.Date
From part_cte PC LEFT Join intable_cte IC on PC.partno=IC.partno and PC.date=IC.Date
LEFT Join outtable_cte OC on PC.partno=OC.partno and PC.date=OC.Date

答案 5 :(得分:0)

试试这个:

select isNull(a.PartNo,b.PartNo) as PartNo,
       IsNull(sum(a.qty),0) as InAmt,
       IsNull(sum(b.qty),0) as OutAmt,
       IsNull(sum(a.qty),0)-IsNull(sum(b.qty),0) as Diff
from Intable a
full join OutTable b on a.partno=b.partNo
group by isNull(a.PartNo,b.PartNo) 

SQLFiddle:http://www.sqlfiddle.com/#!3/5d06d/8

答案 6 :(得分:0)

Idid`t看到派生表,所以这是我从Firebird移植的解决方案

select t.id as [PartNo],
       sum(T.In) as [IN],
       sum(t.Out) as [Out],
       sum(T.inc)-sum(t.decc) as "Total" 
from (
     select p.Partno as [id] ,p.id as [In],0    as [Out],p.Date as dt from PARTSIN p
     union
     select o.Partno as [id], 0    as [In],o.id as [Out,o].Date as dt from PARTSOUT o
) as t
where t.dt='1/1/2013'
group by t.id;