2016-07-25 45 views
1

我有表,看起來像這樣如何獲得與行的總數新列值的MySQL

 Date|Col1|Col2|Col3 

01-01-2016|abc | | | 

02-01-2016|xyz |188 | | 

03-01-2016|100 |abc |155 | 

我需要編寫一個查詢,會給我一個新列與列的總數每天的價值。

 Date|Col1|Col2|Col3|count| 

01-01-2016|abc | | | 1 | 

02-01-2016|xyz |188 | | 2 | 

03-01-2016|100 |abc | 155| 3 | 

在我的實際數據中,我有大約30列,我需要指望。

+1

'select *,count(*)from ... group by col1,col2,col3,...,colN' –

+0

使用您的代碼;我在count列上得到的唯一值是1. – dgl

+2

哦,所以03-01-2016會得到3,因爲3列有值?那麼你需要像'select *,(col1 <>'')+(col2 <>'')+等等......' –

回答

0
SELECT *,(IF(col1 IN (NULL,'') , 0, 1)+ IF(col2 IN (NULL,''), 0, 1)+ IF(col3 IN (NULL,''), 0, 1)) as count 
from table_name 
group by date 

的示例表:
enter image description here

輸出:
enter image description here

希望這能解決!

+0

我沒有得到你的輸出。如果正確理解您的查詢方式,它將計算按日期分組的行數,將返回1,因爲每個日期只有1行。 – dgl

+0

@dgl檢查編輯後的解決方案! –