If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Query to sum 2 Columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-15-11, 06:28
russel russel is offline
Registered User
 
Join Date: Jun 2011
Posts: 7
Query to sum 2 Columns

SELECT Col1 AS A, Col2 AS B, Col3 AS C FROM Table;


Code:
A               B           C          
1               2           3
4               5           6
8               9           5

Need to modify the query to display a fourth column,D which is the sum of first three.

Code:
A               B           C          D
1               2           3          6
4               5           6          15
8               9           5          22
Reply With Quote
  #2 (permalink)  
Old 07-15-11, 06:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT Col1 AS A
     , Col2 AS B
     , Col3 AS C 
     , Col1 + Col2 + Col3 AS D
  FROM daTable
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-15-11, 07:14
russel russel is offline
Registered User
 
Join Date: Jun 2011
Posts: 7
but for col1 i've used ----
Code:
"COUNT(CASE WHEN `contact_status` = 'AAA'
THEN 'whoopee' ELSE NULL END ) AS `A`"
The Query is

Code:
SELECT COUNT(
CASE WHEN `status` = 'AAA'
THEN 'whoopee'
ELSE NULL
END ) AS `A`,
COUNT(
CASE WHEN `status` = 'BBB'
THEN 'whoopee'
ELSE NULL
END ) AS `B`,
COUNT(
CASE WHEN `status` = 'CCC'
THEN 'whoopee'
ELSE NULL
END ) AS `C`,

FROM `Table`

From this i want to display a fourth column,D
Reply With Quote
  #4 (permalink)  
Old 07-15-11, 07:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
wow, that looks awfully familiar

1. why didn't you show the whole query the first time?

2. why can't you apply my solution to your query?

3. are AAA, BBB, CCC the only values in the status column, or are there others?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 07-15-11, 07:31
russel russel is offline
Registered User
 
Join Date: Jun 2011
Posts: 7
I tried many possibilities with SUM(), but didn't get through.

I've only three values in that column
Reply With Quote
  #6 (permalink)  
Old 07-15-11, 07:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by russel View Post
I've only three values in that column
<voice actor="mr. burns">ehhhhhhhxcellent</voice>
Code:
SELECT AgentName 
     , COUNT(CASE WHEN status = 'RPC'
                  THEN 'whoopee'
                  ELSE NULL END) AS `Count of RPC`
     , COUNT(CASE WHEN status = 'BUS'
                  THEN 'whoopee'
                  ELSE NULL END) AS `Count of BUS`
     , COUNT(CASE WHEN status = 'NPC'
                  THEN 'whoopee'
                  ELSE NULL END) AS `Count of NPC`
     , COUNT(*) AS `Count of all three`
  FROM CustomerInfo
GROUP
    BY AgentName
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 07-15-11, 09:10
russel russel is offline
Registered User
 
Join Date: Jun 2011
Posts: 7
what if i've to display count of two more values from another column of the same table i.e;

Code:
A               B           C         D        E         F           G  
1               2           3         5        8         6           14
4               5           6         8        7         15         15
8               9           5         3        9         22         12

where F=A+B+C
AND G=D+E

and D and E belongs to count of values from another column of the same table
Reply With Quote
  #8 (permalink)  
Old 07-15-11, 09:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by russel View Post
what if ...
what if you abandon your As and Bs and Cs, and try writing the query yourself -- i've given you lots of information about the technique

post your attempt here if it doesn't work, but i'm not going to go back to As and Bs and Cs as i did in post #2 which turned out to be counter-productive
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On