SQL如何计算具有条件的子集

时间:2016-12-19 09:25:43

标签: sql

我有下表:

+--------+------------+----------------+
| saleId | saleDate   | contractId     |
+--------+------------+----------------+
| 1      | 01.07.2016 | 1001           |
| 2      | 02.07.2016 | 1001           |
| 3      | 03.07.2016 | 1002           |
| 4      | 04.07.2016 | 1002           |
| 5      | 05.07.2016 | 1001           |
| 6      | 06.07.2016 | 1001           |
+--------+------------+----------------+

我想计算每笔销售(每一行)的合约前期销售数量

+--------+------------+------------+------------------------+
| saleId | saleDate   | contractId | SalesCountPerContract  |
+--------+------------+------------+------------------------+
| 1      | 01.07.2016 | 1001       | 0                      |
| 2      | 02.07.2016 | 1001       | 1                      |
| 3      | 03.07.2016 | 1002       | 0                      |
| 4      | 04.07.2016 | 1002       | 1                      |
| 5      | 05.07.2016 | 1001       | 2                      |
| 6      | 06.07.2016 | 1001       | 3                      |
+--------+------------+------------+------------------------+

1 个答案:

答案 0 :(得分:2)

ALTER TABLE veille
ALTER COLUMN tag
SET IS_NULLABLE false