<!-- language: lang-none -->
+----------------------------------------+
| Here is my sample table |
+----------------------------------------+
| Date Person Column_1 Column_2 |
| 15-03-13 A 100 NULL |
| 15-03-13 B NULL 100 |
| 16-03-13 A 100 50 |
| 16-03-13 B NULL NULL |
| 17-03-13 A 100 50 |
| 17-03-13 B 20 30 |
+----------------------------------------+
现在我想按日期做总和(column_1)。但我还需要哪一行包含NULL数据。 这是我想要实现的输出结果
+---------------------------------+
| Date SUM includesNullval |
+---------------------------------+
| 15-03-13 100 true |
| 16-03-13 100 true |
| 17-03-13 120 false |
+---------------------------------+
我不知道要达到以上输出。任何人都可以给我任何想法或解决这个问题吗?
答案 0 :(得分:1)
使用案例陈述可以帮助您:
Select Date_Column, sum(column_A),
MAX(CASE when column_A is NULL then 'True' else 'False' end) "Includes_NULL_Value"
from table_x
group by Date_column;
答案 1 :(得分:1)
请注意答案的形式和内容。
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(date DATE NOT NULL
,person CHAR(1) NOT NULL
,column_1 INT NULL
,column_2 INT NULL
,PRIMARY KEY(date,person)
);
INSERT INTO my_table VALUES
('2013-03-15','A', 100, NULL),
('2013-03-15','B',NULL, 100),
('2013-03-16','A', 100, 50),
('2013-03-16','B',NULL, NULL),
('2013-03-17','A', 100, 50),
('2013-03-17','B', 20, 30);
SELECT date, SUM(column_1), MAX(column_2 IS NULL) incnull FROM my_table GROUP BY date;
+------------+---------------+---------+
| date | SUM(column_1) | incnull |
+------------+---------------+---------+
| 2013-03-15 | 100 | 1 |
| 2013-03-16 | 100 | 1 |
| 2013-03-17 | 120 | 0 |
+------------+---------------+---------+