- IF OBJECT_ID('TEST1') IS NOT NULL
- DROP TABLE TEST1;
-
- CREATE TABLE TEST1
- (
- 月份 int,
- 买入 int,
- );
- INSERT INTO TEST1
- SELECT 1,10 UNION ALL
- SELECT 1,20 UNION ALL
- SELECT 1,20 UNION ALL
- SELECT 2,30 UNION ALL
- SELECT 2,30;
-
- IF OBJECT_ID('TEST2') IS NOT NULL
- DROP TABLE TEST2;
-
- CREATE TABLE TEST2
- (
- 月份 int,
- 卖出 int,
- );
- INSERT INTO TEST2
- SELECT 1,5 UNION ALL
- SELECT 1,5 UNION ALL
- SELECT 1,10 UNION ALL
- SELECT 2,5 UNION ALL
- SELECT 2,5;
- SELECT A.月份,买入=SUM(A.买入),卖出=SUM(A.卖出)
- FROM
- (
- SELECT 月份,买入,卖出=NULL FROM TEST1
- UNION ALL
- SELECT 月份,买入=NULL,卖出 FROM TEST2
- ) AS A
- GROUP BY A.月份
-
复制代码 月份 买入 卖出
1 50 20
2 60 10 |