SQL query is not producing expected results
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.
sql excel
add a comment |
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.
sql excel
Most people here want sample data and expected results as formatted text, not images.
– jarlh
Nov 27 '18 at 10:13
Usecase
expressions to do conditional aggregation.
– jarlh
Nov 27 '18 at 10:14
add a comment |
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.
sql excel
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.
sql excel
sql excel
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
Usecase
expressions to do conditional aggregation.
– jarlh
Nov 27 '18 at 10:14
add a comment |
Most people here want sample data and expected results as formatted text, not images.
– jarlh
Nov 27 '18 at 10:13
Usecase
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
add a comment |
2 Answers
2
active
oldest
votes
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
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
add a comment |
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
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
|
show 2 more comments
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%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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
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
|
show 2 more comments
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
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
|
show 2 more comments
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
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
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
|
show 2 more comments
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
|
show 2 more comments
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%2f53497132%2fsql-query-is-not-producing-expected-results%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
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