使用sqlite db创建处理原始表的视图

时间:2014-11-18 07:46:03

标签: c# sql sqlite

我正在使用C#Winforms应用程序并尝试使用以下表结构在sqlite中创建视图:

enter image description here

这就是视图的样子: enter image description here

这是sql命令,但不会产生希望的视图:

SELECT tbl1.Time AS C1,
(SELECT MIN(Value) FROM tbl1 AS T WHERE Time = T.Time AND T.VarId = 2) AS C2, 
(SELECT MIN(Value) FROM tbl1 AS T WHERE Time = T.Time AND T.VarId = 3) AS C3,
(SELECT MIN(Value) FROM tbl1 AS T WHERE Time = T.Time AND T.VarId = 4) AS C4, 
(SELECT MIN(Value) FROM tbl1 AS T WHERE Time = T.Time AND T.VarId = 5) AS C5,
(SELECT MIN(Value) FROM tbl1 AS T WHERE Time = T.Time AND T.VarId = 6) AS C6, 
(SELECT MIN(Value) FROM tbl1 AS T WHERE Time = T.Time AND T.VarId = 7) AS C7
FROM tbl1
WHERE tbl1.VarId IN (1, 2, 3, 4, 5, 6, 7)
GROUP BY tbl1.Time
ORDER BY tbl1.Time

我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:1)

在子查询中,没有表名的列名(如Time)引用最里面的表,在本例中为T。 这意味着Time = T.Time被解释为T.Time = T.Time

对于相关子查询,您必须比较内部表(T.Time)的列和外部表(tbl1.Time的列):

SELECT tbl1.Time AS Time,
(SELECT MIN(Value) FROM tbl1 AS T WHERE T.Time = tbl1.Time AND T.VarId = 1) AS C1,
(SELECT MIN(Value) FROM tbl1 AS T WHERE T.Time = tbl1.Time AND T.VarId = 2) AS C2,
(SELECT MIN(Value) FROM tbl1 AS T WHERE T.Time = tbl1.Time AND T.VarId = 3) AS C3,
(SELECT MIN(Value) FROM tbl1 AS T WHERE T.Time = tbl1.Time AND T.VarId = 4) AS C4,
(SELECT MIN(Value) FROM tbl1 AS T WHERE T.Time = tbl1.Time AND T.VarId = 5) AS C5,
(SELECT MIN(Value) FROM tbl1 AS T WHERE T.Time = tbl1.Time AND T.VarId = 6) AS C6,
(SELECT MIN(Value) FROM tbl1 AS T WHERE T.Time = tbl1.Time AND T.VarId = 7) AS C7,
(SELECT MIN(Value) FROM tbl1 AS T WHERE T.Time = tbl1.Time AND T.VarId = 8) AS C8,
(SELECT MIN(Value) FROM tbl1 AS T WHERE T.Time = tbl1.Time AND T.VarId = 9) AS C9,
tbl1.Method AS Method    -- ???
FROM tbl1
GROUP BY tbl1.Time
ORDER BY tbl1.Time