0

我有一个像这样的考勤表

user_name     |   Col  |   col2   |   Col3  |  Col4 + other columns.

person1       |  a     |  3       | 76      | 56  etc.    ---------> sum of row

person2       |  6     |  72      | a       | 13  etc.    ---------> sum of row

其中'a'表示不存在,所以我需要添加一行中的所有列并显示在最后一列中,即在'total'中,这样'a'应该被忽略并且应该添加一行中剩余列的总和并显示在最后一列的行中

4

3 回答 3

0

我建议你使用 NULL 而不是 'a' 但如果你使用 MySql 你可以使用这样的东西:

SELECT
  user_name, Col, col2, Col3, Col4, Col1+Col2+Col3+Col4
FROM
  yourtable
WHERE 'a' IN (Col, Col2, Col3, Col4)

在此处查看小提琴。

于 2013-03-25T11:08:48.857 回答
0

在mysql中试试这个

     SELECT
      user_name, Col, col2, Col3, Col4, Col+Col2+Col3+Col4 as sum
     FROM
      yourtable

这个在sql server

 SELECT
 user_name, Col, col2, Col3, Col4 ,
 case when col = 'a'
            then cast(col2 as int ) + cast(col3 as int ) +cast(col4 as int )
 when col2 = 'a'
            then cast(col as int ) + cast(col3 as int ) +cast(col4 as int )
 when col3 = 'a'
            then cast(col as int ) + cast(col2 as int ) +cast(col4 as int )
 when col4 = 'a'
            then cast(col as int ) + cast(col2 as int ) +cast(col3 as int )
else  cast(col as int )+ cast(col2 as int ) + cast(col3 as int ) +cast(col4 as int)     
 end    as sum_all

 FROM
  yourtable

 group by user_name , col , col2, col3,col4

在这里演示

于 2013-03-25T11:20:54.350 回答
0

在 MSSQL 中这有效:

SELECT
  user_name, Col, col2, Col3, Col4, 
  CAST(REPLACE(Col, 'a', '0') AS INT)+
  CAST(REPLACE(Col2, 'a', '0') AS INT)+
  CAST(REPLACE(Col3, 'a', '0') AS INT)+
  CAST(REPLACE(Col4, 'a', '0') AS INT)
FROM
  yourtable

在此处查看小提琴

除了 REPLACE,您还可以使用 CASE 语句。

于 2013-03-25T12:46:51.203 回答