Reputation: 3
Thank for your help and reading, I have the below query and I don't understand why this error message occurs:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'Price'
I am able to query this data column (Wholesale Price) in a standalone query. I'm using SQL Server 2005 Management Studio .
SELECT
ILE.[Location Code],
ILE.SUM(Quantity) AS "Transfer Qty",
PP.SUM(Wholesale Price) AS "Transfer Cost (HK)"
FROM
[DB].[dbo].[Company$Item Ledger Entry] ILE
INNER JOIN
[DB].[dbo].[Company$Purchase Price] PP ON ILE.[Item No_] = PP.[Item No_]
AND ILE.[Variant Code] = PP.[Variant Code]
WHERE
ILE.[Entry Type] = '4'
AND ILE.[Location Code] NOT LIKE '%STAFF%'
AND ILE.[Location Code] NOT LIKE 'WHSPACKAGE'
AND ILE.[Location Code] NOT LIKE 'WHS'
GROUP BY
ILE.[Location Code], ILE.[Quantity], PP.[Wholesale Price]
ORDER BY
[Location Code]
Thanks!
Regards, Patrick
Upvotes: 0
Views: 1413
Reputation: 3
Thank you so much! Gordon!
Both SUM statement should be wording this format, Sum(ILE.[Quantity] and Sum(PP.[Wholesale Price])
SELECT ILE.[Location Code], Sum(ILE.[Quantity]) as "Transfer Qty",
Sum(PP.[Wholesale Price]) as "Transfer Cost (HK)"
FROM [Dummy-28-Oct-2016].[dbo].[TEST ENV$Item Ledger Entry] ILE
INNER JOIN [Dummy-28-Oct-2016].[dbo].[TEST ENV$Purchase Price] PP
ON ILE.[Item No_] = PP.[Item No_]
AND ILE.[Variant Code] = PP.[Variant Code]
Where ILE.[Entry Type] = '4'
AND ILE.[Location Code] NOT LIKE '%STAFF%'
AND ILE.[Location Code] NOT LIKE 'WHSPACKAGE'
AND ILE.[Location Code] NOT LIKE 'WHS'
Group by ILE.[Location Code], ILE.[Quantity]
Order by [Location Code];
Upvotes: 0
Reputation: 901
Check this select statement for your error
SELECT
ILE.[Location Code],
ILE.SUM(Quantity) AS "Transfer Qty",
PP.SUM([Wholesale Price]) AS "Transfer Cost (HK)"--Error line
Upvotes: 0
Reputation: 1269603
In SQL, you have to escape names that contain special characters -- and spaces are special characters. Because reading and writing code that has lots of square braces is cumbersome, the general advice is to avoid using such names.
In your case, you are missing the square braces:
SELECT ILE.[Location Code], ILE.Sum(Quantity) as "Transfer Qty",
Sum(PP.[Wholesale Price]) as "Transfer Cost (HK)"
FROM [DB].[dbo].[Company$Item Ledger Entry] ILE INNER JOIN
[DB].[dbo].[Company$Purchase Price] PP
ON ILE.[Item No_] = PP.[Item No_] AND
ILE.[Variant Code] = PP.[Variant Code]
Where ILE.[Entry Type] = '4' AND
ILE.[Location Code] NOT LIKE '%STAFF%' AND
ILE.[Location Code] NOT LIKE 'WHSPACKAGE' AND
ILE.[Location Code] NOT LIKE 'WHS'
Group by ILE.[Location Code], ILE.[Quantity]
Order by [Location Code];
In addition:
PP.SUM()
doesn't make sense. The table alias goes with the column name.GROUP BY
doesn't make sense. You want to aggregate the value, so it usually wouldn't go there.Upvotes: 7