- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试弄清楚如何循环查询以与 Charts.JS 一起使用。
查询:
<cfquery name="bymonth" datasource="#application.dsn#">
SELECT DISTINCT r_vehicle, r_month, COUNT(*)
FROM rsvs
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month;
</cfquery>
这给了我这样的数据:
r_vehicle r_month COUNT(*)
1 Limo 01 2
2 Limo 02 1
3 Limo 05 1
4 Limo 07 3
5 Limo 08 3
6 Limo 09 3
7 Limo 11 2
8 Charter Bus 01 3
9 Charter Bus 02 2
10 Charter Bus 03 2
我需要循环查询的 Chart.JS 的主要摘录:
labels: ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"],
{
label: "r_vehicle",
backgroundColor: window.chartColors.red,
borderColor: window.chartColors.red,
data: [
(Number of times this vehicle was used per month -
Displayed as: 3,6,5,2,3,7,9,2,3,8,3,2)
],
fill: false,
}
对于任何未使用 r_vehicle 的月份,我都需要插入一个“零”(因为它们不会出现在查询结果中)。
我已经尝试了很多方法,但它只比我的薪水高出几个档次!非常感谢任何帮助。
更新:当给定月份没有车辆使用时,这应该添加“0”:
<cfquery name="bymonth2" datasource="#application.dsn#">
SELECT DISTINCT r_vehicle, r_month, IFNULL(count(r_vehicle),0) AS vehCount
FROM rsvs
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month;
</cfquery>
所以,在这一点上,我想我只需要知道如何像这样循环输出数据:
LIMO(r_vehicle)2,1,0,0,1,0,3,3,3,0,2,0 (vehCount)
第二次更新:我有以下代码-
<cfquery name="bymonthLimos" datasource="#application.dsn#">
SELECT DISTINCT r_vehicle, r_month, IFNULL(count(r_vehicle),0) AS vehCount
FROM rsvs
WHERE r_vehicle = 'Limo'
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month
</cfquery>
<cfset MonthList = "01,02,03,04,05,06,07,08,09,10,11,12">
<cfset MonthValues = structNew()>
<cfloop list="#MonthList#" index="index" delimiters=",">
<cfset structInsert(MonthValues,"Month#index#", "0")>
</cfloop>
<cfoutput query="bymonthLimos">
<cfset MonthCount = ValueList(bymonth2.vehCount, ",")>
#r_vehicle# - #MonthCount#<br>
</cfoutput>
我一定是做错了输出查询,这就是我得到的:
豪华轿车 - 3,2,2,2,1,1,3,4,3,5,5,7,1,5,13,17,16,12,17,7,16,7,9 ,13,1,3,8,7,7,7,13,9,5,6,7,12,3,3,8,10,3,7,7,5,8,1,3,7 ,1,1,3,4,7,7,2,1,2,1,1,3,3,3,2,1,1,3,1,2,3,5,5,1,2 ,1,1,2,1,1,5,3,6,7,8,6,11,8,7,3
第三次更新将代码更改为:
<cfquery name="bymonthLimos" datasource="#application.dsn#">
SELECT DISTINCT r_vehicle, r_month, COUNT(*) as count
FROM rsvs
WHERE r_vehicle = 'Sedan'
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month
</cfquery>
<cfset MonthList = "01,02,03,04,05,06,07,08,09,10,11,12">
<cfset MonthValues = structNew()>
<cfloop list="#MonthList#" index="index" delimiters=",">
<cfset structInsert(MonthValues,"Month#index#", "0")>
</cfloop>
<cfoutput query="bymonthLimos">
<cfset StructUpdate(MonthValues, "Month#r_month#", #count#)>
#r_vehicle# - #count#<br>
</cfoutput>
请注意,我将它从 Limo 更改为 Sedan,因为 Sedan 缺少几个月。我现在得到:
轿车 - 1轿车 - 1轿车 - 3轿车 - 1轿车 - 2轿车 - 3轿车 - 5轿车 - 5轿车 - 1轿车 - 2
谢谢你 TRose!!!
所以,我看到剩下的两个问题:
1) 我只需要写一次'sedan'。2) 我需要用“零”填写缺失的月份
更新 4:
再次非常感谢 TRose!
所以,我现在有以下代码:
<cfquery name="bymonthLimos" datasource="#application.dsn#">
SELECT DISTINCT r_vehicle, r_month, COUNT(*) as count
FROM rsvs
WHERE r_vehicle = 'Sedan'
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month
</cfquery>
<cfset MonthList = "01,02,03,04,05,06,07,08,09,10,11,12">
<cfset MonthValues = createObject("java", "java.util.LinkedHashMap").init() />
<cfloop list="#MonthList#" index="index" delimiters=",">
<cfset structInsert(MonthValues,"Month#index#", "0")>
</cfloop>
OUTPUT:
<cfoutput>
<cfloop collection="#MonthValues#" item="key">
#key#: #MonthValues[key]# <br />
</cfloop>
</cfoutput>
<br>
OUTPUT2:
<cfoutput>
<cfset step = 0>
<cfloop collection="#MonthValues#" item="key">
<cfset step++>#MonthValues[key]#<cfif step lt 12>,</cfif>
</cfloop>
</cfoutput>
<br>
OUTPUT3:
<cfoutput query="bymonthLimos">#r_vehicle# -
<cfset StructUpdate(MonthValues, "Month#r_month#", #count#)>
#count#<br>
</cfoutput>
结果:
OUTPUT: Month01: 0 Month02: 0 Month03: 0 Month04: 0 Month05: 0 Month06: 0 Month07: 0 Month08: 0 Month09: 0 Month10: 0 Month11: 0 Month12: 0
OUTPUT2: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
OUTPUT3: Sedan - 1 Sedan - 1 Sedan - 3 Sedan - 1 Sedan - 2 Sedan - 3 Sedan - 5 Sedan - 5 Sedan - 1 Sedan - 2
显然我遗漏了一些东西。我正在玩弄它,看看我能做什么。
更新 5
这似乎有效,但不确定它是什么!
OUTPUT4:
<cfoutput>
#bymonthLimos.r_vehicle#
<cfset step = 0>
<cfloop collection="#MonthValues#" item="key">
<cfset step++>#MonthValues[key]#<cfif step lt 12>,</cfif>
</cfloop>
</cfoutput>
结果:
OUTPUT4: Sedan 1, 1, 3, 0, 0, 1, 2, 3, 5, 5, 1, 2
雅虎!!!再次感谢!!
现在可以很好地与 Chart.js 脚本配合使用!
data: [
<cfoutput>
<cfset step = 0>
<cfloop collection="#MonthValues#" item="key">
<cfset step++>#MonthValues[key]#<cfif step lt 12>,</cfif>
</cfloop>
</cfoutput>
],
只是想指出一些事情:
1) 我必须将 CFOUTPUT 内容(不是 Charts.JS 标签中的内容)放入 CFSILENT 标签中,否则数据将以 HTML 格式显示。
2) DB 必须将月份格式设置为 MM 而不仅仅是 M。
最佳答案
这更像是一条超长评论。只是整理一下逻辑。数组和列表是我存在的祸根,并且总是难以有效地处理,但我至少可以给你一些提示。
就像...结构是你的 friend !
我刚刚查找了 Chart.js 的格式。您可以分别选择 Limos 和 Charter Buses,然后将任一查询作为同一图表上的单独数据集进行循环。我认为这很重要,这样我们就可以只关注月,这是你需要努力的,是吗?
来源:https://www.sitepoint.com/introduction-chart-js-2-0-six-examples/
例如,对于 Limos:
<cfquery name="bymonthLimos" datasource="#application.dsn#">
SELECT DISTINCT r_vehicle, r_month, COUNT(*)
FROM rsvs
GROUP BY r_vehicle, r_month
ORDER BY r_vehicle, r_month
WHERE r_vehicle LIKE 'Limo';
</cfquery>
它会给你这样的结果集:
r_vehicle r_month COUNT(*)
1 Limo 01 2
2 Limo 02 1
3 Limo 05 1
4 Limo 07 3
5 Limo 08 3
6 Limo 09 3
7 Limo 11 2
所以我所做的是我制作了一个包含一年中所有月份的列表,其格式与它们在您的数据库中的显示方式相同。
我遍历该列表以创建一个包含一年中所有月份和默认“计数”为零的结构。
<cfset MonthList = "01,02,03,04,05,06,07,08,09,10,11,12">
<cfset MonthValues = structNew()>
<cfloop list="#MonthList#" index="index" delimiters=",">
<cfset structInsert(MonthValues,"Month#index#", "0")>
</cfloop>
接下来,获取您租用 Limo 的月份的实际数据。遍历该查询并更新您的结构。
<cfoutput query="bymonthLimos">
<cfset StructUpdate(MonthValues, "Month#r_month#", #count#)>
</cfoutput>
现在您按顺序获得了每个月的正确计数。我把它扔了,根据你的数据应该是这样的:
从这里您可以以任何您认为合适的方式循环它以生成该数字列表并将其插入 Chart.js。
您可以用同样的方式为您想要的任何其他类型的车辆创建另一个数据集。
我看到您更改了查询以包含 0。如果您仍然需要为 Chart.js 生成列表,请尝试 valueList()。例如:
<cfset MonthCount = ValueList(bymonth2.COUNT, ",")>
好的,替换这个:
<cfset MonthValues = structNew()>
有了这个。
<cfset MonthValues = createObject("java", "java.util.LinkedHashMap").init() />
它们做完全相同的事情,但第二个以特定顺序保存结构信息。
之后,您可以遍历它以获取值。
<cfoutput>
<cfloop collection="#MonthValues#" item="key">
#key#: #MonthValues[key]# <br />
</cfloop>
</cfoutput>
您只需要 #MonthValues[key]#
(计数),它会产生 2, 1, 0, 0, 1, 0, 3, 3, 3, 0, 2, 0
但为了清晰起见,我将所有内容都包括在内。
一旦你遍历它,你就得到了你的列表。只需将其提供给您的图表插件并对其进行格式化。如果我正确地解释了他们的信息页面,您可以为数据制作任意数量的标签。
我冒昧地填写了第一个数据 (Limos) - 所以当您完成后,代码将看起来像这样。
data: {
labels: ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"],
datasets: [{
label: 'Limos',
data: [
<cfoutput>
<cfset step = 0>
<cfloop collection="#MonthValues#" item="key">
<cfset step++>#MonthValues[key]#<cfif step lt 12>,</cfif>
</cfloop>
</cfoutput>
],
backgroundColor: "rgba(153,255,51,1)"
},
{
label: 'Charter Buses',
data: [
YOUR DELIMITED DATA FOR CHARTER BUSES HERE
],
backgroundColor: "rgba(255,153,0,1)"
}]
}
});
等等。
关于mysql - 使用 Coldfusion 迭代查询以在 Charts.js 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43031656/
我有三张 table 。表 A 有选项名称(即颜色、尺寸)。表 B 有选项值名称(即蓝色、红色、黑色等)。表C通过将选项名称id和选项名称值id放在一起来建立关系。 我的查询需要显示值和选项的名称,而
在mysql中,如何计算一行中的非空单元格?我只想计算某些列之间的单元格,比如第 3-10 列之间的单元格。不是所有的列...同样,仅在该行中。 最佳答案 如果你想这样做,只能在 sql 中使用名称而
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我正在为版本7.6进行Elasticsearch查询 我的查询是这样的: { "query": { "bool": { "should": [ {
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
是否可以编写一个查询来检查任一子查询(而不是一个子查询)是否正确? SELECT * FROM employees e WHERE NOT EXISTS (
我找到了很多关于我的问题的答案,但问题没有解决 我有表格,有数据,例如: Data 1 Data 2 Data 3
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
我从 EditText 中获取了 String 值。以及提交查询的按钮。 String sql=editQuery.getText().toString();// SELECT * FROM empl
我有一个或多或少有效的查询(关于结果),但处理大约需要 45 秒。这对于在 GUI 中呈现数据来说肯定太长了。 所以我的需求是找到一个更快/更高效的查询(几毫秒左右会很好)我的数据表大约有 3000
这是我第一次使用 Stack Overflow,所以我希望我以正确的方式提出这个问题。 我有 2 个 SQL 查询,我正在尝试比较和识别缺失值,尽管我无法将 NULL 字段添加到第二个查询中以识别缺失
什么是动态 SQL 查询?何时需要使用动态 SQL 查询?我使用的是 SQL Server 2005。 最佳答案 这里有几篇文章: Introduction to Dynamic SQL Dynami
include "mysql.php"; $query= "SELECT ID,name,displayname,established,summary,searchlink,im
我有一个查询要“转换”为 mysql。这是查询: select top 5 * from (select id, firstName, lastName, sum(fileSize) as To
通过我的研究,我发现至少从 EF 4.1 开始,EF 查询上的 .ToString() 方法将返回要运行的 SQL。事实上,这对我来说非常有用,使用 Entity Framework 5 和 6。 但
我在构造查询来执行以下操作时遇到问题: 按activity_type_id过滤联系人,仅显示最近事件具有所需activity_type_id或为NULL(无事件)的联系人 表格结构如下: 一个联系人可
如何让我输入数据库的信息在输入数据 5 分钟后自行更新? 假设我有一张 table : +--+--+-----+ |id|ip|count| +--+--+-----+ |
我正在尝试搜索正好是 4 位数字的 ID,我知道我需要使用 LENGTH() 字符串函数,但找不到如何使用它的示例。我正在尝试以下(和其他变体)但它们不起作用。 SELECT max(car_id)
我有一个在 mysql 上运行良好的 sql 查询(查询 + 连接): select sum(pa.price) from user u , purchase pu , pack pa where (
我是一名优秀的程序员,十分优秀!