I am pulling my hair out on this! Seems like it should be easy, I just can not get it to work like I want. I know I am doing something wrong, so I hope someone can be so kind to guide me!!!
I have 2 tables: Table1 - columns:
Period
FuelType Table2 - columns:
Period
FuelType
Miles
Gallons
Item - (values, 'F' for fuel, 'M' for miles)
AdjustmentAmount
I am joining the 2 tables on Period & FuelType columns. For each Period within a FuelType I want to sum the miles & gallons.(this is working correctly). I also want to sum the AdjustmentAmount column so I have created an expression for that. The problem is if I put teh aggregate function 'Sum' on this expression instead of 'group by' I get the error "Trying to execute a query that deos not include the specified expression as part of an aggregate function." The expression I created looks at the value in column 'Item' and sums based on the value either 'F' or 'M'. My query runs as is, however, when I change "(IIf([Item]='M',[Adjustment],Null)) AS MilesAdj" to "SUM((IIf([Item]='M',[Adjustment],Null)) AS MilesAdj)", I get the error. Here is my sql statement:
3 12443
On my previous post, not enough room to include my sql statement, here it is: - SELECT DISTINCTROW Table1.Period, Table1.FuelType, Sum(ITable1.TaxableMiles) AS RMiles, Sum(Table1.PaidGallons) AS RFuel, (IIf([Item]='M',[Adjustment],Null)) AS MilesAdj, IIf(Item='F',Adjustment,Null) AS GalsAdj
-
FROM Table1 INNER JOIN Table2 ON (Table1.Period = Table2.Period) AND (Table1.FuelType = Table2.FuelType)
-
GROUP BY Table1.Period, Table1.FuelType, Table2.Item, Table2.Adjustment
MMcCarthy 14,534
Recognized Expert Moderator MVP
You have posted this in the Articles section. I am moving it to the Access forum.
ADMIN
NeoPa 32,556
Recognized Expert Moderator MVP - SELECT DISTINCTROW Table1.Period,
-
Table1.FuelType,
-
Sum(ITable1.TaxableMiles) AS RMiles,
-
Sum(Table1.PaidGallons) AS RFuel,
-
(IIf([Item]='M',[Adjustment],Null)) AS MilesAdj,
-
IIf(Item='F',Adjustment,Null) AS GalsAdj
-
FROM Table1 INNER JOIN Table2
-
ON (Table1.Period = Table2.Period)
-
AND (Table1.FuelType = Table2.FuelType)
-
GROUP BY Table1.Period,
-
Table1.FuelType,
-
Table2.Item,
-
Table2.Adjustment
Clearly you typed this in rather than using Copy/Paste.
That is the cause of so much wasted time.
I've re-arranged the SQL so that it's in code tags and isn't a large mess of gobledegook. Now it is clear that the third line (in my version) has an extraneous 'I' before the table name.
Some things to start with : - Change ITable1 to Table1.
- Lose DISTINCTROW predicate. Very good to know about, but inappropriate in a GROUP BY query.
- Think about what you should actually be GROUPing by. Remember, the Sum() results are determined by this.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Paul M |
last post by:
I encountered the following error when trying to perform a SQL UPDATE to a
MySQL database table from Python.
I would apprciate any assistance. In the Python code I have tried integer
and decimal...
|
by: David Berry |
last post by:
Hi All. I have a SQL Statement on an ASP page that only returns 4 records. When I run it in SQL Server or Query Analyzer it runs in less than a second. When I run it from my ASP page I get:
...
|
by: Morten Gulbrandsen |
last post by:
C:\mysql\bin>mysql -u elmasri -pnavathe company
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29 to server version: 4.1.0-alpha-max-debug
Type...
|
by: Matias Silva |
last post by:
Hi Everyone, I wrote a for loop to build several select
statements that are combined with a UNION. When I execute
one of the queries separately, it works, but when I execute
the query with a UNION...
|
by: jj |
last post by:
I've got a form button that fires off 3 queries but if the first query
returns an error, I don't want the other two queries to happen.
Example: first query runs an insert from a linked table but...
| |
by: RC |
last post by:
In my Access 2002 form, I have a combo box and on the AfterUpdate
event I use
DoCmd.RunSQL ("UPDATE ....
to update records in a table. When it starts to run I get a message
"You are about to...
|
by: sara |
last post by:
I am getting "Type Mismatch Error" when the following code executes. I
am trying to notify the user if she attempts to add a customer with the
same FirstName, LastName, Address(line1) and City as...
|
by: arun |
last post by:
Query is too complex
--------------------------------------------------------------------------------
Hi, I was trying to solve this problem since last two days but couldn't
find any solution.
...
|
by: Akinyemi |
last post by:
I created a Database which I named "Address".
I went through the Control Panel and created a DSN to enable me connect to the Database through ODBC.
I then created a Form with the same fields as...
|
by: Tonio Tanzi |
last post by:
I have the following problem in a Win 2000 Server + SQL Server 2000
environment and I hope somewhat can help me to resolve it (after many
days of useless attempts I am desperate).
In my database...
|
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...
| |
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...
|
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,...
|
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: 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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |