SQL query is not producing expected results












0















I need to turn our data (first part of the picture) into a column wise version (second part of the picture).



My code so far is



StrQuery = "SELECT  CAST(readingDTTM AS DATETIME) DATETIME, rValue FROM
loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft'" & _
"and Sensor = 'Depth' GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor"


but this only brings back the Depth. I need a further 2 columns for temperature and voltage.



Images of Data










share|improve this question

























  • Most people here want sample data and expected results as formatted text, not images.

    – jarlh
    Nov 27 '18 at 10:13











  • Use case expressions to do conditional aggregation.

    – jarlh
    Nov 27 '18 at 10:14
















0















I need to turn our data (first part of the picture) into a column wise version (second part of the picture).



My code so far is



StrQuery = "SELECT  CAST(readingDTTM AS DATETIME) DATETIME, rValue FROM
loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft'" & _
"and Sensor = 'Depth' GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor"


but this only brings back the Depth. I need a further 2 columns for temperature and voltage.



Images of Data










share|improve this question

























  • Most people here want sample data and expected results as formatted text, not images.

    – jarlh
    Nov 27 '18 at 10:13











  • Use case expressions to do conditional aggregation.

    – jarlh
    Nov 27 '18 at 10:14














0












0








0








I need to turn our data (first part of the picture) into a column wise version (second part of the picture).



My code so far is



StrQuery = "SELECT  CAST(readingDTTM AS DATETIME) DATETIME, rValue FROM
loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft'" & _
"and Sensor = 'Depth' GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor"


but this only brings back the Depth. I need a further 2 columns for temperature and voltage.



Images of Data










share|improve this question
















I need to turn our data (first part of the picture) into a column wise version (second part of the picture).



My code so far is



StrQuery = "SELECT  CAST(readingDTTM AS DATETIME) DATETIME, rValue FROM
loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft'" & _
"and Sensor = 'Depth' GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor"


but this only brings back the Depth. I need a further 2 columns for temperature and voltage.



Images of Data







sql excel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 19:21









halfer

14.6k758113




14.6k758113










asked Nov 27 '18 at 10:03









stephen smithstephen smith

1




1













  • Most people here want sample data and expected results as formatted text, not images.

    – jarlh
    Nov 27 '18 at 10:13











  • Use case expressions to do conditional aggregation.

    – jarlh
    Nov 27 '18 at 10:14



















  • Most people here want sample data and expected results as formatted text, not images.

    – jarlh
    Nov 27 '18 at 10:13











  • Use case expressions to do conditional aggregation.

    – jarlh
    Nov 27 '18 at 10:14

















Most people here want sample data and expected results as formatted text, not images.

– jarlh
Nov 27 '18 at 10:13





Most people here want sample data and expected results as formatted text, not images.

– jarlh
Nov 27 '18 at 10:13













Use case expressions to do conditional aggregation.

– jarlh
Nov 27 '18 at 10:14





Use case expressions to do conditional aggregation.

– jarlh
Nov 27 '18 at 10:14












2 Answers
2






active

oldest

votes


















0














SELECT CAST(readingDTTM AS DATETIME) DATETIME,
case Sensor when 'Depth' then rValue end as Depth,
case Sensor when 'Temperature' then rValue end as Temperature
FROM loggerData
WHERE sName = 'Westoe' AND subName = 'Crown Shaft'
and Sensor in ('Depth','Temperature')
GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor



or you could use pivot






share|improve this answer


























  • ve added a new comment can you have a look please Waldermort? thanks for your help @Waldermort

    – stephen smith
    Nov 27 '18 at 11:24



















0














select DATETIME, max(Depth) Depth, max(Temperature) Temperature from(
SELECT CAST(readingDTTM AS DATETIME) DATETIME,
case Sensor when 'Depth' then rValue end as Depth,
case Sensor when 'Temperature' then rValue end as Temperature
FROM loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft'
and Sensor in ('Depth','Temperature')
GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor
)
group by DATETIME



--not pretty, but it should work






share|improve this answer


























  • its not liking the group by syntax ---- "Incorrect syntax near the keyword 'group'." @waldermort ... really appreciate this

    – stephen smith
    Nov 27 '18 at 11:39











  • please try again with last modification, maybe your database is case sensitive, otherwise I have no idea, why it should not work

    – waldemort
    Nov 27 '18 at 11:59











  • it fails over on "Invalid object name 'loggerData'." now :( @waldermort

    – stephen smith
    Nov 27 '18 at 12:07













  • the semicolon at the end is causing problems

    – waldemort
    Nov 27 '18 at 12:37











  • i got it to work.... youre a star thanks very much for your help :)

    – stephen smith
    Nov 27 '18 at 13:57











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53497132%2fsql-query-is-not-producing-expected-results%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














