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

Help me understand this sql code

I have this bit of code:
SELECT itemcode, min(Price) AS minprice
FROM itemlist
GROUP BY itemcode;

it lists the minimum price for each itemcode group. It works fine.

But when I want to see the last name of the person who bought this item, it
give me that aggregate error saying the lastname is not part of the
aggregate function.
BUt that is silly, because if I put it in the group by, then the function
does not work.

SELECT lastname, itemcode, min(Price) AS minprice
FROM itemlist
GROUP BY itemcode;
<< this gives an error

SELECT lastname, itemcode, min(Price) AS minprice
FROM itemlist
GROUP BY itemcode, lastname;
this will not group by the itemcode only which I need, it groups but

itemcode and lastname

what do I do?
Nov 13 '05 #1
4 1520
If you think about it logically, how could it give you the last name
information? You're telling it to group by the item code and give you the
minimum price of each. Fine. Say there are three prices for a particular
item code. The result is one line with the item code and the minimum price
of the three. Fine. Now you want the last name. Which last name does it give
you? There are three of them. How can it know which one you want?

You may say, "I want the one that's associated with the minimum price." OK;
but what if there are two last names that have the same price and both are
the minimum price. Which last name would it use?

Consider the following:

ItemCode: 1
LastName: Smith
Price: 100.00

ItemCode:1
LastName: Jones
Price: 100.00

ItemCode:1
LastName: Peters
Price: 200

In your current query, the line for item code 1 will read as follows:

ItemCode MinPrice
1 100.00

Now you want to add the last name. Which one does it use, Smith or Jones?
Hence the error.

The answer is either to group by the last name (which will ruin your item
code group by, as you found out), or create two queries -- one to get the
minimum price, and the other to get the name (in SQL Server you can do this
with one query, but in Access you'd need to use a stored subquery).

So, you would keep your first query the way it is. Call it Query1. Then
create a new query with the following SQL:

SELECT itemlist.ItemCode, itemlist.Price, First(itemlist.Name) AS Name
FROM itemlist INNER JOIN Query1 ON (itemlist.Price = Query1.MinPrice) AND
(itemlist.ItemCode = Query1.ItemCode)
GROUP BY itemlist.ItemCode, itemlist.Price

That would give you the first name in the case that there is more than one
with the same minimum price.

Neil
"Danny" <da********@hotmail.com> wrote in message
news:N_**********************@news4.srv.hcvlny.cv. net...
I have this bit of code:
SELECT itemcode, min(Price) AS minprice
FROM itemlist
GROUP BY itemcode;

it lists the minimum price for each itemcode group. It works fine.

But when I want to see the last name of the person who bought this item,
it
give me that aggregate error saying the lastname is not part of the
aggregate function.
BUt that is silly, because if I put it in the group by, then the function
does not work.

SELECT lastname, itemcode, min(Price) AS minprice
FROM itemlist
GROUP BY itemcode;
<< this gives an error

SELECT lastname, itemcode, min(Price) AS minprice
FROM itemlist
GROUP BY itemcode, lastname;
this will not group by the itemcode only which I need, it groups but

itemcode and lastname

what do I do?

Nov 13 '05 #2
My question is similar except it involves the count aggregate:

I am counting the number of times a certain "code" is used. I want to
return the count, the code
and also the codes description. However each code has multiple
descriptions. Ideally I would like to return the most popular
description, but another option would be to return all of the
descriptions.

This is my code so far:

SELECT code, Count(code) as a
FROM codebank
GROUP BY code
ORDER BY a Desc

This works fine to return the count as well as the code, but attaching
the description to the output is giving me fits.

I've tried:

SELECT code, Count(code) as a, description
FROM codebank
GROUP BY code
ORDER BY a Desc

But I get the Error: "description is not included in an AGGREGATE
function or Group By. I understand that since I am using the count
aggregate it wants me to include the description inside of the GROUP
BY. However, this defeats my desired outcome. I get an output that
looks like this

# Code desc
-------------------------------------------------------------------
3 001 dollars
2 001 euros
2 002 laundry
1 003 food

What I want is:
# Code desc
-------------------------------------------------------------------
5 001 dollars (or dollars, euros)
2 002 laundry
1 003 food
Please Help....

Nov 13 '05 #3
Try adding all the select criteria into the group by statement

SELECT code, Count(code) as a, description
FROM codebank
GROUP BY code,a,description
ORDER BY a Desc
<ma*********@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
My question is similar except it involves the count aggregate:

I am counting the number of times a certain "code" is used. I want to
return the count, the code
and also the codes description. However each code has multiple
descriptions. Ideally I would like to return the most popular
description, but another option would be to return all of the
descriptions.

This is my code so far:

SELECT code, Count(code) as a
FROM codebank
GROUP BY code
ORDER BY a Desc

This works fine to return the count as well as the code, but attaching
the description to the output is giving me fits.

I've tried:

SELECT code, Count(code) as a, description
FROM codebank
GROUP BY code
ORDER BY a Desc

But I get the Error: "description is not included in an AGGREGATE
function or Group By. I understand that since I am using the count
aggregate it wants me to include the description inside of the GROUP
BY. However, this defeats my desired outcome. I get an output that
looks like this

# Code desc
-------------------------------------------------------------------
3 001 dollars
2 001 euros
2 002 laundry
1 003 food

What I want is:
# Code desc
-------------------------------------------------------------------
5 001 dollars (or dollars, euros)
2 002 laundry
1 003 food
Please Help....

Nov 13 '05 #4
Thanks, but I tried that and it gave me that first output I showed. I
only want to sort by the code.

Nov 13 '05 #5

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

Similar topics

3
by: HLong | last post by:
I am trying to understand an example where the Least Significant Bit is replaced. The code is in C# and I am having problems with a line that reads: B= (byte) (Value == 1 ? B | (1 << poss) : B & ~...
6
by: Mark Reed | last post by:
Hi all, I am trying to learn a little about programming (I know next to nothing so far) and have found some code which hides the toolbars. However, this bit of code is a little too effective and...
6
by: Astroman | last post by:
Hi guys and girls. This is my first time posting here so go easy :) . I was wondering if someone could please interpret how this csum() function works in the following C code. I know that the...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
3
by: DM | last post by:
newbie trying to set up a Database and keep getting the same result. ERROR An error occured while retrieving the information from the database: Unable to cast COM object of type...
16
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just...
7
by: Mickyd1561 | last post by:
Hey everyone I'm new to this groups thing and thought maybe someone can help me. My problem is that I can't view specific images on only one website. www.baseballu.net is my baseball team's...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
19
by: mohammaditraders | last post by:
a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions Cal_percentage() which calculate the...
7
by: sara | last post by:
I have a friend doing some pro-bono work for a non-profit that does job training for distressed kids under DCSS care. He asked me for code to do the following (he's using A2003). I can't find...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.