Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sum problem

Status
Not open for further replies.

TheJFM

Programmer
Jun 13, 2002
65
GB
Hello people

I have a table that returns the following:

NUM_TOT B_ST
699 W
1240 X
20 Y
780 V
111 B


I need to run a query that will return the sums of NUM_TOT that are grouped by B_ST as follows:

W & X
Y & V
T

Is it possible or would I need to run a sp that will query the table three times?

Thanks

 
Code:
SELECT
  SUM(CASE WHEN b_st IN ('w', 'x') THEN num_tot ELSE 0 END) AS [W & X],
  SUM(CASE WHEN b_st IN ('y', 'v') THEN num_tot ELSE 0 END) AS [Y & V],
  SUM(CASE WHEN b_st = 't' THEN num_tot ELSE 0 END) AS [T]
FROM tablename

--James
 
try this

SELECT
sum(case when d_st in ('W','X') then num_tot else 0 end) as 'W & X',
sum(case when d_st in ('Y','V') then num_tot else 0 end) as 'Y & V',
sum(case when d_st = 'B' then num_tot else 0 end) as 'B'
FROM yourtable
 
Thanks so much. That is great JamesLean
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top