我是Alasql的新手,我想知道如何创建一个PIVOT并显示两个聚合。在下面的示例中,如何为每个名称和安全性显示AVG值和价格和。另外,我如何为每个聚合提供别名,即
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript" src="https://rawgit.com/agershun/alasql/develop/dist/alasql.js"></script>
</head>
<body>
<xmp id="output"></xmp>
<script type="text/javascript">
var data = [
{ security: 'Preferred Stock', name: 'Robert', value: 5, price:10 , date: '2014-1-3' },
{ security: 'Preferred Stock', name: 'Robert', value: 5, price:10 , date: '2014-1-5' },
{ security: 'Common Stock', name: 'Bert', value: 20, price:10 , date: '2014-1-6' },
{ security: 'Preferred Stock', name: 'Elizabeth', value: 10, price:10 , date: '2014-1-6' },
{ security: 'Common Stock', name: 'Robert', value: 20, price:10 , date: '2014-1-9' },
{ security: 'Preferred Stock', name: 'Bert', value: 20, price:10 , date: '2014-1-11' },
{ security: 'Preferred Stock', name: 'Robert', value: 5, price:10 , date: '2014-1-12' },
{ security: 'Preferred Stock', name: 'Robert', value: 15, price:10 , date: '2014-1-12' },
{ security: 'Options', name: 'Bert', value: 10, price:10 , date: '2014-1-13' },
{ security: 'Preferred Stock', name: 'Robert', value: 5, price:10 , date: '2014-1-14' },
{ security: 'Options', name: 'Robert', value: 15, price:10 , date: '2014-1-17' }
];
var res = alasql('SELECT name, "Series1" AS SeriesName, security, [value] \
FROM ? PIVOT (AVG([value]) FOR security)',[data]);
print( res );
var res1 = alasql('SELECT name, "Series2" AS SeriesName, security, price \
FROM ? PIVOT (SUM([price]) FOR security)',[data]);
print( res1 );
function print(x){
document.getElementById('output').textContent += JSON.stringify(x, null, '\t')+"\n";
}
</script>
</body>
</html>
上面的代码我得到的结果低于结果。
[{
"name": "Robert",
"SeriesName": "Series1",
"Preferred Stock": 7,
"Common Stock": 20,
"Options": 15
},
{
"name": "Bert",
"SeriesName": "Series1",
"Common Stock": 20,
"Preferred Stock": 20,
"Options": 10
},
{
"name": "Elizabeth",
"SeriesName": "Series1",
"Preferred Stock": 10
}
]
[{
"name": "Robert",
"SeriesName": "Series2",
"Preferred Stock": 50,
"Common Stock": 10,
"Options": 10
},
{
"name": "Bert",
"SeriesName": "Series2",
"Common Stock": 10,
"Preferred Stock": 10,
"Options": 10
},
{
"name": "Elizabeth",
"SeriesName": "Series2",
"Preferred Stock": 10
}
]
如何获得以下结果。提前致谢
[{
"name": "Robert",
"Series1 - Preferred Stock": 7,
"Series1 - Common Stock": 20,
"Series1 - Options": 15 "Series2 - Preferred Stock": 50,
"Series2 - Common Stock": 10,
"Series2 - Options": 10
},
{
"name": "Bert",
"Series1 - Common Stock": 20,
"Series1 - Preferred Stock": 20,
"Series1 - Options": 10 "Series2 - Common Stock": 10,
"Series2 - Preferred Stock": 10,
"Series2 - Options": 10
},
{
"name": "Elizabeth",
"Series1 - Preferred Stock": 10 "Series2 - Preferred Stock": 10
}
]
答案 0 :(得分:1)
要附加“系列名称”,您可以像这样修改您的SQL查询。
SELECT name, ("Series1" + security) AS security, [value] \
FROM ? PIVOT (AVG([value]) FOR security)
你可以在这里查看输出..
https://jsfiddle.net/jit_mehta023/qzdfrg0x/1/