我有一个Sqlite表,其中ColumnA中的行具有相同的值但ColumnB中的值不同。以下查询给了我几百行。
从MyTable中选择ColumnA 具有Count(*)>的ColumnA分组1
我的要求是对于具有相同ColumnA值的行,只要ColumnC中的任何行中有值,就在所有行中填充ColumnC值。下面是输入和输出。
有人可以帮忙吗?
+--------------------------+
| +------+-------+------+ |
+--------------------------+
| |ColumnA|ColumnB|ColumnC |
| +------+-------+------+ |
| |C2 |Val1 | |
| +------+-------+------+ |
| |C2 |Val2 |P |
| +------+-------+------+ |
| |B2 |Val3 |Q |
| +------+-------+------+ |
| |B2 |Val4 | |
| +------+-------+------+ |
+--------------------------+
输出:
+---------------------------------------------------+
| +------+-------+------+ |
+---------------------------------------------------+
| |ColumnA|ColumnB|ColumnC |
| +------+-------+------+ |
| |C2 |Val1 |P //add P to this row |
| +------+-------+------+ |
| |C2 |Val2 |P |
| |
| +------+-------+------+ |
| |B2 |Val3 |Q |
| +------+-------+------+ |
| |B2 |Val4 |Q //add Q to this row |
| +------+-------+------+ |
+---------------------------------------------------+
答案 0 :(得分:2)
这样做你想要的吗?它使用相关子查询来更新值:
update mytable
set columnc = (select max(columnC) from mytable t2 where t2.columnA = mytable.columnA)
where columnC is null;