Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. item qty
  2. --------
  3. 3001 1
  4. 3003 3
  5. 3004 1
  6. 3006 2
  7.  
  8. item qty
  9. ----------
  10. 3001 2
  11. 3002 3
  12. 3006 3
  13. 3008 6
  14.  
  15. item qty
  16. -----------
  17. 3001 3
  18. 3002 3
  19. 3003 3
  20. 3004 1
  21. 3006 5
  22. 3008 6
  23.  
  24. SELECT Item, SUM(qty) as Qty FROM
  25. (
  26. SELECT Item, qty FROM warehouse_1
  27. UNION
  28. SELECT Item, qty FROM warehouse_2
  29. ) as a
  30. GROUP BY item;
  31.  
  32. select item,sum(qty) from(
  33. select item, Qty from warehouse_1 UNION
  34. select item, Qty from warehouse_2
  35. )x group by item
  36.  
  37. CREATE OR REPLACE VIEW warehouse_3 as
  38. SELECT coalesce(w1.item,w2.item) as item, w1.qty+w2.qty as qty
  39. FROM warehouse_1 as w1
  40. OUTER JOIN warehouse_2 as w2
  41. ON w1.item = w2.item;
  42.  
  43. select * from warehouse_3;
  44.  
  45. SELECT item , SUM(qty)
  46. FROM (
  47. SELECT item, qty
  48. FROM warehouse_1
  49. UNION
  50. SELECT item, qty
  51. FROM warehouse_2
  52. ) AS U GROUP BY item
  53.  
  54. create table tbl1(item int,qty int)
  55. INSERT INTO tbl1
  56. VALUES(3001,1),(3003,3),(3004,1),(3006,2)
  57.  
  58. create table tbl2(item int,qty int)
  59. INSERT INTO tbl2
  60. VALUES(3001,2),(3002,3),(3006,3),(3008,6)
  61.  
  62.  
  63. select isnull(t1.item,t2.item) as item,isnull(t1.qty,0)+isnull(t2.qty,0) as qty from tbl1 t1 full outer join tbl2 t2 on t1.item =t2.item
  64. order by 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement