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 Dec 21, 2006 | 01:55 PM
  #1  
EP82's Avatar
EP82
Thread Starter
Scooby Regular
 
Joined: Feb 2005
Posts: 333
Likes: 0
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
Reply
Old Dec 21, 2006 | 02:04 PM
  #2  
Sub97's Avatar
Sub97
Scooby Regular
 
Joined: Oct 2002
Posts: 809
Likes: 0
Default

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

Infact i worded that wrong, lol..

I need to subtract the two figures.
Reply
Old Dec 23, 2006 | 01:04 AM
  #4  
Kevin Greeley's Avatar
Kevin Greeley
Scooby Regular
25 Year Member
 
Joined: May 1999
Posts: 780
Likes: 2
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.
Reply
Old Dec 23, 2006 | 01:15 PM
  #5  
Dracoro's Avatar
Dracoro
Scooby Regular
 
Joined: Sep 2001
Posts: 10,261
Likes: 0
From: A powerslide near you
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
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
KAS35RSTI
Subaru
27
Nov 4, 2021 07:12 PM
slimwiltaz
General Technical
20
Oct 9, 2015 07:40 PM
IanG1983
Wheels, Tyres & Brakes
2
Oct 6, 2015 03:08 PM
Brzoza
Engine Management and ECU Remapping
1
Oct 2, 2015 05:26 PM
the shreksta
Other Marques
26
Oct 1, 2015 02:30 PM




All times are GMT +1. The time now is 10:45 AM.