SELECT CAST(readingDTTM AS DATETIME) DATETIME,
case Sensor when 'Depth' then rValue end as Depth,
case Sensor when 'Temperature' then rValue end as Temperature
FROM loggerData
WHERE sName = 'Westoe' AND subName = 'Crown Shaft'
and Sensor in ('Depth','Temperature')
GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor



or you could use pivot






share|improve this answer


























  • ve added a new comment can you have a look please Waldermort? thanks for your help @Waldermort

    – stephen smith
    Nov 27 '18 at 11:24
















0














SELECT CAST(readingDTTM AS DATETIME) DATETIME,
case Sensor when 'Depth' then rValue end as Depth,
case Sensor when 'Temperature' then rValue end as Temperature
FROM loggerData
WHERE sName = 'Westoe' AND subName = 'Crown Shaft'
and Sensor in ('Depth','Temperature')
GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor



or you could use pivot






share|improve this answer


























  • ve added a new comment can you have a look please Waldermort? thanks for your help @Waldermort

    – stephen smith
    Nov 27 '18 at 11:24














0












0








0







SELECT CAST(readingDTTM AS DATETIME) DATETIME,
case Sensor when 'Depth' then rValue end as Depth,
case Sensor when 'Temperature' then rValue end as Temperature
FROM loggerData
WHERE sName = 'Westoe' AND subName = 'Crown Shaft'
and Sensor in ('Depth','Temperature')
GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor



or you could use pivot






share|improve this answer















SELECT CAST(readingDTTM AS DATETIME) DATETIME,
case Sensor when 'Depth' then rValue end as Depth,
case Sensor when 'Temperature' then rValue end as Temperature
FROM loggerData
WHERE sName = 'Westoe' AND subName = 'Crown Shaft'
and Sensor in ('Depth','Temperature')
GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor



or you could use pivot







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 10:52

























answered Nov 27 '18 at 10:46









waldemortwaldemort

215




215













  • ve added a new comment can you have a look please Waldermort? thanks for your help @Waldermort

    – stephen smith
    Nov 27 '18 at 11:24



















  • ve added a new comment can you have a look please Waldermort? thanks for your help @Waldermort

    – stephen smith
    Nov 27 '18 at 11:24

















ve added a new comment can you have a look please Waldermort? thanks for your help @Waldermort

– stephen smith
Nov 27 '18 at 11:24





ve added a new comment can you have a look please Waldermort? thanks for your help @Waldermort

– stephen smith
Nov 27 '18 at 11:24













0














select DATETIME, max(Depth) Depth, max(Temperature) Temperature from(
SELECT CAST(readingDTTM AS DATETIME) DATETIME,
case Sensor when 'Depth' then rValue end as Depth,
case Sensor when 'Temperature' then rValue end as Temperature
FROM loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft'
and Sensor in ('Depth','Temperature')
GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor
)
group by DATETIME



--not pretty, but it should work






share|improve this answer


























  • its not liking the group by syntax ---- "Incorrect syntax near the keyword 'group'." @waldermort ... really appreciate this

    – stephen smith
    Nov 27 '18 at 11:39











  • please try again with last modification, maybe your database is case sensitive, otherwise I have no idea, why it should not work

    – waldemort
    Nov 27 '18 at 11:59











  • it fails over on "Invalid object name 'loggerData'." now :( @waldermort

    – stephen smith
    Nov 27 '18 at 12:07













  • the semicolon at the end is causing problems

    – waldemort
    Nov 27 '18 at 12:37











  • i got it to work.... youre a star thanks very much for your help :)

    – stephen smith
    Nov 27 '18 at 13:57
















0














select DATETIME, max(Depth) Depth, max(Temperature) Temperature from(
SELECT CAST(readingDTTM AS DATETIME) DATETIME,
case Sensor when 'Depth' then rValue end as Depth,
case Sensor when 'Temperature' then rValue end as Temperature
FROM loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft'
and Sensor in ('Depth','Temperature')
GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor
)
group by DATETIME



--not pretty, but it should work






