2014-10-02 61 views
0

我在sql表中有一列包含3個不同的值;例如:現在,缺席並離開。我想用select來計算它們,並返回不同的計數來反駁它們。我怎樣才能做到這一點。我如何計算一個列的值

回答

3
select columnname, count(*) 
from YourTable 
group by columnName 

select 
sum(case when columnname='present' then =1 end) 'present', 
sum(case when columnname='absent' then =1 end) 'absent', 
sum(case when columnname='leave' then =1 end) 'leave' 
from myTable 
+0

+1沒有比這更簡單! – xQbert 2014-10-02 18:38:19

+0

謝謝,我的選擇語句是:select count(*) from sms.dbo.emp_attendance where username ='admin'AND attendance ='present' group by attendance – Badar 2014-10-02 18:38:49

+0

@Badar由於您是按照出席分組,所以您可能想要將其作爲列添加到您的select語句中,或通過刪除冗餘組。 – 2014-10-02 18:59:20