473,416 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

SQL SUM HAVING Problem

Hi,

I'm trying to write a query, which produces a list of users, whose telephone call costs total over 15.00 for the month, but I'm having difficulty. Here's my query;

Expand|Select|Wrap|Line Numbers
  1. SELECT     userid, username, SUM(netamt)
  2. FROM         EMNCommon
  3. HAVING      (SUM(netamt) >= '15.0000') AND (transdate > GETDATE() - 30)
  4. GROUP BY username
I've tried a few different variations of this, but I'm not getting anywhere. Any help would be much appreciated. Thanks in advance.
Feb 22 '10 #1

✓ answered by gershwyn

What problem are you having specifically?

I think you want transdate > getdate() - 30 in a where clause, not a having clause. You want to restrict the records by date before performing any calculations. Also, the fact that you're including userid but not using it in a group by clause or in an aggregate function might be causing a problem as well.

Finally, you have '15.0000' in quotes, but the sum function is going to return a number. Comparing a number to a string like that results in a data type mismatch error for me.

Try this, and let us know if you're still running into problems.

Expand|Select|Wrap|Line Numbers
  1. SELECT userid, username, SUM(netamt)
  2. FROM EMNCommon
  3. WHERE transdate > GETDATE() - 30
  4. GROUP BY userid, username
  5. HAVING sum(netamt) >= 15;

11 3695
gershwyn
122 100+
What problem are you having specifically?

I think you want transdate > getdate() - 30 in a where clause, not a having clause. You want to restrict the records by date before performing any calculations. Also, the fact that you're including userid but not using it in a group by clause or in an aggregate function might be causing a problem as well.

Finally, you have '15.0000' in quotes, but the sum function is going to return a number. Comparing a number to a string like that results in a data type mismatch error for me.

Try this, and let us know if you're still running into problems.

Expand|Select|Wrap|Line Numbers
  1. SELECT userid, username, SUM(netamt)
  2. FROM EMNCommon
  3. WHERE transdate > GETDATE() - 30
  4. GROUP BY userid, username
  5. HAVING sum(netamt) >= 15;
Feb 22 '10 #2
NeoPa
32,556 Expert Mod 16PB
Nice answer Gershwyn :) You got to the bottom of all the issues I saw, and some I missed.

That should solve pretty well all your problems Pendragon.

Welcome to Bytes!
Feb 23 '10 #3
Thanks for your help guys. That worked a treat. Just need to do some research on how to automate the creation of that report and email it to a user every week. I was thinking of something along the lines of creating a text .sql osql file, a scheduled task and using our own SMTP utility. However it might be easier creating a SQL job and I think SQL has its own in-built email function from a quick search of Help. Just don't have the permissions to check right now.

Thanks again. Much appreciated.
Feb 23 '10 #4
NeoPa
32,556 Expert Mod 16PB
Sounds like you're using MS SQL Server 2000. I know that supports sending notification emails but I haven't seen what it can do on that score from within T-SQL itself. Maybe post for some help in the SQL Server forum. Good luck.
Feb 23 '10 #5
I'm actually using SQL Server 2005, which from what I've read has got better email functionality than previous versions of SQL.

Sorry I've been posting in the wrong forum. Doing a quick search I saw a few SQL questions posted in this Access forum and assumed this was the forum for such posts. I didn't see a SQL forum. Thanks for not flaming me, as I know some sites can be quite militant about such things.
Feb 23 '10 #6
gershwyn
122 100+
No worries. I had wondered it this might be something other than Access (which doesn't have a getdate() function.) You're much more likely get an answer to your automation question in a different forum, though. I can help with SQL queries but I'm no help at all with what you're asking.
Feb 23 '10 #7
NeoPa
32,556 Expert Mod 16PB
@Pendragon
Flaming?!? Gentle guidance I'm sure :D

I hadn't even realised it wasn't Access. As Gershwyn says, we can help to a certain extent (I think in this case it was simple generic SQL), but otherwise it would need to be moved across. I'll move it across anyway now I know, as it is more likely to be found by searchers if in the correct forum.
Feb 23 '10 #8
ck9663
2,878 Expert 2GB
I created some applications that require email notification using sql 2005. This is a good place to start.

Good Luck!!!

~~ CK
Feb 23 '10 #9
Thanks for the link ck9663, it's definitely been of help.
Feb 24 '10 #10
Thanks for helping me produce the query that was originally required. I've been asked to tweak the report by itemising calls by username, for all users who have made telephone calls totalling over 15.00, rather than producing a report grouping and totalling the cost by username. I thought it was a simple case of removing the 'GROUP BY' line or removing 'SUM' from the select line, but different combinations aren't getting me anywhere and I'm a bit out of my SQL depth now. Here's the original query;

Expand|Select|Wrap|Line Numbers
  1. SELECT     userid, username, SUM(cost)
  2. FROM         DATABASE
  3. where (account LIKE 'N%') AND (type ='phone') AND (date >GETDATE()-30) AND (unitid IN ('phone','cell'))
  4. GROUP BY username, userid
  5. HAVING (SUM(cost)>=15)
  6. ORDER BY username
Any assistance would be much appreciated.
Feb 24 '10 #11
NeoPa
32,556 Expert Mod 16PB
For that you need to use a Subquery (See Subqueries in SQL).

The original query would be linked into the original source data (as a Subquery), so you could list all items, but only those where their totals exceed 15.00 currency units.
Feb 25 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: malcolm | last post by:
Hello, We have a small team building a project that involves some 30 or so c# assembly dlls. It is a client server application with 1 exe as the starting point. The dlls and exe are sharing an...
7
by: | last post by:
I fail to understand why that the memory allocated in the void create(int **matrix) does not remain. I passed the address of matrix so shouldn't it still have the allocated memory when it returns...
11
by: Jeff Robichaud | last post by:
Are there any security issues having the ASPNET user account member of Administrators ? Is it a good practice ?
6
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
7
by: Andrew Christiansen | last post by:
Hey everyone. I have Visual Basic .NET 2003 and am trying to show images on a treeview control. I have the imagelist on the form filled with images, and have the ImageList property of the...
0
by: just.starting | last post by:
I am having problem while downloading files from an apache server2.0.53 with php4.3.10.While downloading some files it generally stops after downloading some specific amount and then stops...
3
by: Alok yadav | last post by:
I have an open IP and on that IP our main application is hosted. it uses ajax. in web.config file i have register ajax handlers. there are also other sites or project on that IP. now my problem is...
2
by: ARC | last post by:
Just curious if anyone is having issues with Acc 2007 once the number of objects and complexity increases? I have a fairly large app, with many linked tables, 100's of forms, queries, reports, and...
3
by: =?Utf-8?B?QXhlbCBEYWhtZW4=?= | last post by:
Hi, we've got a strange problem here: We've created an ASP.NET 2.0 web application using Membership.ValidateUser() to manually authenticate users with our website. The problem is: If the...
4
by: muddy22 | last post by:
I'm only having a problem with a small part, i'm fairly new to computer programming and the program i'm using is Dev-C++. The part that i'm having a problem with is this: int WINAPI WinMain...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.