Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 10th, 2008, 11:25 PM
Joe C.
Guest
 
Posts: n/a
Default complicated query (where statement inside select statement, don'tknow how to explain it)

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 10th, 2008, 11:35 PM
Plamen Ratchev
Guest
 
Posts: n/a
Default Re: complicated query (where statement inside select statement, don't know 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, 04:45 PM
Joe C.
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

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 220,989 network members.