実行環境 Microsoft Windows XP Home Edition (SP3)

groupby.js
WScript.Quit(main());
 
function main()
{
	var args = WScript.Arguments;
	if (args.length != 1) {
		WScript.Echo("usage: cscript groupby.js event.csv");
		return 1;
	}
 
	// データベースへの接続
	var cn = new ActiveXObject("ADODB.Connection");
	cn.Open("Driver={Microsoft Text Driver (*.txt; *.csv)}; ReadOnly=1");
 
	// 全件数
	var rs = cn.Execute("select count(*) as total from " + args(0));
	var total = rs.Fields("total").Value;
	rs.Close;
	rs = null;
 
	// 検索
	var CommandText =
		"select 艦種, 艦名, count from 艦種.csv c, (" +
		"select 表示順, 艦名, count(*) as count from " + args(0) + " a, 艦種.csv b " +
		"where a.艦種略号 = b.艦種略号 group by 表示順, 艦名) d " +
		"where c.表示順 = d.表示順 order by c.表示順, count desc";
	var rs = cn.Execute(CommandText);
 
	var save;		// 艦種
	var countType = 0;	// 艦種件数
	var line;
	var c = 0;
	for ( ; !rs.Eof; rs.MoveNext) {
		var type	= rs.Fields("艦種").Value;
		var name	= rs.Fields("艦名").Value;
		var count	= rs.Fields("count").Value;
		if (save != type) {
			TypeWrite(save, countType, total, line);
			save = type;
			countType = 0;
			line = "";
		}
		line += " " + name;
		if (count != 1) {
			line += "x" + count;
		}
		countType += count;
		c += count;
	}
	TypeWrite(save, countType, total, line);
	WScript.Echo("count=" + c);
	rs.Close;
	rs = null;
 
	// 終了処理
	cn.Close;
	cn = null;
	return 0;
}
 
function TypeWrite(type, count, total, line)
{
	if (count < 1) return;
	WScript.Echo(type + "x" + count + "(" + Math.floor(100 * count / total) + "%):" + line);
}
 

schema.ini
[艦種.csv]
ColNameHeader=False
Format=TabDelimited
MaxScanRows=25
CharacterSet=OEM
Col1=表示順 Byte
Col2=艦種略号 Text
Col3=艦種 Text
 
[321.csv]
ColNameHeader=False
Format=TabDelimited
MaxScanRows=25
CharacterSet=OEM
Col1=艦種略号 Text
Col2=艦名 Text
 

艦種.csv
1	CV	正規空母
2	CVL	軽空母
3	AV	水上機母艦
4	BB	戦艦
5	CA	重巡洋艦
6	CL	軽巡洋艦
7	DD	駆逐艦
 

321.csv(抜粋)
CA	妙高
BB	比叡
DD	大潮
DD	朝潮
BB	榛名
CA	筑摩
CA	愛宕
DD	陽炎
CA	筑摩
DD	陽炎
 

出力
C:\projects\[[JScript]]\groupby>cscript groupby.js 321.csv
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

軽空母x10(4%): 祥鳳x5 隼鷹x3 飛鷹x2
水上機母艦x10(4%): 千歳x7 千代田x3
戦艦x35(14%): 伊勢x7 霧島x7 金剛x6 山城x4 比叡x3 日向x3 榛名x3 扶桑x2
重巡洋艦x99(39%): 那智x14 足柄x13 鳥海x12 利根x12 筑摩x12 羽黒x12 摩耶x9 高雄x5 最上x5 妙高x3 愛宕x2
軽巡洋艦x44(17%): 北上x9 多摩x8 川内x4 龍田x4 五十鈴x3 名取x3 那珂x3 神通x3 天龍x3 木曾x3 由良
駆逐艦x52(20%): 黒潮x9 霰x5 荒潮x5 大潮x5 綾波x4 陽炎x4 満潮x4 不知火x4 霞x3 磯波x3 朝潮x2 涼風 敷波 深雪 初雪
count=250
最終更新:2013年07月24日 14:47