Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum records of a single column with different possibilities?

Tags:

java

sql

mysql

I have an mysql query:

SELECT count(*) as `present_days` 
FROM  tbl_intime_status 
WHERE employee_status = 'Out' and 
      present_status = 'Full Day' and 
      date LIKE '%/"+month2+"/"+year1+"' and 
      employee_id="+ EmpId+

From this query I am getting no. of Full Day present.

I have present_status= 'Half Day' & present_status = 'Full Day' in my database records.

How to count 'Full Day' + 'Half Day'?

like image 294
Harshali Avatar asked Aug 27 '12 06:08

Harshali


5 Answers

If you want the counts separate you can do this

SELECT present_status, count(*) as `present_days` 
FROM  tbl_intime_status 
WHERE employee_status = 'Out' and 
      present_status IN ('Full Day','Half Day')
      date LIKE '%/"+month2+"/"+year1+"' and 
      employee_id="+ EmpId+
GROUP BY present_status

If you want the total of both do this

SELECT count(*) as `present_days` 
FROM  tbl_intime_status 
WHERE employee_status = 'Out' and 
      present_status IN ('Full Day','Half Day')
      date LIKE '%/"+month2+"/"+year1+"' and 
      employee_id="+ EmpId+
like image 191
hol Avatar answered Sep 28 '22 02:09

hol


try, don'tforget to escape the date column since it is a reserved word.

SELECT present_status, count(*) as `present_days` 
FROM   tbl_intime_status 
WHERE  employee_status = 'Out' and 
       present_status IN ('Full Day','Half Day')
       `date` LIKE '%/"+month2+"/"+year1+"' and 
       employee_id = " + EmpId + "
GROUP BY present_status
like image 43
John Woo Avatar answered Sep 28 '22 02:09

John Woo


I want to try this to see if it works:

...

present_status in ('Full Day',  'Half Day' )

...

The grammar can be found here : SQL IN Operator.

like image 24
Richard Avatar answered Sep 28 '22 03:09

Richard


Just add it to your where clause, either with IN:

...
WHERE present_status IN ('Full Day','Half Day')
...

or with OR:

...
WHERE (present_status = 'Full Day' OR present_status = 'Half Day')
...
like image 21
lc. Avatar answered Sep 28 '22 02:09

lc.


Is this what you are looking for:

SELECT count(*) as present_days 
FROM tbl_intime_status 
WHERE employee_status = 'Out' 
and present_status IN ('Full Day', 'Half Day') 
and date LIKE '%/"+month2+"/"+year1+"' 
and employee_id="+ EmpId
like image 23
Faraaz Khan Avatar answered Sep 28 '22 02:09

Faraaz Khan