我有两个表,一个存储查询(e_key),一个存储附加到该查询的事件。这些事件具有指定的时间长度。我想加总查询的总时间,然后使用CASE将其分配给三个尺寸类别之一,我将其用作条形图上的x轴。
我正在使用它在我们选择的集成图表软件上设置“维度”字段:
CASE WHEN SUM(Events.EVENT_TIME_SPENT) < 1 THEN 'a. Light Touch' WHEN SUM(Events.EVENT_TIME_SPENT) >= 1 AND SUM(Events.EVENT_TIME_SPENT) < 3 THEN 'b. Medium Intensity' WHEN SUM(Events.EVENT_TIME_SPENT) >= 3 THEN 'c. High Intensity' ELSE 'Unspecified' END
,我得到“聚合函数在调用它的上下文中无效”。这是因为将选择复制到GROUP BY时,它不喜欢使用SUM()。
这是图表生成的完整SQL(y轴上的e_key计数,x轴上的大小带):
SELECT COUNT(distinct(Enquiry.E_KEY)) AS S1, CASE WHEN SUM(Events.EVENT_TIME_SPENT) < 1 THEN 'a. Low Intensity' WHEN SUM(Events.EVENT_TIME_SPENT) >= 1 AND SUM(Events.EVENT_TIME_SPENT) < 3 THEN 'b. Medium Intensity' WHEN SUM(Events.EVENT_TIME_SPENT) >= 3 THEN 'c. High Intensity' ELSE 'Unspecified' END AS D1
FROM
DB2ADMIN.F_ENQUIRY Enquiry
LEFT JOIN DB2ADMIN.F_EVENTS Events ON Enquiry.E_KEY = Events.EVENT_KEY
JOIN DB2ADMIN.F_PRIORITY EnquiryType ON Enquiry.E_PRIORITY_LINK = EnquiryType.PRIORITY_NO
WHERE
(EnquiryType.PRIORITY_NAME = 'Enquiry' )
AND (Enquiry.e_job_type_link=0)
AND (Events.EVENT_TYPE=2)
GROUP BY CASE WHEN SUM(Events.EVENT_TIME_SPENT) < 1 THEN 'a. Low Intensity' WHEN SUM(Events.EVENT_TIME_SPENT) >= 1 AND SUM(Events.EVENT_TIME_SPENT) < 3 THEN 'b. Medium Intensity' WHEN SUM(Events.EVENT_TIME_SPENT) >= 3 THEN 'c. High Intensity' ELSE 'Unspecified' END
ORDER BY CASE WHEN SUM(Events.EVENT_TIME_SPENT) < 1 THEN 'a. Low Intensity' WHEN SUM(Events.EVENT_TIME_SPENT) >= 1 AND SUM(Events.EVENT_TIME_SPENT) < 3 THEN 'b. Medium Intensity' WHEN SUM(Events.EVENT_TIME_SPENT) >= 3 THEN 'c. High Intensity' ELSE 'Unspecified' END
我无法使用该接口将SELECT与GROUP BY部分分开。关于实现此目标的不同方法有什么想法?如果将“类型”设置为“度量”而不是“尺寸”,则它可以按预期工作,但是无法在x轴上使用它。如果我在EVENT_TIME_SPENT周围设置了除SUM()以外的相同字段,则可以将“类型”设置为“维”,并且仍然可以使用。
答案 0 :(得分:0)
您不需要GROUP BY中的聚合。
就这样,您只想向后退一行吗?
SELECT COUNT(distinct(E_KEY)) AS S1
, CASE WHEN SUM(Events.EVENT_TIME_SPENT) < 1 THEN 'a. Low Intensity'
WHEN SUM(Events.EVENT_TIME_SPENT) >= 1 AND SUM(Events.EVENT_TIME_SPENT) < 3
THEN 'b. Medium Intensity'
WHEN SUM(Events.EVENT_TIME_SPENT) >= 3 THEN 'c. High Intensity'
ELSE 'Unspecified' END AS D1
FROM
TABLE(VALUES(1,1),(1,2),(2,2)) AS Events (E_KEY, EVENT_TIME_SPENT)
返回
S1 D1
-- -----------------
2 c. High Intensity
如果您要按某项分组,则例如可行
SELECT E_KEY AS S1
, CASE WHEN SUM(Events.EVENT_TIME_SPENT) < 1 THEN 'a. Low Intensity'
WHEN SUM(Events.EVENT_TIME_SPENT) >= 1 AND SUM(Events.EVENT_TIME_SPENT) < 3
THEN 'b. Medium Intensity'
WHEN SUM(Events.EVENT_TIME_SPENT) >= 3 THEN 'c. High Intensity'
ELSE 'Unspecified' END AS D1
FROM
TABLE(VALUES(1,1),(1,2),(2,2)) AS Events (E_KEY, EVENT_TIME_SPENT)
GROUP BY
E_KEY
S1 D1
-- -------------------
1 c. High Intensity
2 b. Medium Intensity
或者如果您不想以任何方式对EVENT_TIME_SPENT进行SUM(),那么您可能会在此之后
SELECT COUNT(DISTINCT E_KEY) AS S1
, CASE WHEN Events.EVENT_TIME_SPENT < 1 THEN 'a. Low Intensity'
WHEN Events.EVENT_TIME_SPENT >= 1 AND Events.EVENT_TIME_SPENT < 3
THEN 'b. Medium Intensity'
WHEN Events.EVENT_TIME_SPENT >= 3 THEN 'c. High Intensity'
ELSE 'Unspecified' END AS D1
FROM
TABLE(VALUES(1,1),(1,2),(2,2),(2,0),(1,4)) AS Events (E_KEY, EVENT_TIME_SPENT)
GROUP BY
CASE WHEN Events.EVENT_TIME_SPENT < 1 THEN 'a. Low Intensity'
WHEN Events.EVENT_TIME_SPENT >= 1 AND Events.EVENT_TIME_SPENT < 3
THEN 'b. Medium Intensity'
WHEN Events.EVENT_TIME_SPENT >= 3 THEN 'c. High Intensity'
ELSE 'Unspecified' END
S1 D1
-- -------------------
1 a. Low Intensity
2 b. Medium Intensity
1 c. High Intensity