There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
SQL Problem. Heeeelp!!!


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
wango's Avatar
Member with 74 posts.
 
Join Date: Nov 2001
Location: Silver 'Spring
08-Sep-2002, 04:31 PM #1
SQL Problem. Heeeelp!!!
I created the query below in access 2000 and got:

SELECT Contracts.ContractNumber, Contracts.Invoice, Sum(ContractInvoices.InvoiceValue) AS SumOfInvoiceValue,
([SumOfInvoiceValue]/[Invoice])*100 AS PercExpended
FROM Contracts INNER JOIN ContractInvoices ON Contracts.ContractNumber = ContractInvoices.ContractNumber
GROUP BY Contracts.ContractNumber, Contracts.Invoice;

Breeds:

ContractNumber Invoice SumOfInvoiceValue PercExpended
11 added $1,100,000.00 1388666 126.242363636364
5 $500,000.00 1033000 206.6
6 $600,000.00 122500 20.4166666666667
8 $800,000.00 3300 0.4125
9 $900,000.00 364700 40.5222222222222


(by the way, how can I avoid those crazy decimals?)

I want to get only those rows where the %-age is less than 40.
When I insert

“where PercExpended < 40”

to get

SELECT Contracts.ContractNumber, Contracts.Invoice, Sum(ContractInvoices.InvoiceValue) AS SumOfInvoiceValue,
([SumOfInvoiceValue]/[Invoice])*100 AS PercExpended
FROM Contracts INNER JOIN ContractInvoices ON Contracts.ContractNumber = ContractInvoices.ContractNumber where PercExpended < 40
GROUP BY Contracts.ContractNumber, Contracts.Invoice;

I pasted this back in Access query sql window, ran it and I got queried for PercExpended??

Can someone help???
__________________
Wango
TimCottee's Avatar
Senior Member with 186 posts.
 
Join Date: Oct 2001
Location: Southampton, England
09-Sep-2002, 09:25 AM #2
Lets address the second point first, you cannot use a calculated column in a where clause, you have to repeat the calculation itself:

SELECT Contracts.ContractNumber, Contracts.Invoice, Sum(ContractInvoices.InvoiceValue) AS SumOfInvoiceValue,
([SumOfInvoiceValue]/[Invoice])*100 AS PercExpended
FROM Contracts INNER JOIN ContractInvoices ON Contracts.ContractNumber = ContractInvoices.ContractNumber where ([SumOfInvoiceValue]/[Invoice])*100 < 40
GROUP BY Contracts.ContractNumber, Contracts.Invoice;

As to the first, you get these results because of the precision of the stored data, you can simplify this to return integer values or similar if this is more appropriate:

SELECT Contracts.ContractNumber, Contracts.Invoice, Sum(ContractInvoices.InvoiceValue) AS SumOfInvoiceValue,
CInt(([SumOfInvoiceValue]/[Invoice])*100) AS PercExpended
FROM Contracts INNER JOIN ContractInvoices ON Contracts.ContractNumber = ContractInvoices.ContractNumber where CInt(([SumOfInvoiceValue]/[Invoice])*100) < 40
GROUP BY Contracts.ContractNumber, Contracts.Invoice;
__________________
TimCottee
I know a little about a lot of things and a lot about very little.

Brainbench MVP For Visual Basic
http://www.brainbench.com

MCP, MCSD, MCDBA, CPIM

Visit http://flame2006.org.uk or http://www.timcotte.tk
wango's Avatar
Member with 74 posts.
 
Join Date: Nov 2001
Location: Silver 'Spring
09-Sep-2002, 10:09 AM #3
Thanks a million Tim.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:03 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.