按ID和日期范围从两个表中检索数量数据

时间:2015-07-19 07:22:27

标签: sql-server

我有两张表

ItemTable_One

id     itemID    Date           qty
===================================================
1      1         2015-07-1      10   
2      1         2015-07-3      20  
3      2         2015-07-5      30  
4      2         2015-07-7      40  

ItemTable_Two

id     itemID    Date          qty
===================================================
1      1         2015-07-2     50   
2      1         2015-07-4     60  
3      3         2015-07-6     70  
4      3         2015-07-8     80  

我想检索日期范围内 itemID等于1 的数据 例如(2015-07-1和2015-07-30之间的日期范围)

itemID    Date          ItemTableOne_qty     ItemTableTwo_qty
============================================================================
1         2015-07-1     10                   0  
1         2015-07-3     20                   0
1         2015-07-2     0                    50
1         2015-07-4     0                    60 

我已尝试union加入和subquery来做这件事,但我在SQL查询中非常弱。

6 个答案:

答案 0 :(得分:2)

您可以使用UNION ALL获取所需的所有数据:

SELECT ItemTable_One.itemID, ItemTable_One.Date, ItemTable_One.qty as ItemTableOne_qty, 0 as ItemTableTwo_qty
FROM ItemTable_One
WHERE ItemTable_One.itemID = 1 AND 
    ItemTable_One.Date BETWEEN '2015-07-01' AND '2015-08-01'
UNION ALL
SELECT ItemTable_Two.itemID, ItemTable_Two.Date, 0 as ItemTableOne_qty, ItemTable_Two.qty as ItemTableTwo_qty
FROM ItemTable_One
WHERE ItemTable_Two.itemID = 1 AND 
    ItemTable_Two.Date BETWEEN '2015-07-01' AND '2015-08-01'

答案 1 :(得分:1)

试试这个:

select itemID, Date, qty as ItemTableOne_qty, 0 as ItemTableTwo_qty
from ItemTable_One
where ItemID = 1
and date >= '20150701'
and date < '20150731'

union all

select itemID, Date, 0 as ItemTableOne_qty, qty as ItemTableTwo_qty
from ItemTable_Two
where ItemID = 1
and date >= '20150701'
and date < '20150731'

日期的上限是故意&lt;比想要的日期+1所以,如果它是一个有时间的日期时间,那么最后一天也将被包括在内。

答案 2 :(得分:1)

您可以使用FULL OUTER JOIN

SELECT COALESCE(t1.itemID, t2.itemID) AS itemID, 
       COALESCE(t1.[Date], t2.[Date]) AS [Date],
       COALESCE(t1.qty, 0) AS ItemTableOne_qty,
       COALESCE(t2.qty, 0) AS ItemTableTwo_qty
FROM ItemTable_One AS t1
FULL OUTER JOIN ItemTable_Two AS t2 ON t1.itemID = t2.itemID AND t1.[Date] = t2.[Date]
WHERE COALESCE(t1.itemID, t2.itemID) = 1 AND
      COALESCE(t1.[Date], t2.[Date]) BETWEEN '2015-07-01' AND '2015-07-31' 
ORDER BY COALESCE(t1.[Date], t2.[Date])

这会将源表中具有相同[Date]值的记录放入输出表的同一行。

如果ItemTable_One 始终的记录与[Date]的记录具有单独的ItemTable_Two值,则其他答案中提出的UNION解决方案更可行

Demo here

答案 3 :(得分:0)

您使用FULL JOINUNION ALL执行此操作(具体取决于所需的输出)

完全加入

只要两个表之间没有相同的日期,FULL JOIN就可以获得您显示的结果。如果两个表中都存在这样的日期,那么每个日期都会得到一行,并且两个值都已填充。

要使用的查询是:

SELECT COALESCE(t1.itemID, t2.itemID) itemID, COALESCE(t1.Date, t2.Date) Date,
  ISNULL(t1.qty, 0) ItemTableOne_qty, ISNULL(t2.qty, 0) ItemTableTwo_qty
FROM ItemTable_One t1 FULL JOIN ItemTable_Two t2
  ON t1.itemID = t2.itemID AND t1.Date = t2.Date
WHERE COALESCE(t1.itemID, t2.itemID) = 1 AND
  COALESCE(t1.Date, t2.Date) BETWEEN '2015-07-01' AND '2015-08-01'

UNION ALL

UNION ALL将允许您获得您展示的结果,并将创建两个表中存在相同日期的重复行。任何行中始终至少有1'0'值。

要使用的查询是:

SELECT itemID, Date, qty ItemTableOne_qty, 0 ItemTableTwo_qty
FROM ItemTable_One
WHERE itemID = 1 AND Date BETWEEN '2015-07-01' AND '2015-08-01'

UNION ALL

SELECT itemID, Date, 0 ItemTableOne_qty, qty ItemTableTwo_qty
FROM ItemTable_Two
WHERE itemID = 1 AND Date BETWEEN '2015-07-01' AND '2015-08-01'

答案 4 :(得分:0)

这将是我们基于您的样本数据获得相同结果的另一种方式

declare @ItemTable_One table  (id int, itemID int,   Date date,   qty int)
insert into @ItemTable_One values
(1,      1,         '2015-07-1',      10),   
(2,      1,         '2015-07-3',      20), 
(3,      2,         '2015-07-5',      30),  
(4,      2,         '2015-07-7',      40)  

declare @ItemTable_Two table  (id int, itemID int,   Date date,   qty int)
insert into @ItemTable_Two values
(1,      1,         '2015-07-2',     50),   
(2,      1,         '2015-07-4',     60) , 
(3,      3,         '2015-07-6',     70) , 
(4,      3,         '2015-07-8',     80)  

;with CTE AS (
select i.itemID As ItemID1,ii.itemID As ItemID2,i.Date As Dated1,ii.Date As Dated2,i.qty as qty,ii.qty As qty1 from @ItemTable_One i
 CROSS APPLY (select * from @ItemTable_Two )ii
where  i.id = ii.id AND i.itemID = ii.itemID 
)
select * from (
Select ItemID1 As item,Dated1 AS Date, qty,'' as qty1 from CTE 
UNION 
Select ItemID2 As item,Dated2 AS Date,'' as qty,qty1 from CTE)T
--ORDER BY t.qty desc ,t.qty1

答案 5 :(得分:0)

使用内联视图来简化先前答案中使用的horrid sql:

mode