SQL Problem

smashclash

Board Regular
Joined
Nov 24, 2003
Messages
126
Office Version
  1. 365
Platform
  1. Windows
I have a table with two columns. One is PID which is various whole numbers and the other is QTY (Quantity). I'm trying to write an SQL statement that will list the average quantity whenever PID = 2.

Right now I have

SELECT Pid, AVG(qty)
FROM Line_item
Where Pid = '2';


But I keep getting the error - tried to execute a query that does not include the specified expression 'pid' as part of the aggregate function. Anyone have any ideas? TIA.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Are you writing this straight as a SQL statement or using the QBE?
 
Upvote 0
Have you tried using the QBE?

Or is there a particular reason why you need to use SQL?
 
Upvote 0
Hi,

is PID a text-field?

If not then change your query like this:

SELECT Pid, AVG(qty)
FROM Line_item
Where Pid = 2;
 
Upvote 0
If PID is a text field I think the SQL statement should be as follows:

SELECT Line_item.PID, Avg(Line_item.qty) AS AvgOfqty
FROM Line_item
GROUP BY Line_item.PID
HAVING (((Line_item.PID)='2'));

If PID is a number then:

SELECT Line_item.PID, Avg(Line_item.qty) AS AvgOfqty
FROM Line_item
GROUP BY Line_item.PID
HAVING (((Line_item.PID)=2));
 
Upvote 0

Forum statistics

Threads
1,221,832
Messages
6,162,254
Members
451,757
Latest member
iours

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top