473,992 Members | 2,945 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Summary problem

I have an MS Access query that I was hoping to summarize on the Tenure
field but, instead, the rows are created as if the Tenure field were an
Employee ID. So I have duplicate Tenure fields when I thought there
would be just one per different Tenure field with the results summed
within each Tenure. In fact, if I swap out the Tenure field output with
the Employee ID, I get the same results with the exception of the
Employee ID field. I have been trying to really understand the
mechanics of SQL to solve these problems not just to find a quick fix.
This one appears that the "hiredate" field is my problem even though I
am using the DateDiff function to return a result. This is what I think
makes the row think it's an individual record (like a single employee
id) instead of grouping and summing on the Tenure result (such as
">12M"). I just can't figure out how to make it group and sum by
Tenure. Please help! Here is my query:

SELECT
iif(DateDiff("m ",e.HireDate,No w()) is null,"Unknown"
,iif(DateDiff(" m",e.HireDate,N ow()) <= 3,"0-3M"
,iif(DateDiff(" m",e.HireDate,N ow()) > 3 and
DateDiff("m",e. HireDate,Now()) <= 6,"4-6M"
,iif(DateDiff(" m",e.HireDate,N ow()) > 6 and
DateDiff("m",e. HireDate,Now()) <= 9,"7-9M"
,iif(DateDiff(" m",e.HireDate,N ow()) > 9 and
DateDiff("m",e. HireDate,Now()) <= 12,"10-12M"
,iif(DateDiff(" m",e.HireDate,N ow()) > 12,">12M",""))) ))) as [Tenure]
, b.division as [Division]
, b.region as [Region]
, b.district as [District]
, b.rbr as [Cost Center]
, b.name as [Branch]
, sum(iif(a.loanf unddate >= #1/1/2005# and a.loanfunddate <=
#1/31/2005#,1,0)) as [Jan #]
, sum(iif(a.loanf unddate >= #1/1/2005# and a.loanfunddate <=
#1/31/2005#,a.loanamo unt,0)) as [Jan $]
, sum(iif(a.loanf unddate >= #2/1/2005# and a.loanfunddate <=
#2/28/2005#,1,0)) as [Feb #]
, sum(iif(a.loanf unddate >= #2/1/2005# and a.loanfunddate <=
#2/28/2005#,a.loanamo unt,0)) as [Feb $]
, sum(iif(a.loanf unddate >= #3/1/2005# and a.loanfunddate <=
#3/31/2005#,1,0)) as [Mar #]
, sum(iif(a.loanf unddate >= #3/1/2005# and a.loanfunddate <=
#3/31/2005#,a.loanamo unt,0)) as [Mar $]
, sum(iif(a.loanf unddate >= #4/1/2005# and a.loanfunddate <=
#4/30/2005#,1,0)) as [Apr #]
, sum(iif(a.loanf unddate >= #4/1/2005# and a.loanfunddate <=
#4/30/2005#,a.loanamo unt,0)) as [Apr $]
, sum(iif(a.loanf unddate >= #5/1/2005# and a.loanfunddate <=
#5/31/2005#,1,0)) as [May #]
, sum(iif(a.loanf unddate >= #5/1/2005# and a.loanfunddate <=
#5/31/2005#,a.loanamo unt,0)) as [May $]
, sum(iif(a.loanf unddate >= #6/1/2005# and a.loanfunddate <=
#6/30/2005#,1,0)) as [Jun #]
, sum(iif(a.loanf unddate >= #6/1/2005# and a.loanfunddate <=
#6/30/2005#,a.loanamo unt,0)) as [Jun $]
, sum(iif(a.loanf unddate >= #1/1/2005# and a.loanfunddate <=
#6/30/2005#,1,0)) as [YTD #]
, sum(iif(a.loanf unddate >= #1/1/2005# and a.loanfunddate <=
#6/30/2005#,a.loanamo unt,0)) as [YTD $]
FROM (((fundings AS a LEFT JOIN branch AS b ON b.branch = a.branchno)
LEFT JOIN loanofficers AS lo ON lo.loanproducer code =
a.loanproducerc ode)
LEFT JOIN eeqry AS e ON e.empno = lo.empno)
WHERE
a.loanfunddate >= #1/1/2005#
and a.loanfunddate <= #6/30/2005#
and e.terminationda te is null
group by DateDiff("m",e. HireDate,Now()) , b.division, b.region,
b.district, b.rbr, b.name
order by b.district, b.rbr, DateDiff("m",e. HireDate,Now()) ;

A sample output is the following:

Tenure Division Region District Cost Center Branch Jan # Jan $ Feb
# Feb $ Mar # Mar $ Apr # Apr $ May # May $ Jun # Jun $ YTD # YTD $
4-6M C C12 C1212 123 OK 1 $220,000 0 $0 1 $162,000 0 $0 0 $0 0 $0 2 $382,000
7-9M C C12 C1212 123 OK 1 $348,000 0 $0 1 $50,000 0 $0 0 $0 0 $0 2 $398,000
10-12M C C12 C1212 123 OK 0 $0 1 $216,000 0 $0 0 $0 0 $0 0 $0 1 $216,000
12M C C12 C1212 123 OK 0 $0 2 $411,860 4 $598,300 0 $0 0 $0 0 $0 6 $1,120,160
12M C C12 C1212 123 OK 0 $0 4 $593,697 3 $475,385 0 $0 0 $0 0 $0 7 $1,069,082
12M C C12 C1212 123 OK 3 $459,000 17 $3,085,304 5 $662,805 0 $0 0 $0 0 $0 25 $4,207,109
12M C C12 C1212 123 OK 13 $1,392,620 18 $2,450,210 11 $2,272,032 0 $0 0 $0 0 $0 42 $6,114,862


Nov 13 '05 #1
1 1662
I'd say your problem is that you've got the expression DateDiff
("m",e.HireDate ,Now()) in your GROUP BY and ORDER BY clause as well as in
your SELECT clause. Try using [Tenure] in your GROUP BY and ORDER BY fields
and see what that does. If it doesn't help, please post the structure of
the tables and some sample rows so we can debug it more effectively.

--
Message posted via http://www.accessmonster.com
Nov 13 '05 #2

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

Similar topics

0
10228
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
11928
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...
0
11495
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10998
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
10161
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...
0
7712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6666
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5257
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.