Include the column that holds the information that you need, and include that coulumn in the GROUP BY clause:
If you want an ordered result set, you explicitly have to tell how you want your
result set ordered:
Code:
select Emp_status, count(Emp_status) AS [#]
from emp_att
where Emp_status = 'present' and
date like ('%-jan-2012') and
emp_id = 1
GROUP BY Emp_status
UNION
select Emp_status, count(emp_status)
from emp_att
where emp_status = 'absent' and
emp_id = 1 and
date like ('%-jan-2012' )
GROUP BY Emp_status
ORDER BY Emp_status Desc
This is another way to display the information:
Code:
select SUM(CASE WHEN Emp_status = 'present' THEN 1 ELSE 0 END) As TimesPresent,
SUM(CASE WHEN Emp_status = 'absent' THEN 1 ELSE 0 END) As TimesAbsent
from emp_att
where date like ('%-jan-2012') and
emp_id = 1
This
date like ('%-jan-2012') is twice bad:
- using a reserved word ("date") for a user object (column, table, view, ...)
- storing a date in a CHAR or VARCHAR column.
If you application is not yet in production, correct those two flaws right away. It will become harder and harder to get rid of them the longer your program lives.
A good replacement could be:
Code:
WorkDate DATE NOT NULL,
Rudy (r937), has already suggested this in another thread you have started, and also gave you the reason why it is better.