0

I have Leave Entries table like this:

emp_id   LeaveTypeSName    LeaveStatus   FromDate            ToDate
----------------------------------------------------------------------
 1            CL             Full Day      21/07/2014       21/07/2014
 1            SL             Half Day      21/07/2014       21/07/2014
 1            CL             Full Day      21/07/2014       21/07/2014
 1            SL             Full Day      21/07/2014       21/07/2014
 1            CL             Half Day      21/07/2014       21/07/2014
 1            CL             Full Day      21/07/2014       24/07/2014

I am trying get result like this...

 CL_count      SL_count
 -----------------------
    6.5           1.5

My SQL query is

SELECT     
   no_dyas = SUM( DATEDIFF(d, FromDate, ToDate)),
   CL_COUNT = SUM(CASE WHEN LeaveTypeSName='CL' AND LeaveStatus='FullDay'  THEN 1 WHEN LeaveTypeSName='CL' AND LeaveStatus='HalfDay' THEN 0.5  END),
   SL_COUNT = SUM(CASE WHEN LeaveTypeSName='SL' AND LeaveStatus='FullDay' THEN 1 WHEN LeaveTypeSName='SL' AND LeaveStatus='HalfDay' THEN 0.5  END),
   FH_COUNT = SUM(CASE WHEN LeaveTypeSName='FH' AND LeaveStatus='FullDay' THEN 1 WHEN  LeaveTypeSName='FH' AND LeaveStatus='HalfDay' THEN 0.5  END),
   LOP_COUNT = SUM(CASE WHEN LeaveTypeSName='LOP' AND LeaveStatus='FullDay' THEN 1 WHEN LeaveTypeSName='LOP' AND LeaveStatus='HalfDay' THEN 0.5  END)   
FROM 
   LeaveEntries 
WHERE
   EmployeeCode = '1'

Please help me.....

4
  • What is the output you are getting? Commented Jul 23, 2014 at 5:04
  • here i am only getting count...like CL=4 and SL=2 Commented Jul 23, 2014 at 5:04
  • Try using 1.0 instead of 1. Also add else part (ELSE 0.0). Commented Jul 23, 2014 at 5:05
  • Are you getting CL=4 or CL=6.5? What is the actual business logic to calculate these values? Commented Jul 23, 2014 at 5:10

2 Answers 2

2

Please try:

select 
   LeaveTypeSName,
   SUM(case when LeaveStatus='Full Day' then 1 else 0.5 end)
From 
   LeaveEntries 
where emp_id=1
group by LeaveTypeSName

To get the sum of leave days, try:

select LeaveTypeSName,
  SUM(case when LeaveStatus='Full Day' then DATEDIFF(day, FromDate, ToDate)+1 else 0.5 end)
From 
  LeaveEntries 
where emp_id=1
group by LeaveTypeSName
Sign up to request clarification or add additional context in comments.

4 Comments

it should also include the Leaves No ..for a eg...in last row in theleave entries it shows that number of CL is 4
@DileepKumar Please add the same in expected output.
i want LeaveTypesName count.
If you want to add LeaveTypesName count then add COUNT(*) to the select statement
0

this worked for me!!!

SELECT     
  no_dyas = SUM( DATEDIFF(d, FromDate, ToDate)),
  CL_COUNT = SUM(CASE WHEN LeaveTypeSName='CL' AND LeaveStatus='Full Day'  THEN 1 WHEN LeaveTypeSName='CL' AND LeaveStatus='Half Day' THEN 0.5  END),
  SL_COUNT = SUM(CASE WHEN LeaveTypeSName='SL' AND LeaveStatus='Full Day' THEN 1 WHEN LeaveTypeSName='SL' AND LeaveStatus='Half Day' THEN 0.5  END),
  FH_COUNT =SUM(CASE WHEN LeaveTypeSName='FH' AND LeaveStatus='Full Day' THEN 1 WHEN  LeaveTypeSName='FH' AND LeaveStatus='Half Day' THEN 0.5  END),
  LOP_COUNT =SUM(CASE WHEN LeaveTypeSName='LOP' AND LeaveStatus='Full Day' THEN 1 WHEN LeaveTypeSName='LOP' AND LeaveStatus='Half Day' THEN 0.5  END)   
FROM 
  Leave_Entries 
WHERE    emp_id = 1

it's result is true... 3.5 and 1.5
I just changed the 'HalfDay' to 'Half Day' And 'FullDay' to 'Full Day' according to your data in your query and done...

3 Comments

This doen't regard the timespan (FromDate, ToDate)
yes but isn't important, you can add it and it will work, it worked for me correctly without any problem, (my post edited)
But the result should be 6.5 and 1.5, like described by Dileep.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.