Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL -Partition On Weekly Basis

Assume the employee dataset is much larger. And we want to partition on a weekly range the date an employee joined, but only for the last 6 months

How would we do that?

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is expected result.

We see row 1&2 fall in same weekly range and are partitioned together. Row 3 is the only value of that week, so it is by itself as 1. Row 4&5 fall in the same weekly range, therefore they are 2. And row 6 does not fall within a 6 month range, therefore 0.

enter image description here

select id,name,date, count(*) over (partition by Date)
 from employee

>Solution :

use this

select id,name,date, count(*) over (partition by DATEPART(week,Date))
 from employee
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading