tsql - SQL Server to count how many times a value appears between multiple date ranges -


i have table 2 columns in question.

  • column - timestamp
  • column b - empid

what i'm trying compare weeks see how many times empid has repeated. example:

week1 base (starting date range e.g. between '2017-07-22' , '2017-07-29 23:59:59.993'). want compare week2 against week1. if empid repeats in week2 see count of 2 , if appears first time in week2 count of 1.

moving on week3. if empid appears in week1, week2 , week3 want see count of 3, if appears in week2 , week3 count of 2 , if appears in week3 count of 1.

and week4. if empid appears in week1, week2, week3 , week4 want see count of 4. if empid appears in week2, week3 , week4 count of 3, if appears in week3 , week4 count of 2 , if appears first time in week4 count of 1.

any appreciated.


added i've tried far not getting desired results.

select t.emp_id, (select count(emp_id)  [vacation audit care 2017] ((upload_date between '2017-07-22' , '2017-07-29 23:59:59.993') or(upload_date between '2017-07-29' , '2017-08-05 23:59:59.993') or (upload_date between '2017-08-12' , '2017-08-19 23:59:59.993')) , emp_id=t.emp_id) counts [vacation audit care 2017] t group t.emp_id order counts desc 

sample data

╔═════════════════╦═════════╗ ║ time_stamp      ║ emp_id  ║ ║ 7/20/2017 19:40 ║ 3140340 ║ ║ 7/20/2017 19:40 ║ 2000950 ║ ║ 7/20/2017 19:40 ║ 3118410 ║ ║ 7/20/2017 19:40 ║ 311840  ║ ║ 7/23/2017 21:19 ║ 3140340 ║ ║ 7/23/2017 21:19 ║ 2000950 ║ ║ 7/23/2017 21:19 ║ 3118410 ║ ║ 7/23/2017 21:19 ║ 3124160 ║ ║ 7/30/2017 7:00  ║ 3140340 ║ ║ 7/30/2017 7:00  ║ 2000950 ║ ║ 7/30/2017 7:00  ║ 3118410 ║ ║ 7/30/2017 7:00  ║ 311840  ║ ║ 8/6/2017 12:00  ║ 3140340 ║ ║ 8/6/2017 12:00  ║ 3118410 ║ ║ 8/6/2017 12:00  ║ 3124160 ║ ║ 8/6/2017 12:00  ║ 311840  ║ ║ 8/13/2017 12:00 ║ 3140340 ║ ║ 8/13/2017 12:00 ║ 3118410 ║ ║ 8/13/2017 12:00 ║ 3124160 ║ ║ 8/13/2017 12:00 ║ 311840  ║ ╚═════════════════╩═════════╝ 

expected output

╔═════════╦════════╦═══════╦════════╗ ║         ║ 30-jul ║ 6-aug ║ 13-aug ║ ║ emp_id  ║ wk2    ║ wk3   ║ wk4    ║ ║ 3140340 ║ 2      ║ 3     ║ 4      ║ ║ 2000950 ║ 2      ║       ║        ║ ║ 3118410 ║ 2      ║ 3     ║ 4      ║ ║ 311840  ║ 1      ║ 2     ║ 3      ║ ║ 3124160 ║        ║ 1     ║ 2      ║ ╚═════════╩════════╩═══════╩════════╝ 

as per expected output using 7/23 base (week1) table shows expected data results. in week2 311840 appears first time expect count of 1.

the following week (8/6) 311840 appears once in wk2 , once in wk3 expect count of 2 while 3124160 appears first time expect count of 1 etc.

this give results in format show in sample. i've included week before , after in output. remove weeks remove [29] , [33] pivot. comment out pivot line underlying data.

;with  weekcounts (     select time_stamp, emp_id, datepart(week, time_stamp) int_week sampledata )  ,counts (     select emp_id, int_week, 1 int_count     weekcounts     union     select weekcounts.emp_id, weekcounts.int_week, 1 int_count     weekcounts         inner join counts             on weekcounts.emp_id = counts.emp_id             , (weekcounts.int_week - 1) = counts.int_week )  ,countsagg (     select emp_id, int_week, sum(int_count) int_count     counts     group emp_id, int_week )  select * countsagg pivot (max(int_count) int_week in ([29],[30],[31],[32],[33])) piv 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -