case statement with aggregate joining to another table
up vote
0
down vote
favorite
I have a select clause that averages readings for the hour and gives an aggregate score for them.
The select looks like this (image is similar to the code)
Original Select Clause
SELECT DISTINCT
DATEADD(hh,DATEPART(hh,logger_time), CAST(CAST(logger_time AS DATE) AS DATETIME)) AS score_time,
CASE
WHEN AVG(probe_temp) BETWEEN 100 and 200 THEN 100
WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN (100-((ABS(AVG(probe_temp)- 200/abs(210-200)) * (100-60))))
END AS probe_temp_acc,
CASE
WHEN AVG(converter_temp) BETWEEN 700 and 750 THEN 100
WHEN AVG(converter_temp) BETWEEN 750 and 810 THEN (100-((ABS(AVG(converter_temp)- 750/abs(805-750)) * (100-60))))
END AS converter_temp_acc
FROM tblDeviceReading
GROUP BY DATEADD(hh,DATEPART(hh,logger_time),CAST(CAST(logger_time AS DATE) AS DATETIME))
ORDER BY score_time
The sample data that we need to aggregate looks like this:
- logger_time-----------------probe_temp------converter_temp
- 2018-11-10 21:44:00.000-----210.3837--------759.3838
- 2018-11-10 21:45:00.000-----219.8789--------761.1938
I need to join another table (below) to replace the hard coded values with data below. By this I mean that when I am looking at the probe_temp device I want to use aLow, aHigh, bLow, bHigh instead of the hard coded values of 100, 200, etc in the WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN... case statement.
sig---------------aLow-------aHigh-------bLow--------bHigh
probe_temp-------220.0000---235.0000-----210.0000----245.0000
converter_temp---760.0000---800.0000-----740.0000----805.0000
So the first select:
CASE
WHEN AVG(probe_temp) BETWEEN 100 and 200 THEN 100
WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN (100-((ABS(AVG(probe_temp)- 200/abs(210-200)) * (100-60))))
END AS probe_temp_acc
would need to look something like this:
CASE
WHEN AVG(probe_temp) BETWEEN aLow and aHight THEN 100
WHEN AVG(probe_temp) BETWEEN bLow and bHigh THEN (100-((ABS(AVG(probe_temp)- bLow/abs(bHigh-bLow)) * (100-60))))
END AS probe_temp_acc
Is there a way I can join the new table to the existing query and keep the aggregation over the group by clause? I looked at doing this in a cursor (the table of values will only have about 25 rows in it) but was wondering if there was a better way.
I think that philipxy is correct, that I have to use temporary tables to store the results then select at the end to get the results needed.
Results needed
Basically I have new non-hard coded limiters that I need to insert into the original query. The new data is in a row based table but the readings that need to be aggregated are in column values. I am unsure how to join these tables.
Thank you
inner-join aggregate-functions case-statement
add a comment |
up vote
0
down vote
favorite
I have a select clause that averages readings for the hour and gives an aggregate score for them.
The select looks like this (image is similar to the code)
Original Select Clause
SELECT DISTINCT
DATEADD(hh,DATEPART(hh,logger_time), CAST(CAST(logger_time AS DATE) AS DATETIME)) AS score_time,
CASE
WHEN AVG(probe_temp) BETWEEN 100 and 200 THEN 100
WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN (100-((ABS(AVG(probe_temp)- 200/abs(210-200)) * (100-60))))
END AS probe_temp_acc,
CASE
WHEN AVG(converter_temp) BETWEEN 700 and 750 THEN 100
WHEN AVG(converter_temp) BETWEEN 750 and 810 THEN (100-((ABS(AVG(converter_temp)- 750/abs(805-750)) * (100-60))))
END AS converter_temp_acc
FROM tblDeviceReading
GROUP BY DATEADD(hh,DATEPART(hh,logger_time),CAST(CAST(logger_time AS DATE) AS DATETIME))
ORDER BY score_time
The sample data that we need to aggregate looks like this:
- logger_time-----------------probe_temp------converter_temp
- 2018-11-10 21:44:00.000-----210.3837--------759.3838
- 2018-11-10 21:45:00.000-----219.8789--------761.1938
I need to join another table (below) to replace the hard coded values with data below. By this I mean that when I am looking at the probe_temp device I want to use aLow, aHigh, bLow, bHigh instead of the hard coded values of 100, 200, etc in the WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN... case statement.
sig---------------aLow-------aHigh-------bLow--------bHigh
probe_temp-------220.0000---235.0000-----210.0000----245.0000
converter_temp---760.0000---800.0000-----740.0000----805.0000
So the first select:
CASE
WHEN AVG(probe_temp) BETWEEN 100 and 200 THEN 100
WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN (100-((ABS(AVG(probe_temp)- 200/abs(210-200)) * (100-60))))
END AS probe_temp_acc
would need to look something like this:
CASE
WHEN AVG(probe_temp) BETWEEN aLow and aHight THEN 100
WHEN AVG(probe_temp) BETWEEN bLow and bHigh THEN (100-((ABS(AVG(probe_temp)- bLow/abs(bHigh-bLow)) * (100-60))))
END AS probe_temp_acc
Is there a way I can join the new table to the existing query and keep the aggregation over the group by clause? I looked at doing this in a cursor (the table of values will only have about 25 rows in it) but was wondering if there was a better way.
I think that philipxy is correct, that I have to use temporary tables to store the results then select at the end to get the results needed.
Results needed
Basically I have new non-hard coded limiters that I need to insert into the original query. The new data is in a row based table but the readings that need to be aggregated are in column values. I am unsure how to join these tables.
Thank you
inner-join aggregate-functions case-statement
Hi. Why don't you just join subqueries? Also, it's not clear what you mean by "replace the hard coded values with data" or "join the new table to the existing query and keep the aggregation over time".Please give example output & clearly explain in words (and maybe storing intermediates in temporary tables)--indeed give a Minimal, Complete, and Verifiable example.
– philipxy
Nov 12 at 20:27
I am updating the question to help clarify the statements you mentioned. You mentioned joining the subqueries, I was having problems doing that with the data in the subquery clause. Can you give me a simple example of what you mean please? Thanks for answering
– Daniel M. Cleary
Nov 12 at 22:32
Please use text, not images/links, for text (including code, tables & ERDs). Use a link/image only for convenience to supplement text and/or for what cannot be given in text. And never give a diagram without a legend/key. Use edit functions to inline, not links, if you have the rep--make your post self-contained. Tables are best given as tablular initialization code. PS "Basically" not introducing or summarizing a clear detailed presentation just means "unclearly". Your'e still not saying what you want. "row based table" doesn't mean anything.
– philipxy
Nov 12 at 22:53
Just google re SQL subqueries. Anyway you should be following a textbook with an intro to SQL. Dozens are online free in pdf. Re using temporaries I meant, to explain yourself--not as a solution, because there isn't enough question to propose a solution & you probably want subqueries not temporaries. If you can, please write some expression that gives the answer you want, eg possibly by naming results of previous calculations. Now you just say a lot of vague things re joining. PS A Minimal, Complete, and Verifiable example includes cut & paste & runnable DDL & DML as text in your question.
– philipxy
Nov 12 at 22:58
Thank you. I will handle this myself. I appreciate your links to documentation in that site.
– Daniel M. Cleary
Nov 15 at 13:52
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a select clause that averages readings for the hour and gives an aggregate score for them.
The select looks like this (image is similar to the code)
Original Select Clause
SELECT DISTINCT
DATEADD(hh,DATEPART(hh,logger_time), CAST(CAST(logger_time AS DATE) AS DATETIME)) AS score_time,
CASE
WHEN AVG(probe_temp) BETWEEN 100 and 200 THEN 100
WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN (100-((ABS(AVG(probe_temp)- 200/abs(210-200)) * (100-60))))
END AS probe_temp_acc,
CASE
WHEN AVG(converter_temp) BETWEEN 700 and 750 THEN 100
WHEN AVG(converter_temp) BETWEEN 750 and 810 THEN (100-((ABS(AVG(converter_temp)- 750/abs(805-750)) * (100-60))))
END AS converter_temp_acc
FROM tblDeviceReading
GROUP BY DATEADD(hh,DATEPART(hh,logger_time),CAST(CAST(logger_time AS DATE) AS DATETIME))
ORDER BY score_time
The sample data that we need to aggregate looks like this:
- logger_time-----------------probe_temp------converter_temp
- 2018-11-10 21:44:00.000-----210.3837--------759.3838
- 2018-11-10 21:45:00.000-----219.8789--------761.1938
I need to join another table (below) to replace the hard coded values with data below. By this I mean that when I am looking at the probe_temp device I want to use aLow, aHigh, bLow, bHigh instead of the hard coded values of 100, 200, etc in the WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN... case statement.
sig---------------aLow-------aHigh-------bLow--------bHigh
probe_temp-------220.0000---235.0000-----210.0000----245.0000
converter_temp---760.0000---800.0000-----740.0000----805.0000
So the first select:
CASE
WHEN AVG(probe_temp) BETWEEN 100 and 200 THEN 100
WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN (100-((ABS(AVG(probe_temp)- 200/abs(210-200)) * (100-60))))
END AS probe_temp_acc
would need to look something like this:
CASE
WHEN AVG(probe_temp) BETWEEN aLow and aHight THEN 100
WHEN AVG(probe_temp) BETWEEN bLow and bHigh THEN (100-((ABS(AVG(probe_temp)- bLow/abs(bHigh-bLow)) * (100-60))))
END AS probe_temp_acc
Is there a way I can join the new table to the existing query and keep the aggregation over the group by clause? I looked at doing this in a cursor (the table of values will only have about 25 rows in it) but was wondering if there was a better way.
I think that philipxy is correct, that I have to use temporary tables to store the results then select at the end to get the results needed.
Results needed
Basically I have new non-hard coded limiters that I need to insert into the original query. The new data is in a row based table but the readings that need to be aggregated are in column values. I am unsure how to join these tables.
Thank you
inner-join aggregate-functions case-statement
I have a select clause that averages readings for the hour and gives an aggregate score for them.
The select looks like this (image is similar to the code)
Original Select Clause
SELECT DISTINCT
DATEADD(hh,DATEPART(hh,logger_time), CAST(CAST(logger_time AS DATE) AS DATETIME)) AS score_time,
CASE
WHEN AVG(probe_temp) BETWEEN 100 and 200 THEN 100
WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN (100-((ABS(AVG(probe_temp)- 200/abs(210-200)) * (100-60))))
END AS probe_temp_acc,
CASE
WHEN AVG(converter_temp) BETWEEN 700 and 750 THEN 100
WHEN AVG(converter_temp) BETWEEN 750 and 810 THEN (100-((ABS(AVG(converter_temp)- 750/abs(805-750)) * (100-60))))
END AS converter_temp_acc
FROM tblDeviceReading
GROUP BY DATEADD(hh,DATEPART(hh,logger_time),CAST(CAST(logger_time AS DATE) AS DATETIME))
ORDER BY score_time
The sample data that we need to aggregate looks like this:
- logger_time-----------------probe_temp------converter_temp
- 2018-11-10 21:44:00.000-----210.3837--------759.3838
- 2018-11-10 21:45:00.000-----219.8789--------761.1938
I need to join another table (below) to replace the hard coded values with data below. By this I mean that when I am looking at the probe_temp device I want to use aLow, aHigh, bLow, bHigh instead of the hard coded values of 100, 200, etc in the WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN... case statement.
sig---------------aLow-------aHigh-------bLow--------bHigh
probe_temp-------220.0000---235.0000-----210.0000----245.0000
converter_temp---760.0000---800.0000-----740.0000----805.0000
So the first select:
CASE
WHEN AVG(probe_temp) BETWEEN 100 and 200 THEN 100
WHEN AVG(probe_temp) BETWEEN 200 and 300 THEN (100-((ABS(AVG(probe_temp)- 200/abs(210-200)) * (100-60))))
END AS probe_temp_acc
would need to look something like this:
CASE
WHEN AVG(probe_temp) BETWEEN aLow and aHight THEN 100
WHEN AVG(probe_temp) BETWEEN bLow and bHigh THEN (100-((ABS(AVG(probe_temp)- bLow/abs(bHigh-bLow)) * (100-60))))
END AS probe_temp_acc
Is there a way I can join the new table to the existing query and keep the aggregation over the group by clause? I looked at doing this in a cursor (the table of values will only have about 25 rows in it) but was wondering if there was a better way.
I think that philipxy is correct, that I have to use temporary tables to store the results then select at the end to get the results needed.
Results needed
Basically I have new non-hard coded limiters that I need to insert into the original query. The new data is in a row based table but the readings that need to be aggregated are in column values. I am unsure how to join these tables.
Thank you
inner-join aggregate-functions case-statement
inner-join aggregate-functions case-statement
edited Nov 12 at 22:40
asked Nov 12 at 17:56
Daniel M. Cleary
11
11
Hi. Why don't you just join subqueries? Also, it's not clear what you mean by "replace the hard coded values with data" or "join the new table to the existing query and keep the aggregation over time".Please give example output & clearly explain in words (and maybe storing intermediates in temporary tables)--indeed give a Minimal, Complete, and Verifiable example.
– philipxy
Nov 12 at 20:27
I am updating the question to help clarify the statements you mentioned. You mentioned joining the subqueries, I was having problems doing that with the data in the subquery clause. Can you give me a simple example of what you mean please? Thanks for answering
– Daniel M. Cleary
Nov 12 at 22:32
Please use text, not images/links, for text (including code, tables & ERDs). Use a link/image only for convenience to supplement text and/or for what cannot be given in text. And never give a diagram without a legend/key. Use edit functions to inline, not links, if you have the rep--make your post self-contained. Tables are best given as tablular initialization code. PS "Basically" not introducing or summarizing a clear detailed presentation just means "unclearly". Your'e still not saying what you want. "row based table" doesn't mean anything.
– philipxy
Nov 12 at 22:53
Just google re SQL subqueries. Anyway you should be following a textbook with an intro to SQL. Dozens are online free in pdf. Re using temporaries I meant, to explain yourself--not as a solution, because there isn't enough question to propose a solution & you probably want subqueries not temporaries. If you can, please write some expression that gives the answer you want, eg possibly by naming results of previous calculations. Now you just say a lot of vague things re joining. PS A Minimal, Complete, and Verifiable example includes cut & paste & runnable DDL & DML as text in your question.
– philipxy
Nov 12 at 22:58
Thank you. I will handle this myself. I appreciate your links to documentation in that site.
– Daniel M. Cleary
Nov 15 at 13:52
add a comment |
Hi. Why don't you just join subqueries? Also, it's not clear what you mean by "replace the hard coded values with data" or "join the new table to the existing query and keep the aggregation over time".Please give example output & clearly explain in words (and maybe storing intermediates in temporary tables)--indeed give a Minimal, Complete, and Verifiable example.
– philipxy
Nov 12 at 20:27
I am updating the question to help clarify the statements you mentioned. You mentioned joining the subqueries, I was having problems doing that with the data in the subquery clause. Can you give me a simple example of what you mean please? Thanks for answering
– Daniel M. Cleary
Nov 12 at 22:32
Please use text, not images/links, for text (including code, tables & ERDs). Use a link/image only for convenience to supplement text and/or for what cannot be given in text. And never give a diagram without a legend/key. Use edit functions to inline, not links, if you have the rep--make your post self-contained. Tables are best given as tablular initialization code. PS "Basically" not introducing or summarizing a clear detailed presentation just means "unclearly". Your'e still not saying what you want. "row based table" doesn't mean anything.
– philipxy
Nov 12 at 22:53
Just google re SQL subqueries. Anyway you should be following a textbook with an intro to SQL. Dozens are online free in pdf. Re using temporaries I meant, to explain yourself--not as a solution, because there isn't enough question to propose a solution & you probably want subqueries not temporaries. If you can, please write some expression that gives the answer you want, eg possibly by naming results of previous calculations. Now you just say a lot of vague things re joining. PS A Minimal, Complete, and Verifiable example includes cut & paste & runnable DDL & DML as text in your question.
– philipxy
Nov 12 at 22:58
Thank you. I will handle this myself. I appreciate your links to documentation in that site.
– Daniel M. Cleary
Nov 15 at 13:52
Hi. Why don't you just join subqueries? Also, it's not clear what you mean by "replace the hard coded values with data" or "join the new table to the existing query and keep the aggregation over time".Please give example output & clearly explain in words (and maybe storing intermediates in temporary tables)--indeed give a Minimal, Complete, and Verifiable example.
– philipxy
Nov 12 at 20:27
Hi. Why don't you just join subqueries? Also, it's not clear what you mean by "replace the hard coded values with data" or "join the new table to the existing query and keep the aggregation over time".Please give example output & clearly explain in words (and maybe storing intermediates in temporary tables)--indeed give a Minimal, Complete, and Verifiable example.
– philipxy
Nov 12 at 20:27
I am updating the question to help clarify the statements you mentioned. You mentioned joining the subqueries, I was having problems doing that with the data in the subquery clause. Can you give me a simple example of what you mean please? Thanks for answering
– Daniel M. Cleary
Nov 12 at 22:32
I am updating the question to help clarify the statements you mentioned. You mentioned joining the subqueries, I was having problems doing that with the data in the subquery clause. Can you give me a simple example of what you mean please? Thanks for answering
– Daniel M. Cleary
Nov 12 at 22:32
Please use text, not images/links, for text (including code, tables & ERDs). Use a link/image only for convenience to supplement text and/or for what cannot be given in text. And never give a diagram without a legend/key. Use edit functions to inline, not links, if you have the rep--make your post self-contained. Tables are best given as tablular initialization code. PS "Basically" not introducing or summarizing a clear detailed presentation just means "unclearly". Your'e still not saying what you want. "row based table" doesn't mean anything.
– philipxy
Nov 12 at 22:53
Please use text, not images/links, for text (including code, tables & ERDs). Use a link/image only for convenience to supplement text and/or for what cannot be given in text. And never give a diagram without a legend/key. Use edit functions to inline, not links, if you have the rep--make your post self-contained. Tables are best given as tablular initialization code. PS "Basically" not introducing or summarizing a clear detailed presentation just means "unclearly". Your'e still not saying what you want. "row based table" doesn't mean anything.
– philipxy
Nov 12 at 22:53
Just google re SQL subqueries. Anyway you should be following a textbook with an intro to SQL. Dozens are online free in pdf. Re using temporaries I meant, to explain yourself--not as a solution, because there isn't enough question to propose a solution & you probably want subqueries not temporaries. If you can, please write some expression that gives the answer you want, eg possibly by naming results of previous calculations. Now you just say a lot of vague things re joining. PS A Minimal, Complete, and Verifiable example includes cut & paste & runnable DDL & DML as text in your question.
– philipxy
Nov 12 at 22:58
Just google re SQL subqueries. Anyway you should be following a textbook with an intro to SQL. Dozens are online free in pdf. Re using temporaries I meant, to explain yourself--not as a solution, because there isn't enough question to propose a solution & you probably want subqueries not temporaries. If you can, please write some expression that gives the answer you want, eg possibly by naming results of previous calculations. Now you just say a lot of vague things re joining. PS A Minimal, Complete, and Verifiable example includes cut & paste & runnable DDL & DML as text in your question.
– philipxy
Nov 12 at 22:58
Thank you. I will handle this myself. I appreciate your links to documentation in that site.
– Daniel M. Cleary
Nov 15 at 13:52
Thank you. I will handle this myself. I appreciate your links to documentation in that site.
– Daniel M. Cleary
Nov 15 at 13:52
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53267613%2fcase-statement-with-aggregate-joining-to-another-table%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
Hi. Why don't you just join subqueries? Also, it's not clear what you mean by "replace the hard coded values with data" or "join the new table to the existing query and keep the aggregation over time".Please give example output & clearly explain in words (and maybe storing intermediates in temporary tables)--indeed give a Minimal, Complete, and Verifiable example.
– philipxy
Nov 12 at 20:27
I am updating the question to help clarify the statements you mentioned. You mentioned joining the subqueries, I was having problems doing that with the data in the subquery clause. Can you give me a simple example of what you mean please? Thanks for answering
– Daniel M. Cleary
Nov 12 at 22:32
Please use text, not images/links, for text (including code, tables & ERDs). Use a link/image only for convenience to supplement text and/or for what cannot be given in text. And never give a diagram without a legend/key. Use edit functions to inline, not links, if you have the rep--make your post self-contained. Tables are best given as tablular initialization code. PS "Basically" not introducing or summarizing a clear detailed presentation just means "unclearly". Your'e still not saying what you want. "row based table" doesn't mean anything.
– philipxy
Nov 12 at 22:53
Just google re SQL subqueries. Anyway you should be following a textbook with an intro to SQL. Dozens are online free in pdf. Re using temporaries I meant, to explain yourself--not as a solution, because there isn't enough question to propose a solution & you probably want subqueries not temporaries. If you can, please write some expression that gives the answer you want, eg possibly by naming results of previous calculations. Now you just say a lot of vague things re joining. PS A Minimal, Complete, and Verifiable example includes cut & paste & runnable DDL & DML as text in your question.
– philipxy
Nov 12 at 22:58
Thank you. I will handle this myself. I appreciate your links to documentation in that site.
– Daniel M. Cleary
Nov 15 at 13:52