Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

SQL Math Question

Thread Tools
 
Search this Thread
 
Old 21 December 2006, 01:55 PM
  #1  
EP82
Scooby Regular
Thread Starter
 
EP82's Avatar
 
Join Date: Feb 2005
Posts: 333
Likes: 0
Received 0 Likes on 0 Posts
Default SQL Math Question

Is it possible to add two figures up that are in the same column as each other?

For instance;

select department, count(*)
from table
where deparment in ('55', '60')
group by department

I now have two rows returned one with a count of how many rows are in department 55 another with a count of how many rows are in department 60.

How do i now add those two figures together?

Any help would be great.

Cheers
Old 21 December 2006, 02:04 PM
  #2  
Sub97
Scooby Regular
 
Sub97's Avatar
 
Join Date: Oct 2002
Posts: 809
Likes: 0
Received 0 Likes on 0 Posts
Default

select count(*)
from table
where department in ('55, '60')
Old 21 December 2006, 04:52 PM
  #3  
EP82
Scooby Regular
Thread Starter
 
EP82's Avatar
 
Join Date: Feb 2005
Posts: 333
Likes: 0
Received 0 Likes on 0 Posts
Default

Infact i worded that wrong, lol..

I need to subtract the two figures.
Old 23 December 2006, 01:04 AM
  #4  
Kevin Greeley
Scooby Regular
 
Kevin Greeley's Avatar
 
Join Date: May 1999
Posts: 779
Likes: 0
Received 0 Likes on 0 Posts
Default

If using Access, you could use:

SELECT SUM(SWITCH(Department=55,1,Department=60,-1))
FROM Table;

For Oracle, change to DECODE function.
For others, maybe the CASE function.
Old 23 December 2006, 01:15 PM
  #5  
Dracoro
Scooby Regular
 
Dracoro's Avatar
 
Join Date: Sep 2001
Location: A powerslide near you
Posts: 10,261
Likes: 0
Received 0 Likes on 0 Posts
Default

SELECT
COUNT(CASE WHEN department = '55' THEN 1 ELSE 0),
COUNT(CASE WHEN department = '60' THEN 1 ELSE 0),
COUNT(CASE WHEN department = '55' THEN 1 ELSE 0) - COUNT(CASE WHEN department = '60' THEN 1 ELSE 0),
COUNT(CASE WHEN department = '60' THEN 1 ELSE 0) - COUNT(CASE WHEN department = '55' THEN 1 ELSE 0)
FROM table

Gives you count of 55, count of 60, count of 55s - 60s and count of 60s - 55s
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
KAS35RSTI
Subaru
27
04 November 2021 07:12 PM
slimwiltaz
General Technical
20
09 October 2015 07:40 PM
IanG1983
Wheels, Tyres & Brakes
2
06 October 2015 03:08 PM
Brzoza
Engine Management and ECU Remapping
1
02 October 2015 05:26 PM
the shreksta
Other Marques
26
01 October 2015 02:30 PM



Quick Reply: SQL Math Question



All times are GMT +1. The time now is 05:28 AM.