1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| --------------
set autocommit = 0
--------------
--------------
start transaction
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
default character set `latin1`
default collate `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `buy`
--------------
--------------
CREATE TABLE `buy`
( `id` integer unsigned not null auto_increment primary key,
`date` date not null,
`reference` varchar(255) not null,
`name` varchar(255) not null,
`qty_buy` integer not null
) engine=innoDB
default charset=latin1 collate=latin1_general_ci
row_format=compressed
--------------
--------------
insert into `buy` (`date`,`reference`,`name`,`qty_buy`) values
('2015-02-12', 'x001', 'glass', 3),
('2015-02-12', 'x002', 'bottle', 2),
('2015-04-18', 'x001', 'glass', 5),
('2015-04-19', 'x002', 'bottle', 1),
('2015-06-11', 'x001', 'glass', 1)
--------------
--------------
select * from buy
--------------
+----+------------+-----------+--------+---------+
| id | date | reference | name | qty_buy |
+----+------------+-----------+--------+---------+
| 1 | 2015-02-12 | x001 | glass | 3 |
| 2 | 2015-02-12 | x002 | bottle | 2 |
| 3 | 2015-04-18 | x001 | glass | 5 |
| 4 | 2015-04-19 | x002 | bottle | 1 |
| 5 | 2015-06-11 | x001 | glass | 1 |
+----+------------+-----------+--------+---------+
--------------
DROP TABLE IF EXISTS `sell`
--------------
--------------
CREATE TABLE `sell`
( `id` integer unsigned not null auto_increment primary key,
`date` date not null,
`reference` varchar(255) not null,
`name` varchar(255) not null,
`qty_sell` integer not null
) engine=innoDB
default charset=latin1 collate=latin1_general_ci
row_format=compressed
--------------
--------------
insert into `sell` (`date`,`reference`,`name`,`qty_sell`) values
('2015-02-24', 'x001', 'glass', 1),
('2015-03-02', 'x002', 'bottle', 1),
('2015-06-17', 'x001', 'glass', 8),
('2015-08-22', 'x001', 'glass', 1),
('2015-09-21', 'x002', 'bottle', 1)
--------------
--------------
select * from sell
--------------
+----+------------+-----------+--------+----------+
| id | date | reference | name | qty_sell |
+----+------------+-----------+--------+----------+
| 1 | 2015-02-24 | x001 | glass | 1 |
| 2 | 2015-03-02 | x002 | bottle | 1 |
| 3 | 2015-06-17 | x001 | glass | 8 |
| 4 | 2015-08-22 | x001 | glass | 1 |
| 5 | 2015-09-21 | x002 | bottle | 1 |
+----+------------+-----------+--------+----------+
--------------
select reference,
name,
sum(qty_buy) as buy,
sum(qty_sell) as sell,
sum(qty_buy - qty_sell) as diff
from (
select reference,
name,
qty_buy,
0 as qty_sell
from buy
union all
select reference,
name,
0 as qty_buy,
qty_sell
from sell
) as x
group by reference, name
order by reference, name
--------------
+-----------+--------+------+------+------+
| reference | name | buy | sell | diff |
+-----------+--------+------+------+------+
| x001 | glass | 9 | 10 | -1 |
| x002 | bottle | 3 | 2 | 1 |
+-----------+--------+------+------+------+
--------------
commit
--------------
--------------
set autocommit = 1
--------------
Appuyez sur une touche pour continuer... |
Partager