473,413 Members | 1,856 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,413 software developers and data experts.

SQL: join and exists are combined

5
I've got 3 tables. The irrelevant columns are omitted.
Table1:Items
ID ItemsName
1 Food
2 Beverage
Table2:Invoice
ID User Amount
1 1 20
2 1 30
Table3:Invoice_details
ID Invoice_id Items_id DetailsAmount
1 1 1 10
2 1 2 10
3 2 1 30

Aim:
Food 40
Beverage 10

My query:
Expand|Select|Wrap|Line Numbers
  1. SELECT ItemsName, SUM(DetailsAmount) 
  2. FROM Invoice_details 
  3. INNER JOIN Items ON Items_id = Items.ID 
  4. WHERE EXISTS (SELECT * FROM Invoice WHERE User = 1 AND Invoice_details.Invoice_id = Invoice.ID) 
  5. GROUP BY ItemsName
Error: You tried to execute a query that does not include the specified expression 'ItemsName' as part of an aggregate function.
Does anyone have a clue where I went wrong?
Aug 8 '14 #1
10 1406
Seth Schrock
2,965 Expert 2GB
The word Name is a reserved word in Access. The best solution would be to change the field name to something else, like ItemName. Another, less desirable, option would be to place the field name in square brackets.
Expand|Select|Wrap|Line Numbers
  1. GROUP BY [Name];
Aug 8 '14 #2
gdrop
5
Actually my real query mentions "PostNaam" instead of "Name". For reasons of simplicity and translation I changed the orginal query a bit. Apologies for the misunderstanding. I updated my question accordingly.
Aug 8 '14 #3
Seth Schrock
2,965 Expert 2GB
Please do a copy and paste of your query WITHOUT changing the original query. Also, please use code tags (the [CODE/] button will add them for you) when you post it.
Aug 8 '14 #4
NeoPa
32,556 Expert Mod 16PB
@gdrop.
The idea to simplify is a good one that many intelligent and helpful people come up with when posting their questions. Unfortunately, in practice, it seems to cause more problems than it avoids. Sometimes it's necessary as the original is extremely long or complicated in some way. If/when ever you do so (avoid where possible) then please always indicate that you have (Just as you did with fields left out of your tables).

Your table design.
The table [Invoice] should definitely not contain an amount field. It isn't a problem here - but will be later. See Database Normalisation and Table Structures.
NB. If you learn nothing else about databases at this site, but learn about normalisation, then we will have helped you more than you can imagine. I cannot overstate the importance of this issue.

Your SQL.
May I suggest a simpler approach that links the tables together logically and which allows you to filter what you want in the WHERE clause more straightforwardly :
Expand|Select|Wrap|Line Numbers
  1. SELECT   [Invoice].[User]
  2.        , [Items].[ItemsName]
  3.        , SUM([Invoice_Details].[DetailsAmount]) AS [Amount]
  4. FROM     ([Invoice_Details]
  5.          INNER JOIN
  6.          [Invoice]
  7.   ON     [Invoice_Details].[Invoice_ID]=[Invoice].[ID])
  8.          INNER JOIN
  9.          [Invoice_Details].[Items_ID]=[Items].[ID]
  10. WHERE    ([Invoice].[User]=1)
  11. GROUP BY [Invoice].[User]
  12.        , [Items].[ItemsName]
With this approach you can lose the WHERE clause and still filter the results externally, or even not filter at all if that's what you find you need.
Aug 10 '14 #5
gdrop
5
Hello NeoPa,
Your reply is very helpful to improve my insight in normalisation. Unfortunately the outcome of the query fails to the same error... (Note that I need to group by [ItemsName], not by [User], [ItemsName].)
Aug 11 '14 #6
Seth Schrock
2,965 Expert 2GB
Every field that isn't included in an expression/aggregate function (such as the Sum() function) has to be included in the GROUP BY clause. That is why you are getting the error. This is why we need the whole SQL code from your query so that we can figure out which field is missing.
Aug 11 '14 #7
gdrop
5
Expand|Select|Wrap|Line Numbers
  1. "SELECT Post.PostNaam, SUM(PostBoeking.Bedrag) " +
  2.             "FROM " +
  3.                 "(PostBoeking " + 
  4.                 "INNER JOIN LeverancierBoeking " +
  5.                 "ON PostBoeking.LeverancierBoeking_id = LeverancierBoeking.ID) " +
  6.             "INNER JOIN Post " +
  7.                 " ON PostBoeking.Post_id = Post.ID " +
  8.             "WHERE LeverancierBoeking.Actief = 1  " +
  9.             "AND LeverancierBoeking.Gebruiker_id = " + ID + " " +
  10.             "AND LeverancierBoeking.Valutadatum BETWEEN #" + vanaf + "# AND #" + tot + "# " +
  11.             "GROUP BY Post.PostNaam";
