我想知道下面的底部3'创建表'段是否有优雅的重构,它会检索相同的结果。 我希望能够推广出更大的数据集。 非常感谢提前。
// These top two lines for viewing in SQLite:
.mode column
.headers on
Create table Data_Table (
Other varchar(255),
Id varchar(255),
Value int );
INSERT INTO Data_Table (other, id, value )
VALUES ('x','a', 1);
INSERT INTO Data_Table (other,id, value )
VALUES ('y','a', 2);
INSERT INTO Data_Table (other,id, value )
VALUES ('x','b', 2);
INSERT INTO Data_Table (other,id, value )
VALUES ('y','b', 3);
Create table SubTable_A as
select t1.other as other, t1.value as a
from Data_table as t1
where t1.id = 'a';
Create table SubTable_B as
select t2.value as b
from Data_table as t2
where t2.id = 'b';
Create table Soln_Table as
select t1.*, t2.*
from SubTable_A as t1, SubTable_B as t2
where t1.rowid = t2.rowid;
底线,我们现在有了这个数据集
other a b
---------- ---------- ----------
x 1 2
y 2 3
答案 0 :(得分:0)
您可以使用条件聚合:
select t.other,
max(case when t.id = 'a' then value end) as a,
max(case when t.id = 'b' then value end) as b
from Data_Table t
group by t.other;