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:




  1. logger_time-----------------probe_temp------converter_temp

  2. 2018-11-10 21:44:00.000-----210.3837--------759.3838

  3. 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.




  1. sig---------------aLow-------aHigh-------bLow--------bHigh


  2. probe_temp-------220.0000---235.0000-----210.0000----245.0000


  3. 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










share|improve this question
























  • 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















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:




  1. logger_time-----------------probe_temp------converter_temp

  2. 2018-11-10 21:44:00.000-----210.3837--------759.3838

  3. 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.




  1. sig---------------aLow-------aHigh-------bLow--------bHigh


  2. probe_temp-------220.0000---235.0000-----210.0000----245.0000


  3. 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










share|improve this question
























  • 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













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:




  1. logger_time-----------------probe_temp------converter_temp

  2. 2018-11-10 21:44:00.000-----210.3837--------759.3838

  3. 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.




  1. sig---------------aLow-------aHigh-------bLow--------bHigh


  2. probe_temp-------220.0000---235.0000-----210.0000----245.0000


  3. 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










share|improve this question















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:




  1. logger_time-----------------probe_temp------converter_temp

  2. 2018-11-10 21:44:00.000-----210.3837--------759.3838

  3. 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.




  1. sig---------------aLow-------aHigh-------bLow--------bHigh


  2. probe_temp-------220.0000---235.0000-----210.0000----245.0000


  3. 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











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',
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
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?