Connecting Tech Pros Worldwide Help | Site Map

complicated query (where statement inside select statement, don'tknow how to explain it)

  #1  
Old July 11th, 2008, 12:25 AM
Joe C.
Guest
 
Posts: n/a
hello, just joined the group, i've run into a wall and am seeking some
help.

here is my query:

SELECT
DATEPART(year, TTmain.TTDate) AS 'Year',
SUM(TTmain.TTAmt) + SUM(TTmain.TTFee) AS 'Total'
FROM
TTmain INNER JOIN TTdealers ON
TTmain.dealerID = TTdealers.dealerID
GROUP BY
DATEPART(year, TTmain.TTDate)

basically, it's summing the values of a column, then grouping by year,
results look like this:

Year | Total
----------------------------
2007 | 194309.9
2008 | 418763.24

i need to include another column, which is a summation of the "Total"
value (SUM(nsfAmt) + SUM(nsfFee) AS 'Total') but filtered based on
customer type.

so, suppose the query would look something like this (this is not
correct, it's for illustration only):

SELECT
DATEPART(year, TTmain.TTDate) AS 'Year',
SUM(TTmain.TTAmt) + SUM(TTmain.TTFee) AS 'Total',
SUM(TTmain.TTAmt) + SUM(TTmain.TTFee) AS 'Different Total' where
TTdealers.dealerID = 1
FROM
TTmain INNER JOIN TTdealers ON
TTmain.dealerID = TTdealers.dealerID
GROUP BY
DATEPART(year, TTmain.TTDate)

so, the "Total" result is without any conditions, and the "Different
Total" is with a condition of TTdealers.dealerID = 1. can the sum of
both values be included on the same row of the result?

i'm not sure if this possible (or if the question i'm posing even
makes sense), but thought to bounce it off the pros on this board.

any help would be greatly appreciated.
  #2  
Old July 11th, 2008, 12:35 AM
Plamen Ratchev
Guest
 
Posts: n/a

re: complicated query (where statement inside select statement, don'tknow how to explain it)


You can use the CASE function to filter the total for the selected customer:

SELECT
DATEPART(year, TTmain.TTDate) AS 'Year',
SUM(TTmain.TTAmt + TTmain.TTFee) AS 'Total',
SUM(CASE WHEN TTdealers.dealerID = 1
THEN TTmain.TTAmt + TTmain.TTFee
ELSE 0
END) AS 'Total Dealer 1'
FROM
TTmain INNER JOIN TTdealers ON
TTmain.dealerID = TTdealers.dealerID
GROUP BY
DATEPART(year, TTmain.TTDate)

HTH,

Plamen Ratchev
http://www.SQLStudio.com
  #3  
Old July 11th, 2008, 05:45 PM
Joe C.
Guest
 
Posts: n/a

re: complicated query (where statement inside select statement, don'tknow how to explain it)


my goodness, how awesome is this right here? IT WORKS PERFECTLY!

thank you SO MUCH!

without this, i was going to finagle multiple results onto one table
dynamically, so this is basically going to save a ton of work and many
lines of code.

thank you VERY MUCH, i am greatly indebted.

On Jul 10, 4:16 pm, "Joe C." <jsk.c...@gmail.comwrote:
Quote:
hello, just joined the group, i've run into a wall and am seeking some
help.
>
here is my query:
>
SELECT
DATEPART(year, TTmain.TTDate) AS 'Year',
SUM(TTmain.TTAmt) + SUM(TTmain.TTFee) AS 'Total'
FROM
TTmain INNER JOIN TTdealers ON
TTmain.dealerID = TTdealers.dealerID
GROUP BY
DATEPART(year, TTmain.TTDate)
>
basically, it's summing the values of a column, then grouping by year,
results look like this:
>
Year | Total
----------------------------
2007 | 194309.9
2008 | 418763.24
>
i need to include another column, which is a summation of the "Total"
value (SUM(nsfAmt) + SUM(nsfFee) AS 'Total') but filtered based on
customer type.
>
so, suppose the query would look something like this (this is not
correct, it's for illustration only):
>
SELECT
DATEPART(year, TTmain.TTDate) AS 'Year',
SUM(TTmain.TTAmt) + SUM(TTmain.TTFee) AS 'Total',
SUM(TTmain.TTAmt) + SUM(TTmain.TTFee) AS 'Different Total' where
TTdealers.dealerID = 1
FROM
TTmain INNER JOIN TTdealers ON
TTmain.dealerID = TTdealers.dealerID
GROUP BY
DATEPART(year, TTmain.TTDate)
>
so, the "Total" result is without any conditions, and the "Different
Total" is with a condition of TTdealers.dealerID = 1. can the sum of
both values be included on the same row of the result?
>
i'm not sure if this possible (or if the question i'm posing even
makes sense), but thought to bounce it off the pros on this board.
>
any help would be greatly appreciated.
Closed Thread