473,806 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.TTAm t) + SUM(TTmain.TTFe e) AS 'Total'
FROM
TTmain INNER JOIN TTdealers ON
TTmain.dealerID = TTdealers.deale rID
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.TTAm t) + SUM(TTmain.TTFe e) AS 'Total',
SUM(TTmain.TTAm t) + SUM(TTmain.TTFe e) AS 'Different Total' where
TTdealers.deale rID = 1
FROM
TTmain INNER JOIN TTdealers ON
TTmain.dealerID = TTdealers.deale rID
GROUP BY
DATEPART(year, TTmain.TTDate)

so, the "Total" result is without any conditions, and the "Different
Total" is with a condition of TTdealers.deale rID = 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.
Jul 10 '08 #1
2 3235
You can use the CASE function to filter the total for the selected customer:

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

HTH,

Plamen Ratchev
http://www.SQLStudio.com
Jul 10 '08 #2
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:
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.TTAm t) + SUM(TTmain.TTFe e) AS 'Total'
FROM
TTmain INNER JOIN TTdealers ON
TTmain.dealerID = TTdealers.deale rID
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.TTAm t) + SUM(TTmain.TTFe e) AS 'Total',
SUM(TTmain.TTAm t) + SUM(TTmain.TTFe e) AS 'Different Total' where
TTdealers.deale rID = 1
FROM
TTmain INNER JOIN TTdealers ON
TTmain.dealerID = TTdealers.deale rID
GROUP BY
DATEPART(year, TTmain.TTDate)

so, the "Total" result is without any conditions, and the "Different
Total" is with a condition of TTdealers.deale rID = 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.
Jul 11 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
10170
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
9
3140
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
3
2455
by: Thomas R. Hummel | last post by:
Hi, I was just helping a coworker optimize a query. He had two versions: one which used UNION for each value for which he was tallying results and another query which used GROUP BY. Here is an aproximation of what they were: Query #1: --------- SELECT 12 AS ,
2
8543
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An example of what I'm trying to do is below: update (tbl_ind_mananas LEFT JOIN (select count(*) as count, (dubicacion || zona || manzana) as cod_manzana from tbl_censo_poblacion_1993 group by dubicacion, zona, manzana) tbl1 on relacion = cod_manzana) as...
11
2146
by: 73blazer | last post by:
We are migrating a customer from Version 7.1 FP3, to Version 8.2 (8.1 FP8). For the most part, things are faster, but there is one query that is much much slower, and it is a query that is used all the time. select ATTR1,ATTR2,ATTR3,ATTR4 from physical.part_list where S_PART_NUMBER like '%KJS%' The widlcard before and after seems to be hosing it, but for this particular piece of the application, this type of query is neccessary.
12
2327
by: Joe Stanton | last post by:
Hello Group I have a query that works in Oracle and SQL Server, but fails in Microsoft Access. The query is: SELECT data fromTABLE1 WHERE data>='A&' AND data<'A''' Here is my sample data:
4
1628
by: Matthew Crouch | last post by:
i suck so much that i don't even know if this is a JOIN or a subquery or who-knows what. Here's the idea: I want to select two things at the same time (form one table) average for columnX and average for columnX where columnY=Z so i started of course with select avg(columnX) as avg1, avg(columnX) as avg2 from table where columnY=Z
7
2534
by: nathaniel.k.lee | last post by:
Is it not possible, in IE, to dynamically click a radio button? I'm grabbing some values from a database and using them to populate radio buttons on a page. I have alternate code for Firefox browsers using the setAttribute() function. Everything works as planned in Firefox but in IE, the buttons won't populate and, what's worse, I can't even click on them after everything loads. I see the slight shadow that indicates you're clicking on a...
16
3610
by: Sam Durai | last post by:
Hello, I need to merge a small table (of rows less than 100,sometimes even 0 rows) to a big table (of rows around 4 billion). I used the PK of the big table as merge key but merge does a table scan so it runs for ever. I checked the table and PK statistics of the big table and it looks good. Please let me know if I need to check for something else. Here are more details Small table - Non Partitioned ( Node 0)
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10620
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10372
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.