Select Datetime and group by Datetime Query
I have a query where it gets the maximum value per day of the month. What I wanted to do is to also get the time of the result not just the date.
SELECT convert(date,a.time) AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by convert(date,a.time)
Result I get:
The results I get above is correct but I want to also view the time not just the date in the column monthly. The datatype of column time is already datetime. Here's the result if I select just the time. What I want to get is only the maximum data per the day of the month displaying the date and also the time.
SELECT a.time AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by time
sql

add a comment |
I have a query where it gets the maximum value per day of the month. What I wanted to do is to also get the time of the result not just the date.
SELECT convert(date,a.time) AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by convert(date,a.time)
Result I get:
The results I get above is correct but I want to also view the time not just the date in the column monthly. The datatype of column time is already datetime. Here's the result if I select just the time. What I want to get is only the maximum data per the day of the month displaying the date and also the time.
SELECT a.time AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by time
sql

2
provider your sample data and expected output in table format
– fa06
Nov 20 '18 at 5:29
already edited and added some pics
– Miku Hatsune
Nov 20 '18 at 6:11
I want to get is only the maximum data per the day
based on what column ?va
orvb
?
– Squirrel
Nov 20 '18 at 6:26
both columns. @Squirrel
– Miku Hatsune
Nov 20 '18 at 6:29
add a comment |
I have a query where it gets the maximum value per day of the month. What I wanted to do is to also get the time of the result not just the date.
SELECT convert(date,a.time) AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by convert(date,a.time)
Result I get:
The results I get above is correct but I want to also view the time not just the date in the column monthly. The datatype of column time is already datetime. Here's the result if I select just the time. What I want to get is only the maximum data per the day of the month displaying the date and also the time.
SELECT a.time AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by time
sql

I have a query where it gets the maximum value per day of the month. What I wanted to do is to also get the time of the result not just the date.
SELECT convert(date,a.time) AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by convert(date,a.time)
Result I get:
The results I get above is correct but I want to also view the time not just the date in the column monthly. The datatype of column time is already datetime. Here's the result if I select just the time. What I want to get is only the maximum data per the day of the month displaying the date and also the time.
SELECT a.time AS monthly, MAX(a.Va) AS monthlyVA, MAX(a.vb) AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
GROUP by time
sql

sql

edited Nov 20 '18 at 6:10
Miku Hatsune
asked Nov 20 '18 at 5:26


