sql - how to fetch number of records based on changing column value -


i having leave table following

empid       leavedt       appdt      leavetype ------      -------       -------    --------- e1          10-3-13       5-3-13      cl e1          11-3-13       5-3-13      cl e1          12-3-13       5-3-13      cl e1          13-3-13       5-3-13      sl e1          14-3-13       5-3-13      sl e1          15-3-13       5-3-13      pl e1          16-3-13       5-3-13      cl e1          17-3-13       5-3-13      cl e1          18-3-13       5-3-13      pl e1          19-3-13       5-3-13      pl e1          20-3-13       5-3-13      cl 

now want fetch record in want show how many times , how many days employee e1 has taken each type of leave. above example query result should :---

empid                   leavetype -----      -----    ------      --------- e1         10-3-13  12-3-13     cl e1         13-3-13  14-3-13     sl e1         15-3-13  15-3-13     pl e1         16-3-13  17-3-13     cl e1         18-3-13  19-3-13     pl e1         20-3-13  20-3-13     cl 

please me suitable sql query. please note: empid , leavedt primary keys of table.

with small modification table structure below.

     create table leaves (empid varchar(10) ,                   leavefromdt date,                  leavetodate date,                  appdate date,                  leavetype varchar(2)); 

sample data

      insert leaves values('e1', cast('3-10-2013' datetime),         cast('3-12-2013' datetime), cast('3-5-2013' datetime),'cl');        insert leaves values('e1',cast('3-13-2013'as datetime),         cast('3-14-2013'as datetime),cast('5-3-2013'as datetime),'sl');        insert leaves values('e1',cast('3-15-2013'as datetime),         cast('3-15-2013'as datetime),cast('5-3-2013'as datetime),'pl');        insert leaves values('e1',cast('3-16-2013'as datetime),          cast('3-17-2013'as datetime),cast('5-3-2013'as datetime),'cl'); 

query

     select * , datediff(day, leavefromdt,leavetodate )+1 days leaves       order empid,leavetype 

demo sqlfiddle


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -