我正在尝试使用查询查询创建图表,但无论我尝试什么,都会收到500错误。我最终确定了cfchart标签作为问题,但无法在其中找到任何语法错误,也没有任何逻辑错误。
<cfquery name="getData" datasource="#dsn#">
SELECT
( SELECT Count(UserID)
FROM Users
WHERE StripeCustomerID IS NOT NULL
) AS 'Licenses'
, COUNT(UserID) AS 'Registrations'
, UserID AS 'User'
FROM Users
WHERE DATEDIFF(hour, FirstContact, DateStamp) <= '12'
GROUP BY UserID
</cfquery>
<cfquery name="queryOfQuery" dbtype="query">
SELECT (Licenses/Registrations) AS Percent FROM getData
</cfquery>
<cfchart format="flash">
<cfchartseries type="bar"
query="queryOfQuery"
itemcolumn="UserID"
valuecolumn="Percent" />
</cfchart>
示例数据
Users Table Sample Data
UserID StripeCustomerID
35 cus_8LOSlmvEQcfg1g
36 cus_6ixCBe7aU1MJEt
50 NULL
60 cus_6ifLCNmR9rOBCl
69 NULL
70 NULL
72 NULL
73 NULL
74 NULL
答案 0 :(得分:2)
您的语法问题是
SELECT (Licenses/Registrations) AS Percent FROM getData
百分比是一个保留的关键字,因此您需要添加括号或单引号,以便SQL引擎知道您使用保留关键字作为列别名。
SELECT (Licenses/Registrations) AS [Percent] FROM getData
OR
SELECT (Licenses/Registrations) AS 'Percent' FROM getData
您可以轻松地将百分比列添加到原始数据集中。
SELECT
(SELECT Count(UserID) FROM Users WHERE StripeCustomerID IS NOT NULL) AS Licenses,
COUNT(UserID) AS Registrations,
(SELECT Count(UserID) FROM Users WHERE StripeCustomerID IS NOT NULL) / COUNT(UserID) AS [Percent],
UserID AS User
FROM Users
WHERE DATEDIFF(hour, FirstContact, DateStamp) <= '12'
GROUP BY UserID
另外,我很好奇你想要的许可证。您是否想要获得所有DISTINCT UserId
的{{1}} count
或count
。其余的查询表明non null UserIds
在Users表中重复出现(我觉得很奇怪)......所以USerId
5可能存在2或3次,所以计算不同会给你1 userid 5虽然没有添加distinct但会给你2或3 ...
UserId
总是registrations
吗?如果是这样,则1
是唯一的,您根本不需要在主查询中使用您的聚合。而Percent的答案将始终与1 /#许可证相同.......
根据您的评论,这似乎是您想要的查询。
UserId