SQL Server count records group by 4 weeks in month and location
I want to count total rows of 4 weeks in month and group it by location the results should be like:
location week1 week2 week3 week4
Floor A 10 0 3 4
Floor B 5 2 0 0
...
I've tried this to get a week number by given a date.
CASE WHEN DAY(create_date) >= 1 AND DAY(create_date) < 8 THEN 1
WHEN DAY(create_date) >= 8 AND DAY(create_date) < 15 THEN 2
WHEN DAY(create_date) >= 15 AND DAY(create_date) < 22 THEN 3
ELSE 4 END AS WeekNumber
It's gives me this result:
+----+-----------+--------------+------------+
| id | create_by | create_date | WeekNumber |
+----+-----------+--------------+------------+
| 1 | 555001 | '2018-11-01' | 1 |
| 2 | 555002 | '2018-11-05' | 1 |
| 3 | 555004 | '2018-11-06' | 1 |
| 4 | 555001 | '2018-11-10' | 2 |
| 5 | 555003 | '2018-11-17' | 3 |
| 6 | 555002 | '2018-11-17' | 3 |
| 7 | 555001 | '2018-11-18' | 3 |
| 8 | 555003 | '2018-11-20' | 3 |
| 9 | 555001 | '2018-11-22' | 4 |
| 10 | 555001 | '2018-11-25' | 4 |
+----+-----------+--------------+------------+
My tables
tbl_user
+----+----------+----------+
| id | username | location |
+----+----------+----------+
| 1 | 555001 | Floor A |
| 2 | 555002 | Floor B |
| 3 | 555003 | Floor C |
| 4 | 555004 | Floor A |
| 5 | 555005 | Floor C |
+----+----------+----------+
tbl_list
+----+-----------+--------------+
| id | create_by | create_date |
+----+-----------+--------------+
| 1 | 555001 | '2018-11-01' |
| 2 | 555002 | '2018-11-05' |
| 3 | 555004 | '2018-11-06' |
| 4 | 555001 | '2018-11-10' |
| 5 | 555003 | '2018-11-17' |
| 6 | 555002 | '2018-11-17' |
| 7 | 555001 | '2018-11-18' |
| 8 | 555003 | '2018-11-20' |
| 9 | 555001 | '2018-11-22' |
| 10 | 555001 | '2018-11-25' |
+----+-----------+--------------+
sql sql-server
add a comment |
I want to count total rows of 4 weeks in month and group it by location the results should be like:
location week1 week2 week3 week4
Floor A 10 0 3 4
Floor B 5 2 0 0
...
I've tried this to get a week number by given a date.
CASE WHEN DAY(create_date) >= 1 AND DAY(create_date) < 8 THEN 1
WHEN DAY(create_date) >= 8 AND DAY(create_date) < 15 THEN 2
WHEN DAY(create_date) >= 15 AND DAY(create_date) < 22 THEN 3
ELSE 4 END AS WeekNumber
It's gives me this result:
+----+-----------+--------------+------------+
| id | create_by | create_date | WeekNumber |
+----+-----------+--------------+------------+
| 1 | 555001 | '2018-11-01' | 1 |
| 2 | 555002 | '2018-11-05' | 1 |
| 3 | 555004 | '2018-11-06' | 1 |
| 4 | 555001 | '2018-11-10' | 2 |
| 5 | 555003 | '2018-11-17' | 3 |
| 6 | 555002 | '2018-11-17' | 3 |
| 7 | 555001 | '2018-11-18' | 3 |
| 8 | 555003 | '2018-11-20' | 3 |
| 9 | 555001 | '2018-11-22' | 4 |
| 10 | 555001 | '2018-11-25' | 4 |
+----+-----------+--------------+------------+
My tables
tbl_user
+----+----------+----------+
| id | username | location |
+----+----------+----------+
| 1 | 555001 | Floor A |
| 2 | 555002 | Floor B |
| 3 | 555003 | Floor C |
| 4 | 555004 | Floor A |
| 5 | 555005 | Floor C |
+----+----------+----------+
tbl_list
+----+-----------+--------------+
| id | create_by | create_date |
+----+-----------+--------------+
| 1 | 555001 | '2018-11-01' |
| 2 | 555002 | '2018-11-05' |
| 3 | 555004 | '2018-11-06' |
| 4 | 555001 | '2018-11-10' |
| 5 | 555003 | '2018-11-17' |
| 6 | 555002 | '2018-11-17' |
| 7 | 555001 | '2018-11-18' |
| 8 | 555003 | '2018-11-20' |
| 9 | 555001 | '2018-11-22' |
| 10 | 555001 | '2018-11-25' |
+----+-----------+--------------+
sql sql-server
add a comment |
I want to count total rows of 4 weeks in month and group it by location the results should be like:
location week1 week2 week3 week4
Floor A 10 0 3 4
Floor B 5 2 0 0
...
I've tried this to get a week number by given a date.
CASE WHEN DAY(create_date) >= 1 AND DAY(create_date) < 8 THEN 1
WHEN DAY(create_date) >= 8 AND DAY(create_date) < 15 THEN 2
WHEN DAY(create_date) >= 15 AND DAY(create_date) < 22 THEN 3
ELSE 4 END AS WeekNumber
It's gives me this result:
+----+-----------+--------------+------------+
| id | create_by | create_date | WeekNumber |
+----+-----------+--------------+------------+
| 1 | 555001 | '2018-11-01' | 1 |
| 2 | 555002 | '2018-11-05' | 1 |
| 3 | 555004 | '2018-11-06' | 1 |
| 4 | 555001 | '2018-11-10' | 2 |
| 5 | 555003 | '2018-11-17' | 3 |
| 6 | 555002 | '2018-11-17' | 3 |
| 7 | 555001 | '2018-11-18' | 3 |
| 8 | 555003 | '2018-11-20' | 3 |
| 9 | 555001 | '2018-11-22' | 4 |
| 10 | 555001 | '2018-11-25' | 4 |
+----+-----------+--------------+------------+
My tables
tbl_user
+----+----------+----------+
| id | username | location |
+----+----------+----------+
| 1 | 555001 | Floor A |
| 2 | 555002 | Floor B |
| 3 | 555003 | Floor C |
| 4 | 555004 | Floor A |
| 5 | 555005 | Floor C |
+----+----------+----------+
tbl_list
+----+-----------+--------------+
| id | create_by | create_date |
+----+-----------+--------------+
| 1 | 555001 | '2018-11-01' |
| 2 | 555002 | '2018-11-05' |
| 3 | 555004 | '2018-11-06' |
| 4 | 555001 | '2018-11-10' |
| 5 | 555003 | '2018-11-17' |
| 6 | 555002 | '2018-11-17' |
| 7 | 555001 | '2018-11-18' |
| 8 | 555003 | '2018-11-20' |
| 9 | 555001 | '2018-11-22' |
| 10 | 555001 | '2018-11-25' |
+----+-----------+--------------+
sql sql-server
I want to count total rows of 4 weeks in month and group it by location the results should be like:
location week1 week2 week3 week4
Floor A 10 0 3 4
Floor B 5 2 0 0
...
I've tried this to get a week number by given a date.
CASE WHEN DAY(create_date) >= 1 AND DAY(create_date) < 8 THEN 1
WHEN DAY(create_date) >= 8 AND DAY(create_date) < 15 THEN 2
WHEN DAY(create_date) >= 15 AND DAY(create_date) < 22 THEN 3
ELSE 4 END AS WeekNumber
It's gives me this result:
+----+-----------+--------------+------------+
| id | create_by | create_date | WeekNumber |
+----+-----------+--------------+------------+
| 1 | 555001 | '2018-11-01' | 1 |
| 2 | 555002 | '2018-11-05' | 1 |
| 3 | 555004 | '2018-11-06' | 1 |
| 4 | 555001 | '2018-11-10' | 2 |
| 5 | 555003 | '2018-11-17' | 3 |
| 6 | 555002 | '2018-11-17' | 3 |
| 7 | 555001 | '2018-11-18' | 3 |
| 8 | 555003 | '2018-11-20' | 3 |
| 9 | 555001 | '2018-11-22' | 4 |
| 10 | 555001 | '2018-11-25' | 4 |
+----+-----------+--------------+------------+
My tables
tbl_user
+----+----------+----------+
| id | username | location |
+----+----------+----------+
| 1 | 555001 | Floor A |
| 2 | 555002 | Floor B |
| 3 | 555003 | Floor C |
| 4 | 555004 | Floor A |
| 5 | 555005 | Floor C |
+----+----------+----------+
tbl_list
+----+-----------+--------------+
| id | create_by | create_date |
+----+-----------+--------------+
| 1 | 555001 | '2018-11-01' |
| 2 | 555002 | '2018-11-05' |
| 3 | 555004 | '2018-11-06' |
| 4 | 555001 | '2018-11-10' |
| 5 | 555003 | '2018-11-17' |
| 6 | 555002 | '2018-11-17' |
| 7 | 555001 | '2018-11-18' |
| 8 | 555003 | '2018-11-20' |
| 9 | 555001 | '2018-11-22' |
| 10 | 555001 | '2018-11-25' |
+----+-----------+--------------+
sql sql-server
sql sql-server
asked Nov 21 '18 at 7:48
Vintage BeefVintage Beef
154112
154112
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can try using pivot
with cte as
(
select *,location,
CASE WHEN DAY(create_date) >= 1 AND DAY(create_date) < 8 THEN 1
WHEN DAY(create_date) >= 8 AND DAY(create_date) < 15 THEN 2
WHEN DAY(create_date) >= 15 AND DAY(create_date) < 22 THEN 3
ELSE 4 END AS WeekNumber
from tbl_list a left join tbl_user b on a.create_by=b.username
)
select * from
(select * from cte
pivot
(
count(create_by) for WeekNumber in ([1],[2],[3],[4])
) as pv)A left join
(select location,count(username) countofUser from tbl_user group by location)B on
A.location=B.location
I got this error msg "Incorrect syntax near '1'"
– Vintage Beef
Nov 21 '18 at 8:11
As a CASE stops when the first TRUE condition is met (and there's no day 0), all the... >= ... AND
can be simply removed
– dnoeth
Nov 21 '18 at 8:22
Thank you so much. One more question How do I count numbers of user like Floor A has 2 users that created list data Floor B 1 user and Floor C 1
– Vintage Beef
Nov 21 '18 at 8:29
@VintageBeef, updated the answer for count user location wise - you can check
– fa06
Nov 21 '18 at 8:36
add a comment |
I would simply use conditional aggregation:
select u.location,
sum(case when day(l.create_date) >= 1 and day(l.create_date) < 8 then 1 else 0
end) as week1,
sum(case when day(l.create_date) >= 8 and day(l.create_date) < 15 then 1 else 0
end) as week2,
sum(case when day(l.create_date) >= 15 and day(l.create_date) < 22 then 1 else 0
end) as week3,
sum(case when day(l.create_date) >= 22 and day(l.create_date) < 29 then 1 else 0
end) as week4
from tbl_list l join
tbl_user u
on l.create_by = u.username
group by u.location;
This seems much, much simpler than attempting to use pivot
. Subqueries are simply not needed for this logic.
Thank you so much. I found the same as your by myself too.
– Vintage Beef
Nov 29 '18 at 11:15
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%2f53407387%2fsql-server-count-records-group-by-4-weeks-in-month-and-location%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
You can try using pivot
with cte as
(
select *,location,
CASE WHEN DAY(create_date) >= 1 AND DAY(create_date) < 8 THEN 1
WHEN DAY(create_date) >= 8 AND DAY(create_date) < 15 THEN 2
WHEN DAY(create_date) >= 15 AND DAY(create_date) < 22 THEN 3
ELSE 4 END AS WeekNumber
from tbl_list a left join tbl_user b on a.create_by=b.username
)
select * from
(select * from cte
pivot
(
count(create_by) for WeekNumber in ([1],[2],[3],[4])
) as pv)A left join
(select location,count(username) countofUser from tbl_user group by location)B on
A.location=B.location
I got this error msg "Incorrect syntax near '1'"
– Vintage Beef
Nov 21 '18 at 8:11
As a CASE stops when the first TRUE condition is met (and there's no day 0), all the... >= ... AND
can be simply removed
– dnoeth
Nov 21 '18 at 8:22
Thank you so much. One more question How do I count numbers of user like Floor A has 2 users that created list data Floor B 1 user and Floor C 1
– Vintage Beef
Nov 21 '18 at 8:29
@VintageBeef, updated the answer for count user location wise - you can check
– fa06
Nov 21 '18 at 8:36
add a comment |
You can try using pivot
with cte as
(
select *,location,
CASE WHEN DAY(create_date) >= 1 AND DAY(create_date) < 8 THEN 1
WHEN DAY(create_date) >= 8 AND DAY(create_date) < 15 THEN 2
WHEN DAY(create_date) >= 15 AND DAY(create_date) < 22 THEN 3
ELSE 4 END AS WeekNumber
from tbl_list a left join tbl_user b on a.create_by=b.username
)
select * from
(select * from cte
pivot
(
count(create_by) for WeekNumber in ([1],[2],[3],[4])
) as pv)A left join
(select location,count(username) countofUser from tbl_user group by location)B on
A.location=B.location
I got this error msg "Incorrect syntax near '1'"
– Vintage Beef
Nov 21 '18 at 8:11
As a CASE stops when the first TRUE condition is met (and there's no day 0), all the... >= ... AND
can be simply removed
– dnoeth
Nov 21 '18 at 8:22
Thank you so much. One more question How do I count numbers of user like Floor A has 2 users that created list data Floor B 1 user and Floor C 1
– Vintage Beef
Nov 21 '18 at 8:29
@VintageBeef, updated the answer for count user location wise - you can check
– fa06
Nov 21 '18 at 8:36
add a comment |
You can try using pivot
with cte as
(
select *,location,
CASE WHEN DAY(create_date) >= 1 AND DAY(create_date) < 8 THEN 1
WHEN DAY(create_date) >= 8 AND DAY(create_date) < 15 THEN 2
WHEN DAY(create_date) >= 15 AND DAY(create_date) < 22 THEN 3
ELSE 4 END AS WeekNumber
from tbl_list a left join tbl_user b on a.create_by=b.username
)
select * from
(select * from cte
pivot
(
count(create_by) for WeekNumber in ([1],[2],[3],[4])
) as pv)A left join
(select location,count(username) countofUser from tbl_user group by location)B on
A.location=B.location
You can try using pivot
with cte as
(
select *,location,
CASE WHEN DAY(create_date) >= 1 AND DAY(create_date) < 8 THEN 1
WHEN DAY(create_date) >= 8 AND DAY(create_date) < 15 THEN 2
WHEN DAY(create_date) >= 15 AND DAY(create_date) < 22 THEN 3
ELSE 4 END AS WeekNumber
from tbl_list a left join tbl_user b on a.create_by=b.username
)
select * from
(select * from cte
pivot
(
count(create_by) for WeekNumber in ([1],[2],[3],[4])
) as pv)A left join
(select location,count(username) countofUser from tbl_user group by location)B on
A.location=B.location
edited Nov 21 '18 at 8:35
answered Nov 21 '18 at 7:54
fa06fa06
17k21018
17k21018
I got this error msg "Incorrect syntax near '1'"
– Vintage Beef
Nov 21 '18 at 8:11
As a CASE stops when the first TRUE condition is met (and there's no day 0), all the... >= ... AND
can be simply removed
– dnoeth
Nov 21 '18 at 8:22
Thank you so much. One more question How do I count numbers of user like Floor A has 2 users that created list data Floor B 1 user and Floor C 1
– Vintage Beef
Nov 21 '18 at 8:29
@VintageBeef, updated the answer for count user location wise - you can check
– fa06
Nov 21 '18 at 8:36
add a comment |
I got this error msg "Incorrect syntax near '1'"
– Vintage Beef
Nov 21 '18 at 8:11
As a CASE stops when the first TRUE condition is met (and there's no day 0), all the... >= ... AND
can be simply removed
– dnoeth
Nov 21 '18 at 8:22
Thank you so much. One more question How do I count numbers of user like Floor A has 2 users that created list data Floor B 1 user and Floor C 1
– Vintage Beef
Nov 21 '18 at 8:29
@VintageBeef, updated the answer for count user location wise - you can check
– fa06
Nov 21 '18 at 8:36
I got this error msg "Incorrect syntax near '1'"
– Vintage Beef
Nov 21 '18 at 8:11
I got this error msg "Incorrect syntax near '1'"
– Vintage Beef
Nov 21 '18 at 8:11
As a CASE stops when the first TRUE condition is met (and there's no day 0), all the
... >= ... AND
can be simply removed– dnoeth
Nov 21 '18 at 8:22
As a CASE stops when the first TRUE condition is met (and there's no day 0), all the
... >= ... AND
can be simply removed– dnoeth
Nov 21 '18 at 8:22
Thank you so much. One more question How do I count numbers of user like Floor A has 2 users that created list data Floor B 1 user and Floor C 1
– Vintage Beef
Nov 21 '18 at 8:29
Thank you so much. One more question How do I count numbers of user like Floor A has 2 users that created list data Floor B 1 user and Floor C 1
– Vintage Beef
Nov 21 '18 at 8:29
@VintageBeef, updated the answer for count user location wise - you can check
– fa06
Nov 21 '18 at 8:36
@VintageBeef, updated the answer for count user location wise - you can check
– fa06
Nov 21 '18 at 8:36
add a comment |
I would simply use conditional aggregation:
select u.location,
sum(case when day(l.create_date) >= 1 and day(l.create_date) < 8 then 1 else 0
end) as week1,
sum(case when day(l.create_date) >= 8 and day(l.create_date) < 15 then 1 else 0
end) as week2,
sum(case when day(l.create_date) >= 15 and day(l.create_date) < 22 then 1 else 0
end) as week3,
sum(case when day(l.create_date) >= 22 and day(l.create_date) < 29 then 1 else 0
end) as week4
from tbl_list l join
tbl_user u
on l.create_by = u.username
group by u.location;
This seems much, much simpler than attempting to use pivot
. Subqueries are simply not needed for this logic.
Thank you so much. I found the same as your by myself too.
– Vintage Beef
Nov 29 '18 at 11:15
add a comment |
I would simply use conditional aggregation:
select u.location,
sum(case when day(l.create_date) >= 1 and day(l.create_date) < 8 then 1 else 0
end) as week1,
sum(case when day(l.create_date) >= 8 and day(l.create_date) < 15 then 1 else 0
end) as week2,
sum(case when day(l.create_date) >= 15 and day(l.create_date) < 22 then 1 else 0
end) as week3,
sum(case when day(l.create_date) >= 22 and day(l.create_date) < 29 then 1 else 0
end) as week4
from tbl_list l join
tbl_user u
on l.create_by = u.username
group by u.location;
This seems much, much simpler than attempting to use pivot
. Subqueries are simply not needed for this logic.
Thank you so much. I found the same as your by myself too.
– Vintage Beef
Nov 29 '18 at 11:15
add a comment |
I would simply use conditional aggregation:
select u.location,
sum(case when day(l.create_date) >= 1 and day(l.create_date) < 8 then 1 else 0
end) as week1,
sum(case when day(l.create_date) >= 8 and day(l.create_date) < 15 then 1 else 0
end) as week2,
sum(case when day(l.create_date) >= 15 and day(l.create_date) < 22 then 1 else 0
end) as week3,
sum(case when day(l.create_date) >= 22 and day(l.create_date) < 29 then 1 else 0
end) as week4
from tbl_list l join
tbl_user u
on l.create_by = u.username
group by u.location;
This seems much, much simpler than attempting to use pivot
. Subqueries are simply not needed for this logic.
I would simply use conditional aggregation:
select u.location,
sum(case when day(l.create_date) >= 1 and day(l.create_date) < 8 then 1 else 0
end) as week1,
sum(case when day(l.create_date) >= 8 and day(l.create_date) < 15 then 1 else 0
end) as week2,
sum(case when day(l.create_date) >= 15 and day(l.create_date) < 22 then 1 else 0
end) as week3,
sum(case when day(l.create_date) >= 22 and day(l.create_date) < 29 then 1 else 0
end) as week4
from tbl_list l join
tbl_user u
on l.create_by = u.username
group by u.location;
This seems much, much simpler than attempting to use pivot
. Subqueries are simply not needed for this logic.
answered Nov 21 '18 at 12:24
Gordon LinoffGordon Linoff
788k35312417
788k35312417
Thank you so much. I found the same as your by myself too.
– Vintage Beef
Nov 29 '18 at 11:15
add a comment |
Thank you so much. I found the same as your by myself too.
– Vintage Beef
Nov 29 '18 at 11:15
Thank you so much. I found the same as your by myself too.
– Vintage Beef
Nov 29 '18 at 11:15
Thank you so much. I found the same as your by myself too.
– Vintage Beef
Nov 29 '18 at 11:15
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%2f53407387%2fsql-server-count-records-group-by-4-weeks-in-month-and-location%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