Aug 11 '14 #8
NeoPa
32,556 Expert Mod 16PB
Did you use the SQL as I posted it or did you only test it after removing the [User] field from the GROUP BY clause?

NB. When posting SQL it is helpful to post the actual SQL and not some part of the VBA code you have used to produce the SQL.
Aug 11 '14 #9
gdrop
5
The latest SQL that I posted, was the SQL adapted to your suggestion. That's the last version that I tested with. I'm not sure what VBA code you are referring to. The SQL is just a String that I wrote myself for my Java program (Eclipse IDE), the language is Dutch, in case you're wondering :-). I've only just graduated as a bachelor in software development, and sometimes I still seem to struggle with writing SQL statements...
Aug 12 '14 #10
NeoPa
32,556 Expert Mod 16PB
gdrop:
The latest SQL that I posted, was the SQL adapted to your suggestion.
Let me try to explain. Being a graduate I expect your difficulty is the language, though I find the Dutch education system for English is very good. Most young people from The Netherlands speak English very well. Still, it's a foreign language so harder than speaking in Dutch of course.

Here goes...

What you posted is not SQL - but text formatted as SQL strings (in VBA style) that should, when executed, result in a string which can be used as SQL. This is not the same as posting the SQL. The SQL would have the values for [vanaf] and [tot] resolved already and embedded in the string. See Before Posting (VBA or SQL) Code.

From what I could see of the SQL that was still contained within the VBA strings it absolutely does not reflect what I suggested. One glaring omission is the [User] reference within the GROUP BY clause. There may be (I suspect there are) more that will be easier to spot when we have the actual SQL to look at.

My Dutch is pretty rudimentary, but I can recognise (and even pronounce to a fashion) many of the terms used and I can see easily which are references to named items and which are SQL words. I suspect we can help just as much in Dutch as we could in English.

Please try again to :
  1. Update the SQL to match what I suggested.
  2. Test that SQL.
  3. Separate out the actual SQL code for posting what you tried and what result you got.
Aug 14 '14 #11

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

Similar topics

8
by: red | last post by:
This is a simple join but I am having trouble with it. I feel like such an idiot. I start with this select statement, which works fine: $query = "SELECT a.auction, a.winner, b.id FROM...
13
by: kieran | last post by:
Hi, I have the following SQL statement which is pulling a few details from a database. As you can see, there is only the one table from which i am creating a temporary copy. The reason I do...
1
by: serge calderara | last post by:
Dear all, I have an access database which is access by an application. From a remote PC using vb.net I am able to access to this database and collect necessary data. I need to make the remote...
5
by: Alejandrina | last post by:
Hi all, I wonder if anyone can help with this sql: Table "Property_Name" catalogs properties: (property_id, property_name) Table "File_Property" catalogs files and their properties: (file_id,...
9
by: deko | last post by:
I need to create a Recordset of all records that appear in one table but do NOT appear in another. To get the records that appear in BOTH tables, I can do this: SELECT tblEntity.Entity_ID...
7
by: germanshorthairpointer | last post by:
Hello, I'm trying to do a join based on the following tables: Person(person_id,person_name) Grade(grade_id,grade_person_id,grade_score) The data looks like this: Person:
2
by: clickon | last post by:
DataSet relationships seem to be focussed on one to many type realtionships. I have got a one to one realtionship in a DataSet between two tables. I can't do a join between the two tables in the...
2
by: BobLewiston | last post by:
Could someone please give me the simplest possible C# code snippets / SQL queries to determine whether: * a given SQL database exists, * a given existing SQL database is accessible, and * a...
2
anfetienne
by: anfetienne | last post by:
What is the best sql join method to use when i want to join upto 4 tables that all have the same columns?
1
by: Christian Sol | last post by:
I am confuse about combining and view recordes in 3 different tables simultaneously using the SQL join Operatores in the select query statement;SalesPerson,Customers and Orders. sample...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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,...
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...
0
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 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.