Yevgeniy Bagackiy
Yevgeniy Bagackiy

Reputation: 974

How to count sum of three columns MySql

I have three columns with names:

 projectNo| process | procLeader | procCheker  |     Stuff     |
----------+---------+------------+-------------+---------------+
 16090001 | ANM     | ben        | barry       | bob, bart, bok| 
 16090001 | BLD     | anton      | kirill      | kart, ali     |

What I want to is to count procLeader, procChecker, stuff columns assigned to projectNo. I managed to count each column by using query:

SELECT 
    COUNT(procLeader) AS `ld`, 
    COUNT(procChecker) AS `ch`, 
    SUM((LENGTH(stuff) - LENGTH(REPLACE(stuff,",","")) + 1)) AS `st` 
FROM `process` 
WHERE projectNo=16090001;

I get

 ld| ch | st |
---+----+----+
  2|   2|   5|

I need something like 'total' table

How I can sum this values? or maybe use another method?

Upvotes: 1

Views: 312

Answers (3)

AT-2017
AT-2017

Reputation: 3149

Use the following query and it worked:

SELECT 
COUNT(procLeader) AS `ld`, COUNT(procChecker) AS `ch`, 
SUM((LENGTH(stuff) - LENGTH(REPLACE(stuff,",","")) + 1)) AS `st` ,
(
   COUNT(procLeader) + 
   COUNT(procChecker) +
   SUM((LENGTH(stuff) - LENGTH(REPLACE(stuff,",","")) + 1)) 
) As `Total` 
FROM `process` WHERE projectNo = 16090001

Output:

Id - ch - st - Total
2  - 2  - 5  - 9

Upvotes: 1

Mr. Bhosale
Mr. Bhosale

Reputation: 3106

SELECT 
COUNT(procLeader) AS `ld`, COUNT(procCheker) AS `ch`, 
SUM((LENGTH(stuff) - LENGTH(REPLACE(stuff,",","")) + 1)) AS `st` ,
(
   COUNT(procLeader) + 
    COUNT(procCheker) +
    SUM((LENGTH(stuff) - LENGTH(REPLACE(stuff,",","")) + 1)) 
 ) As `Total` 
 FROM `process` WHERE projectNo=16090001

Please let us know if you have any concerns or que.

Upvotes: 3

olatarkowska
olatarkowska

Reputation: 41

not sure I understand question, but if you want to count characters, did you try char_length and concat ?

SELECT char_length(concat(procleader, proccheker, REPLACE(stuff,',',''))) FROM process WHERE projectNo=16090001

Upvotes: 0

Related Questions