Miku HatsuneMiku Hatsune
164
164
2
provider your sample data and expected output in table format
– fa06
Nov 20 '18 at 5:29
already edited and added some pics
– Miku Hatsune
Nov 20 '18 at 6:11
I want to get is only the maximum data per the day
based on what column ?va
orvb
?
– Squirrel
Nov 20 '18 at 6:26
both columns. @Squirrel
– Miku Hatsune
Nov 20 '18 at 6:29
add a comment |
2
provider your sample data and expected output in table format
– fa06
Nov 20 '18 at 5:29
already edited and added some pics
– Miku Hatsune
Nov 20 '18 at 6:11
I want to get is only the maximum data per the day
based on what column ?va
orvb
?
– Squirrel
Nov 20 '18 at 6:26
both columns. @Squirrel
– Miku Hatsune
Nov 20 '18 at 6:29
2
2
provider your sample data and expected output in table format
– fa06
Nov 20 '18 at 5:29
provider your sample data and expected output in table format
– fa06
Nov 20 '18 at 5:29
already edited and added some pics
– Miku Hatsune
Nov 20 '18 at 6:11
already edited and added some pics
– Miku Hatsune
Nov 20 '18 at 6:11
I want to get is only the maximum data per the day
based on what column ? va
or vb
?– Squirrel
Nov 20 '18 at 6:26
I want to get is only the maximum data per the day
based on what column ? va
or vb
?– Squirrel
Nov 20 '18 at 6:26
both columns. @Squirrel
– Miku Hatsune
Nov 20 '18 at 6:29
both columns. @Squirrel
– Miku Hatsune
Nov 20 '18 at 6:29
add a comment |
3 Answers
3
active
oldest
votes
use row_number()
window function. partition by
dateadd(day, datediff(day, 0, a.time), 0)
will gives you the number by day. Here I am assuming you wanted the row based on the maximum value of Va
follow by Vb
SELECT *
FROM
(
SELECT *,
rn = row_number() over (partition by dateadd(day, datediff(day, 0, a.time), 0)
order by a.Va desc, a.Vb desc)
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
) d
WHERE d.rn = 1
add a comment |
You can try using correlated subquery
SELECT a.time AS monthly, a.Va AS monthlyVA, a.vb AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
and exists
(select 1 from tbl_sample b where convert(date,a.time)=convert(date,b.time)
group by GROUP by convert(date,a.time) having max(b.Va)=a.Va and max(b.Vb)=a.Vb
)
add a comment |
you can simply use CONVERT(datetime,a.time)
in place of CONVERT(date,a.time)
in your query.
if your requirement something different then could you please send some example data, right now i am not much clear.
the column time is already in datetime format but when the result I get is not correct.
– Miku Hatsune
Nov 20 '18 at 6:11
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%2f53386732%2fselect-datetime-and-group-by-datetime-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
use row_number()
window function. partition by
dateadd(day, datediff(day, 0, a.time), 0)
will gives you the number by day. Here I am assuming you wanted the row based on the maximum value of Va
follow by Vb
SELECT *
FROM
(
SELECT *,
rn = row_number() over (partition by dateadd(day, datediff(day, 0, a.time), 0)
order by a.Va desc, a.Vb desc)
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
) d
WHERE d.rn = 1
add a comment |
use row_number()
window function. partition by
dateadd(day, datediff(day, 0, a.time), 0)
will gives you the number by day. Here I am assuming you wanted the row based on the maximum value of Va
follow by Vb
SELECT *
FROM
(
SELECT *,
rn = row_number() over (partition by dateadd(day, datediff(day, 0, a.time), 0)
order by a.Va desc, a.Vb desc)
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
) d
WHERE d.rn = 1
add a comment |
use row_number()
window function. partition by
dateadd(day, datediff(day, 0, a.time), 0)
will gives you the number by day. Here I am assuming you wanted the row based on the maximum value of Va
follow by Vb
SELECT *
FROM
(
SELECT *,
rn = row_number() over (partition by dateadd(day, datediff(day, 0, a.time), 0)
order by a.Va desc, a.Vb desc)
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
) d
WHERE d.rn = 1
use row_number()
window function. partition by
dateadd(day, datediff(day, 0, a.time), 0)
will gives you the number by day. Here I am assuming you wanted the row based on the maximum value of Va
follow by Vb
SELECT *
FROM
(
SELECT *,
rn = row_number() over (partition by dateadd(day, datediff(day, 0, a.time), 0)
order by a.Va desc, a.Vb desc)
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
) d
WHERE d.rn = 1
answered Nov 20 '18 at 6:28
SquirrelSquirrel
11.8k22027
11.8k22027
add a comment |
add a comment |
You can try using correlated subquery
SELECT a.time AS monthly, a.Va AS monthlyVA, a.vb AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
and exists
(select 1 from tbl_sample b where convert(date,a.time)=convert(date,b.time)
group by GROUP by convert(date,a.time) having max(b.Va)=a.Va and max(b.Vb)=a.Vb
)
add a comment |
You can try using correlated subquery
SELECT a.time AS monthly, a.Va AS monthlyVA, a.vb AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
and exists
(select 1 from tbl_sample b where convert(date,a.time)=convert(date,b.time)
group by GROUP by convert(date,a.time) having max(b.Va)=a.Va and max(b.Vb)=a.Vb
)
add a comment |
You can try using correlated subquery
SELECT a.time AS monthly, a.Va AS monthlyVA, a.vb AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
and exists
(select 1 from tbl_sample b where convert(date,a.time)=convert(date,b.time)
group by GROUP by convert(date,a.time) having max(b.Va)=a.Va and max(b.Vb)=a.Vb
)
You can try using correlated subquery
SELECT a.time AS monthly, a.Va AS monthlyVA, a.vb AS monthlyVB
FROM tbl_sample a
WHERE DATEPART(month, a.time) = 10 and DATEPART(year, a.time) = 2018
and exists
(select 1 from tbl_sample b where convert(date,a.time)=convert(date,b.time)
group by GROUP by convert(date,a.time) having max(b.Va)=a.Va and max(b.Vb)=a.Vb
)
answered Nov 20 '18 at 6:28
fa06fa06
11.8k2917
11.8k2917
add a comment |
add a comment |
you can simply use CONVERT(datetime,a.time)
in place of CONVERT(date,a.time)
in your query.
if your requirement something different then could you please send some example data, right now i am not much clear.
the column time is already in datetime format but when the result I get is not correct.
– Miku Hatsune
Nov 20 '18 at 6:11
add a comment |
you can simply use CONVERT(datetime,a.time)
in place of CONVERT(date,a.time)
in your query.
if your requirement something different then could you please send some example data, right now i am not much clear.
the column time is already in datetime format but when the result I get is not correct.
– Miku Hatsune
Nov 20 '18 at 6:11
add a comment |
you can simply use CONVERT(datetime,a.time)
in place of CONVERT(date,a.time)
in your query.
if your requirement something different then could you please send some example data, right now i am not much clear.
you can simply use CONVERT(datetime,a.time)
in place of CONVERT(date,a.time)
in your query.
if your requirement something different then could you please send some example data, right now i am not much clear.
answered Nov 20 '18 at 5:49
DevDev
194
194
the column time is already in datetime format but when the result I get is not correct.
– Miku Hatsune
Nov 20 '18 at 6:11
add a comment |
the column time is already in datetime format but when the result I get is not correct.
– Miku Hatsune
Nov 20 '18 at 6:11
the column time is already in datetime format but when the result I get is not correct.
– Miku Hatsune
Nov 20 '18 at 6:11
the column time is already in datetime format but when the result I get is not correct.
– Miku Hatsune
Nov 20 '18 at 6:11
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%2f53386732%2fselect-datetime-and-group-by-datetime-query%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
2
provider your sample data and expected output in table format
– fa06
Nov 20 '18 at 5:29
already edited and added some pics
– Miku Hatsune
Nov 20 '18 at 6:11
I want to get is only the maximum data per the day
based on what column ?va
orvb
?– Squirrel
Nov 20 '18 at 6:26
both columns. @Squirrel
– Miku Hatsune
Nov 20 '18 at 6:29