如何将特定日期放在alasql中的where子句中

时间:2016-03-15 10:27:52

标签: javascript alasql

请参阅以下我正在进行查询的数据。

result = alasql("SELECT HourOfDay, Count(HourOfDay) TotalCount from ? where ReportedDate = DATE('1/20/2012') group by HourOfDay ", [data])
<table style="width:100%">
  <tr>
  <th>ReportedDate</th>	<th>HourOfDay</th>
  </tr>
  <tr>   
    <td>2012-01-20 09:30:00.000</td>
    <td>9</td>   
  </tr>
  <tr>
    <td>2012-01-21 09:30:00.000</td>
    <td>11</td>   
  </tr>
  <tr>
    <td>2012-01-11 09:30:00.000</td>
    <td>9</td>   
  </tr><tr>
    <td>2012-01-20 09:30:00.000</td>
    <td>5</td>   
  </tr><tr>
    <td>2012-01-20 09:30:00.000</td>
    <td>9</td>   
  </tr><tr>
    <td>2012-01-13 09:30:00.000</td>    
    <td>7</td>   
  </tr><tr>
    <td>2012-01-20 09:30:00.000</td>    
    <td>9</td>   
  </tr><tr>
    <td>2012-01-20 09:30:00.000</td>   
    <td>10</td>   
  </tr>
</table>

我需要HourOfDay的总计数,其中日期是“1/20/2012”,我用给定的查询查询这些数据,但我没有得到任何结果所以请帮助我,我是alasql的新手。

这是我的数组

data=[
{'ReportedDate': '2012-01-20 09:30:00.000','HourOfDay':'9'},
{'ReportedDate': '2012-01-21 09:30:00.000','HourOfDay':'10'},
{'ReportedDate': '2012-01-11 09:30:00.000','HourOfDay':'1'},
{'ReportedDate': '2012-01-20 09:30:00.000','HourOfDay':'4'},
{'ReportedDate': '2012-01-20 09:30:00.000','HourOfDay':'5'},
{'ReportedDate': '2012-01-20 09:30:00.000','HourOfDay':'6'},
{'ReportedDate': '2012-01-20 09:30:00.000','HourOfDay':'8'},
{'ReportedDate': '2012-01-13 09:30:00.000','HourOfDay':'12'}];

1 个答案:

答案 0 :(得分:1)

我建议使用自定义功能。它可能是这样的:

alasql.fn.theDate(s){
    return s.split(' ').shift();
} 

hours = alasql("VALUE OF SELECT SUM(HourOfDay) FROM ? WHERE theDate(ReportedDate) = '2012-01-20'", [data])

在某些时候,您可能会发现每天获得总和很有意思:

totalHoursPerDay = alasql("SELECT theDate(ReportedDate) as date, SUM(HourOfDay) as hours FROM ? GROUP BY theDate(ReportedDate)", [data])