0

我有一个表,其中包含一组数据,例如

record_date  |  id   | category | model    | name | alternate_name
-------------------------------------------------------------------
9/1/2012     |  N42  |   X      |  ABC     | blah |  blah
10/1/2011    |  N42  |   Y      |  No Code | #N/A |  #N/A
6/1/2012     |  N42  |   X      |  DEF     | xxxx |  yyyy
7/1/2011     |  N42  |   Z      |  No Data | #N/A |  #N/A

由于数据集不完整,我想用包含在 id 字段上匹配的数据的最新记录中的数据填充缺失的数据(模型、名称、备用名称)。

即我希望它看起来像这样

record_date  |  id   | category | model    | name | alternate_name
-------------------------------------------------------------------
9/1/2012     |  N42  |   X      |  ABC     | blah |  blah
10/1/2011    |  N42  |   Y      |  ABC     | blah |  blah
6/1/2012     |  N42  |   X      |  DEF     | xxxx |  yyyy
7/1/2011     |  N42  |   Z      |  ABC     | blah |  blah
4

3 回答 3

1

这是一种使用三个相关子查询的方法:

update "table" t
    set model = (select model from "table" t2 where t2.id = t.id and t2.record_date < t.record_date and model <> 'No Code' order by t2.record_date limit 1),
        name = (select name from "table" t2 where t2.id = t.id and t2.record_date < t.record_date and name <> '#N/A' order by t2.record_date limit 1),
        alternate_name  = (select alternate_name from "table" t2 where t2.id = t.id and t2.record_date < t.record_date and alternate_name <> '#N/A' order by t2.record_date limit 1)      
    where model = 'No Code' and name = '#N/A' and alternate_name = '#N/A';

我建议您在每一行上都有一个唯一的 ID。

于 2013-09-11T14:51:47.307 回答
0

感谢您的建议,但似乎都不太正确。我最终分两步完成了这项工作。

use myDB;
drop table if exists tmp_myTable;
create temporary table tmp_myTable like myTable;
insert into tmp_myTable select * from myTable as t1
  where record_date in (select max(record_Date)
    from myTable where id=t1.id 
    and (model!="No Code" or model="No Data")
    and name!="#N/A")
  and (category="X"
    or category="Y"
    or category="Z")
  and (model!="No Code" or model="No Data")
  and name!="#N/A";

update myTable as t1 join tmp_myTable as t2 on t1.id=t2.id
  set t1.model=t2.model, 
      t1.name=t2.name,
      t1.alternate_name=t2.alternate_name
  where (t1.category="X"
    or t1.category="Y"
    or t1.category="Z")
  and (t1.model="No Code" or t1.model="No Data")
  and t1.name="#N/A";
于 2013-09-12T12:57:58.053 回答
0

假设您的意思NULL是没有数据,您可以使用这些查询来更新一列。你可以用类似的IS NULL东西替换= "No Data"

UPDATE tableName A JOIN tableName B ON A.id = B.id
SET A.dummyID = B.dummyID
WHERE A.dummyID IS NULL AND B.dummyID IS NOT NULL;

例子

您应该对其他列运行类似的查询。您可以将此类查询合并为一个,但这会使查询复杂化并使其可读性降低。

假设您有另一列称为anotherColumn并且"N/A"没有数据,您可以使用以下查询 -

UPDATE tableName A JOIN tableName B ON A.id = B.id JOIN tableName C ON A.id = C.id
SET A.dummyID = B.dummyID, C.anotherColumn = B.anotherColumn
WHERE A.dummyID IS NULL AND B.dummyID IS NOT NULL AND
C.anotherColumn = "N/A" AND B.anotherColumn <> "N/A";

查看示例

于 2013-09-11T14:49:12.053 回答