Aggregating within CASE

Multi tool use
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have two tables, one storing enquiries (e_key) and one storing events attached to that enquiry. These Events have lengths of time assigned to them. I want to add up the total time attached to an enquiry and then use a CASE to assign it to one of three size categories, which I am using as an x axis on a bar chart.
I am using this to set up a Dimension field on the charting software we integrate with as my select:
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
and I get 'an aggregate function is not valid in the context in which it was invoked'. This is because when the select is copied into the GROUP BY it doesn't like the use of SUM().
This is the full SQL the chart is generating (e_key count on y axis, size band on x axis):
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
I cannot separate the SELECT from the GROUP BY section using the interface. Any ideas on different ways to achieve this? If I set the Type to Measure rather than Dimension it works as expected but then I cannot use it on an x axis. If I set up the same field excluding SUM() around EVENT_TIME_SPENT it allows me to set Type to Dimension and still work.
sql db2
add a comment |
I have two tables, one storing enquiries (e_key) and one storing events attached to that enquiry. These Events have lengths of time assigned to them. I want to add up the total time attached to an enquiry and then use a CASE to assign it to one of three size categories, which I am using as an x axis on a bar chart.
I am using this to set up a Dimension field on the charting software we integrate with as my select:
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
and I get 'an aggregate function is not valid in the context in which it was invoked'. This is because when the select is copied into the GROUP BY it doesn't like the use of SUM().
This is the full SQL the chart is generating (e_key count on y axis, size band on x axis):
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
I cannot separate the SELECT from the GROUP BY section using the interface. Any ideas on different ways to achieve this? If I set the Type to Measure rather than Dimension it works as expected but then I cannot use it on an x axis. If I set up the same field excluding SUM() around EVENT_TIME_SPENT it allows me to set Type to Dimension and still work.
sql db2
Use the first query to create a view.
– jarlh
Nov 15 '18 at 16:07
Another option is make the query with the Summing a subquery and do the CASE checks in the outer query.
– Jorge Campos
Nov 15 '18 at 16:20
I'm not very good at this so if you could show me the structure of either of these suggestions I'll have a go.
– corky
Nov 15 '18 at 16:41
add a comment |
I have two tables, one storing enquiries (e_key) and one storing events attached to that enquiry. These Events have lengths of time assigned to them. I want to add up the total time attached to an enquiry and then use a CASE to assign it to one of three size categories, which I am using as an x axis on a bar chart.
I am using this to set up a Dimension field on the charting software we integrate with as my select:
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
and I get 'an aggregate function is not valid in the context in which it was invoked'. This is because when the select is copied into the GROUP BY it doesn't like the use of SUM().
This is the full SQL the chart is generating (e_key count on y axis, size band on x axis):
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
I cannot separate the SELECT from the GROUP BY section using the interface. Any ideas on different ways to achieve this? If I set the Type to Measure rather than Dimension it works as expected but then I cannot use it on an x axis. If I set up the same field excluding SUM() around EVENT_TIME_SPENT it allows me to set Type to Dimension and still work.
sql db2
I have two tables, one storing enquiries (e_key) and one storing events attached to that enquiry. These Events have lengths of time assigned to them. I want to add up the total time attached to an enquiry and then use a CASE to assign it to one of three size categories, which I am using as an x axis on a bar chart.
I am using this to set up a Dimension field on the charting software we integrate with as my select:
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
and I get 'an aggregate function is not valid in the context in which it was invoked'. This is because when the select is copied into the GROUP BY it doesn't like the use of SUM().
This is the full SQL the chart is generating (e_key count on y axis, size band on x axis):
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
I cannot separate the SELECT from the GROUP BY section using the interface. Any ideas on different ways to achieve this? If I set the Type to Measure rather than Dimension it works as expected but then I cannot use it on an x axis. If I set up the same field excluding SUM() around EVENT_TIME_SPENT it allows me to set Type to Dimension and still work.
sql db2
sql db2
edited Nov 15 '18 at 16:21
corky
asked Nov 15 '18 at 15:48
corkycorky
11
11
Use the first query to create a view.
– jarlh
Nov 15 '18 at 16:07
Another option is make the query with the Summing a subquery and do the CASE checks in the outer query.
– Jorge Campos
Nov 15 '18 at 16:20
I'm not very good at this so if you could show me the structure of either of these suggestions I'll have a go.
– corky
Nov 15 '18 at 16:41
add a comment |
Use the first query to create a view.
– jarlh
Nov 15 '18 at 16:07
Another option is make the query with the Summing a subquery and do the CASE checks in the outer query.
– Jorge Campos
Nov 15 '18 at 16:20
I'm not very good at this so if you could show me the structure of either of these suggestions I'll have a go.
– corky
Nov 15 '18 at 16:41
Use the first query to create a view.
– jarlh
Nov 15 '18 at 16:07
Use the first query to create a view.
– jarlh
Nov 15 '18 at 16:07
Another option is make the query with the Summing a subquery and do the CASE checks in the outer query.
– Jorge Campos
Nov 15 '18 at 16:20
Another option is make the query with the Summing a subquery and do the CASE checks in the outer query.
– Jorge Campos
Nov 15 '18 at 16:20
I'm not very good at this so if you could show me the structure of either of these suggestions I'll have a go.
– corky
Nov 15 '18 at 16:41
I'm not very good at this so if you could show me the structure of either of these suggestions I'll have a go.
– corky
Nov 15 '18 at 16:41
add a comment |
1 Answer
1
active
oldest
votes
You don't need aggregates in the GROUP BY.
As it is, you are only wanting one row back right?
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)
which returns
S1 D1
-- -----------------
2 c. High Intensity
if you want to group by something, then e.g. this works
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
or if you don't want to SUM() you EVENT_TIME_SPENT by anything, you might be after this
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
As D1 is the x axis I need it to return only three rows, one for each level of intensity. If I'm understanding you correctly grouping by e_key will still give me one row per enquiry.
– corky
Nov 15 '18 at 16:38
If you need to aggregate Events.EVENT_TIME_SPENT at some level, then you would need a sub-select. If you don't want to aggregate it, then use by last suggestion added above
– Paul Vernon
Nov 15 '18 at 18:27
It's the sub select within a case that I can't figure out how to format.
– corky
Nov 15 '18 at 23:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323108%2faggregating-within-case%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't need aggregates in the GROUP BY.
As it is, you are only wanting one row back right?
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)
which returns
S1 D1
-- -----------------
2 c. High Intensity
if you want to group by something, then e.g. this works
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
or if you don't want to SUM() you EVENT_TIME_SPENT by anything, you might be after this
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
As D1 is the x axis I need it to return only three rows, one for each level of intensity. If I'm understanding you correctly grouping by e_key will still give me one row per enquiry.
– corky
Nov 15 '18 at 16:38
If you need to aggregate Events.EVENT_TIME_SPENT at some level, then you would need a sub-select. If you don't want to aggregate it, then use by last suggestion added above
– Paul Vernon
Nov 15 '18 at 18:27
It's the sub select within a case that I can't figure out how to format.
– corky
Nov 15 '18 at 23:45
add a comment |
You don't need aggregates in the GROUP BY.
As it is, you are only wanting one row back right?
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)
which returns
S1 D1
-- -----------------
2 c. High Intensity
if you want to group by something, then e.g. this works
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
or if you don't want to SUM() you EVENT_TIME_SPENT by anything, you might be after this
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
As D1 is the x axis I need it to return only three rows, one for each level of intensity. If I'm understanding you correctly grouping by e_key will still give me one row per enquiry.
– corky
Nov 15 '18 at 16:38
If you need to aggregate Events.EVENT_TIME_SPENT at some level, then you would need a sub-select. If you don't want to aggregate it, then use by last suggestion added above
– Paul Vernon
Nov 15 '18 at 18:27
It's the sub select within a case that I can't figure out how to format.
– corky
Nov 15 '18 at 23:45
add a comment |
You don't need aggregates in the GROUP BY.
As it is, you are only wanting one row back right?
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)
which returns
S1 D1
-- -----------------
2 c. High Intensity
if you want to group by something, then e.g. this works
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
or if you don't want to SUM() you EVENT_TIME_SPENT by anything, you might be after this
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
You don't need aggregates in the GROUP BY.
As it is, you are only wanting one row back right?
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)
which returns
S1 D1
-- -----------------
2 c. High Intensity
if you want to group by something, then e.g. this works
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
or if you don't want to SUM() you EVENT_TIME_SPENT by anything, you might be after this
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
edited Nov 15 '18 at 18:26
answered Nov 15 '18 at 16:21
Paul VernonPaul Vernon
1,6071312
1,6071312
As D1 is the x axis I need it to return only three rows, one for each level of intensity. If I'm understanding you correctly grouping by e_key will still give me one row per enquiry.
– corky
Nov 15 '18 at 16:38
If you need to aggregate Events.EVENT_TIME_SPENT at some level, then you would need a sub-select. If you don't want to aggregate it, then use by last suggestion added above
– Paul Vernon
Nov 15 '18 at 18:27
It's the sub select within a case that I can't figure out how to format.
– corky
Nov 15 '18 at 23:45
add a comment |
As D1 is the x axis I need it to return only three rows, one for each level of intensity. If I'm understanding you correctly grouping by e_key will still give me one row per enquiry.
– corky
Nov 15 '18 at 16:38
If you need to aggregate Events.EVENT_TIME_SPENT at some level, then you would need a sub-select. If you don't want to aggregate it, then use by last suggestion added above
– Paul Vernon
Nov 15 '18 at 18:27
It's the sub select within a case that I can't figure out how to format.
– corky
Nov 15 '18 at 23:45
As D1 is the x axis I need it to return only three rows, one for each level of intensity. If I'm understanding you correctly grouping by e_key will still give me one row per enquiry.
– corky
Nov 15 '18 at 16:38
As D1 is the x axis I need it to return only three rows, one for each level of intensity. If I'm understanding you correctly grouping by e_key will still give me one row per enquiry.
– corky
Nov 15 '18 at 16:38
If you need to aggregate Events.EVENT_TIME_SPENT at some level, then you would need a sub-select. If you don't want to aggregate it, then use by last suggestion added above
– Paul Vernon
Nov 15 '18 at 18:27
If you need to aggregate Events.EVENT_TIME_SPENT at some level, then you would need a sub-select. If you don't want to aggregate it, then use by last suggestion added above
– Paul Vernon
Nov 15 '18 at 18:27
It's the sub select within a case that I can't figure out how to format.
– corky
Nov 15 '18 at 23:45
It's the sub select within a case that I can't figure out how to format.
– corky
Nov 15 '18 at 23:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323108%2faggregating-within-case%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
jl1UWvSGBhJJOeIKPhD6N1R7 aZ2KRjweQ IrpCWZbwxQwm8qMiULUMZ,Q,Xo,tHUYRp,2,6
Use the first query to create a view.
– jarlh
Nov 15 '18 at 16:07
Another option is make the query with the Summing a subquery and do the CASE checks in the outer query.
– Jorge Campos
Nov 15 '18 at 16:20
I'm not very good at this so if you could show me the structure of either of these suggestions I'll have a go.
– corky
Nov 15 '18 at 16:41