share|improve this answer


























  • its not liking the group by syntax ---- "Incorrect syntax near the keyword 'group'." @waldermort ... really appreciate this

    – stephen smith
    Nov 27 '18 at 11:39











  • please try again with last modification, maybe your database is case sensitive, otherwise I have no idea, why it should not work

    – waldemort
    Nov 27 '18 at 11:59











  • it fails over on "Invalid object name 'loggerData'." now :( @waldermort

    – stephen smith
    Nov 27 '18 at 12:07













  • the semicolon at the end is causing problems

    – waldemort
    Nov 27 '18 at 12:37











  • i got it to work.... youre a star thanks very much for your help :)

    – stephen smith
    Nov 27 '18 at 13:57














0












0








0







select DATETIME, max(Depth) Depth, max(Temperature) Temperature from(
SELECT CAST(readingDTTM AS DATETIME) DATETIME,
case Sensor when 'Depth' then rValue end as Depth,
case Sensor when 'Temperature' then rValue end as Temperature
FROM loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft'
and Sensor in ('Depth','Temperature')
GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor
)
group by DATETIME



--not pretty, but it should work






share|improve this answer















select DATETIME, max(Depth) Depth, max(Temperature) Temperature from(
SELECT CAST(readingDTTM AS DATETIME) DATETIME,
case Sensor when 'Depth' then rValue end as Depth,
case Sensor when 'Temperature' then rValue end as Temperature
FROM loggerData WHERE sName = 'Westoe' AND subName = 'Crown Shaft'
and Sensor in ('Depth','Temperature')
GROUP BY CAST(readingDTTM AS DATETIME), rValue, Sensor
)
group by DATETIME



--not pretty, but it should work







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 12:24

























answered Nov 27 '18 at 11:23









waldemortwaldemort

215




215













  • its not liking the group by syntax ---- "Incorrect syntax near the keyword 'group'." @waldermort ... really appreciate this

    – stephen smith
    Nov 27 '18 at 11:39











  • please try again with last modification, maybe your database is case sensitive, otherwise I have no idea, why it should not work

    – waldemort
    Nov 27 '18 at 11:59











  • it fails over on "Invalid object name 'loggerData'." now :( @waldermort

    – stephen smith
    Nov 27 '18 at 12:07













  • the semicolon at the end is causing problems

    – waldemort
    Nov 27 '18 at 12:37











  • i got it to work.... youre a star thanks very much for your help :)

    – stephen smith
    Nov 27 '18 at 13:57



















  • its not liking the group by syntax ---- "Incorrect syntax near the keyword 'group'." @waldermort ... really appreciate this

    – stephen smith
    Nov 27 '18 at 11:39











  • please try again with last modification, maybe your database is case sensitive, otherwise I have no idea, why it should not work

    – waldemort
    Nov 27 '18 at 11:59











  • it fails over on "Invalid object name 'loggerData'." now :( @waldermort

    – stephen smith
    Nov 27 '18 at 12:07













  • the semicolon at the end is causing problems

    – waldemort
    Nov 27 '18 at 12:37











  • i got it to work.... youre a star thanks very much for your help :)

    – stephen smith
    Nov 27 '18 at 13:57

















its not liking the group by syntax ---- "Incorrect syntax near the keyword 'group'." @waldermort ... really appreciate this

– stephen smith
Nov 27 '18 at 11:39





its not liking the group by syntax ---- "Incorrect syntax near the keyword 'group'." @waldermort ... really appreciate this

– stephen smith
Nov 27 '18 at 11:39













please try again with last modification, maybe your database is case sensitive, otherwise I have no idea, why it should not work

– waldemort
Nov 27 '18 at 11:59





please try again with last modification, maybe your database is case sensitive, otherwise I have no idea, why it should not work

– waldemort
Nov 27 '18 at 11:59













it fails over on "Invalid object name 'loggerData'." now :( @waldermort

– stephen smith
Nov 27 '18 at 12:07







it fails over on "Invalid object name 'loggerData'." now :( @waldermort

– stephen smith
Nov 27 '18 at 12:07















the semicolon at the end is causing problems

– waldemort
Nov 27 '18 at 12:37





the semicolon at the end is causing problems

– waldemort
Nov 27 '18 at 12:37













i got it to work.... youre a star thanks very much for your help :)

– stephen smith
Nov 27 '18 at 13:57





i got it to work.... youre a star thanks very much for your help :)

– stephen smith
Nov 27 '18 at 13:57


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53497132%2fsql-query-is-not-producing-expected-results%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)