2010-04-16 92 views
2

我有一个CSV格式的事务日志文件,我想用它来运行统计信息。日志具有以下字段:直接从CSV文件计算统计信息

 
date: Time/date stamp 
salesperson: The username of the person who closed the sale 
promo: sum total of items in the sale that were promotions. 
amount: grand total of the sale 

我希望得到以下数据:

 
salesperson: The username of the salesperson being analyzed. 
minAmount: The smallest grand total of this salesperson's transaction. 
avgAmount: The mean grand total.. 
maxAmount: The largest grand total.. 
minPromo: The smallest promo amount by the salesperson. 
avgPromo: The mean promo amount... 

我很想建立一个数据库结构,导入此文件,写SQL,和拉统计数据。除了这些统计数据,我不需要这些数据。有更容易的方法吗?我希望一些bash脚本可以使这一点变得简单。

+0

问题的哪一部分是您遇到问题?统计数据本身?数据结构?解析?对于任何您熟悉的脚本语言来说,这似乎不是一个非常难的问题。 – Kena 2010-04-16 19:30:58

+0

*拥抱PowerShell * ... – Joey 2010-04-16 19:42:00

回答

2

也可以敲出一个awk脚本来做到这一点。这只是带有几个变量的CSV。

+0

Awk很自然 - 甚至有开始/结束,所以你可以初始化,然后计算平均值很容易 – 2010-04-16 19:38:01

+0

非常有趣。我如何使用awk做“GROUP BY销售人员”? – User1 2010-04-16 20:17:25

+0

@ User1:使用关联数组或asort()或asorti()。 – 2010-04-16 20:36:43

1

您可以遍历CSV中的行并使用bash脚本变量来保存最小/最大金额。对于平均水平,只要保持一个运行总数,然后除以总行数(不包括可能的标题)。

这里有一些useful snippets用于在bash中处理CSV文件。

如果您的数据可能被引用(例如,因为一个字段包含逗号),使用bash,sed等进行处理变得更加复杂。

3

TxtSushi做到这一点:

tssql -table trans transactions.csv \ 
'select 
    salesperson, 
    min(as_real(amount)) as minAmount, 
    avg(as_real(amount)) as avgAmount, 
    max(as_real(amount)) as maxAmount, 
    min(as_real(promo)) as minPromo, 
    avg(as_real(promo)) as avgPromo 
from trans 
group by salesperson'

我有一大堆的example scripts说明如何使用它。

编辑:固定语法

+0

+1这看起来好多了。下次我不得不下一次。顺便说一句:你是一个土豚吗? – User1 2010-04-19 15:00:32

+0

不,先生,我是一个糖滑翔机 – Keith 2010-04-19 17:10:41

+0

这就是我所害怕的......我希望你的主人不会因为有你在他家(如果你在美国)而被捣毁。我听说你们“特别聪明”,但是,哇,回答是非常重要的。那么,糖是否让你如此聪明? – User1 2010-04-20 14:23:02