Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有多列结果的sqlite查询_Sql_Sqlite_Group By_Pivot - Fatal编程技术网

具有多列结果的sqlite查询

具有多列结果的sqlite查询,sql,sqlite,group-by,pivot,Sql,Sqlite,Group By,Pivot,我想要一个sqlite数据表: Margin Load Volt ------ ---- ---- 10 1 6.0 15 2 6.0 20 3 6.0 35 1 7.0 45 2 7.0 55 3 7.0 91 3 8.0 92 3 8.0 95 3 8.0 并创建新表: Load Margin6 Margin7 Ma

我想要一个sqlite数据表:

Margin  Load  Volt
------  ----  ----
10      1     6.0
15      2     6.0
20      3     6.0
35      1     7.0
45      2     7.0
55      3     7.0
91      3     8.0
92      3     8.0
95      3     8.0
并创建新表:

Load  Margin6  Margin7  Margin8
----  -------  -------  -------
1     10       35       91  
2     15       45       92
3     20       55       95

我不知道如何实现这一点。

您可以执行条件聚合:

select 
    load,
    max(case when volt = 6 then margin end) margin6,
    max(case when volt = 7 then margin end) margin7,
    max(case when volt = 8 then margin end) margin8
from mytable
group by load
如果要创建包含查询结果的表,可以使用CREATETABLE。。。作为选择。。。语法:

我也不建议这样做,因为它没有给您定义约束、主键和其他相关元信息的空间。您也可以先创建表,然后在其中插入一个insert。。。选择声明

create table newtable as
select 
    load,
    max(case when volt = 6 then margin end) margin6,
    max(case when volt = 7 then margin end) margin7,
    max(case when volt = 8 then margin end) margin8
from mytable
group by load