complicated query (where statement inside select statement, don'tknow how to explain it) 
July 11th, 2008, 12:25 AM
| | | |
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. | 
July 11th, 2008, 12:35 AM
| | | | 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 | 
July 11th, 2008, 05:45 PM
| | | | 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.
| |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,720 network members.
|