SQL query missing expression?

Thug
Soldato
Joined
4 Jan 2013
Posts
3,783
SELECT CustomerNumber.invoices, COUNT(InvoiceNumber.invoices) AS NumberofInvoices,
FROM Invoices INNER JOIN customers
ON customernumber.invoices = customernumber.customers
GROUP BY CustomerNumber
HAVING CustomerNumber=1;


I'm trying to get the number of invoices sent to one person but its spitting up an error on line 2.

Anyone got a quick tip? Thanks.
 
Your table names need to be on the left-hand-side of the dot - so if your table name is Invoices you should be selecting 'Invoices.CustomerNumber', not 'CustomerNumber.Invoices'
 
Also another query problem I have is:


SELECT purchaseordernumber, SUM(quantityordered*costpriceperunit)
FROM purchaseorderline
GROUP BY purchaseordernumber
HAVING purchaseordernumber=10005;

I'm trying to find the total cost of purchase order (as shown below)

elac3Vd.png



Any ideas? Really appreciate it. :)
 
Shouldn't have a comma before FROM

Thanks, I got rid of it, and swapped a line down, and seem to have made some progress.

SELECT CustomerNumber.invoices, COUNT(InvoiceNumber.invoices) AS NumberofInvoices
FROM Invoices INNER JOIN customers
ON invoices.customernumber =customers.customernumber
GROUP BY CustomerNumber
HAVING CustomerNumber=1;


It is now saying line 5 is ambiguously defined (having customernumber=1).

Is there a better way to say it?
 
You shouldn't need to use HAVING in that particular query - try it with 'WHERE CustomerNumber=1' instead.

Your other issue to do with multiplying and sum looks ok to me.. try sticking an alias on the SUM() column, so it's:
SELECT purchaseordernumber, SUM(quantityordered*costpriceperunit) AS Total
 
You shouldn't need to use HAVING in that particular query - try it with 'WHERE CustomerNumber=1' instead.

Your other issue to do with multiplying and sum looks ok to me.. try sticking an alias on the SUM() column, so it's:
SELECT purchaseordernumber, SUM(quantityordered*costpriceperunit) AS Total


changing it to where, eg

SELECT CustomerNumber.invoices, COUNT(InvoiceNumber.invoices) AS NumberofInvoices
FROM Invoices INNER JOIN customers
ON invoices.customernumber =customers.customernumber
GROUP BY CustomerNumber
WHERE CustomerNumber=1;

gives the error SQL command not properly ended. :(



The other issue when I do that just gives the result:

4efOPnp.png
 
Using my data so it makes sense.

Just change the field names, table names - basically your where needs to be before the group by.

SELECT

elec.ADDRESS,
COUNT(elec.MPAN + gas.mprn) AS STUFF

FROM dbo.elec_stuff elec INNER JOIN dbo.gas_stuff gas

ON gas.ADDRESS= elec.ADDRESS

WHERE elec.DATA_ITEM = 'STUFF'

GROUP BY elec.ADDRESS
 
Sorry, the WHERE clause should be before the GROUP BY...

SELECT CustomerNumber.invoices, COUNT(InvoiceNumber.invoices) AS NumberofInvoices
FROM Invoices INNER JOIN customers
ON invoices.customernumber =customers.customernumber
WHERE CustomerNumber=1
GROUP BY CustomerNumber;

The answer to your other query is correct:
20*4 = 80
5*4 = 20
80+20 = 100
 
Sorry, the WHERE clause should be before the GROUP BY...

SELECT CustomerNumber.invoices, COUNT(InvoiceNumber.invoices) AS NumberofInvoices
FROM Invoices INNER JOIN customers
ON invoices.customernumber =customers.customernumber
WHERE CustomerNumber=1
GROUP BY CustomerNumber;

The answer to your other query is correct:
20*4 = 80
5*4 = 20
80+20 = 100

I just noticed the other one was correct, my bad! :o

SELECT CustomerNumber.invoices, COUNT(InvoiceNumber.invoices) AS NumberofInvoices
FROM Invoices INNER JOIN customers
ON invoices.customernumber =customers.customernumber
WHERE CustomerNumber=1
GROUP BY CustomerNumber;

It now says it is ambigously defined. :(
 
Try this,

SELECT
Inv.CustomerNumber
, COUNT(inv.InvoiceNumber) AS NumberofInvoices
FROM
Invoices Inv
JOIN
customers cus
ON
inv.customernumber = cus.customernumber
WHERE
cus.CustomerNumber=1
GROUP BY
cus.CustomerNumber
 
You need to specify the table & column names everywhere if they are shared.

Edit - ahh beaten like a red headed stepchild.
 
Back
Top Bottom