SQL Server Incorrect Syntax error - sql-server

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

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.
Putting the wholesale price in the GROUP BY doesn't make sense. You want to aggregate the value, so it usually wouldn't go there.

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

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];

Related

SQL Query - SUM and JOIN multiple tables but return missing record

SQL Query - SUM and JOIN multiple tables but return missing record;
Good day!
here's the table lists;
Barcodes Table
Item Ledger Entry Table
Trans_ Sales Entry Table
Transfer Line Table
Note: all table has a relation Store No/Location, Item No and Variant Code
Need help.
when i JOIN and SUM the Trans_ Sales Entry Table and Transfer Line Table , but the query result return some missing record
screenshot: missing record.jpg
Thank you!
here's my sql query code;
;WITH Barcodes AS
(
SELECT
[BBI$Barcodes].[Item No_]
,[BBI$Barcodes].[Description]
,[BBI$Barcodes].[Variant Code]
FROM [BBI$Barcodes]
),
ILEtransfer AS
(
SELECT
[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Location Code] AS [Location]
,[BBI$Item Ledger Entry].[Item No_]
,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]
,[BBI$Item Ledger Entry].[Variant Code]
,SUM([BBI$Item Ledger Entry].[Quantity]) AS [PDel]
FROM [BBI$Item Ledger Entry]
WHERE
[BBI$Item Ledger Entry].[Location Code]='HPGW'
AND [BBI$Item Ledger Entry].[Entry Type] = '4'
GROUP BY
[BBI$Item Ledger Entry].[Location Code]
,[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Item No_]
,[BBI$Item Ledger Entry].[Variant Code]
),
ILEmsales AS
(
SELECT
[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Location Code] AS [Location]
,[BBI$Item Ledger Entry].[Item No_]
,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]
,[BBI$Item Ledger Entry].[Variant Code]
,SUM([BBI$Item Ledger Entry].[Quantity]) AS [MSales]
FROM [BBI$Item Ledger Entry]
WHERE
[BBI$Item Ledger Entry].[Location Code]='HPGW'
AND [BBI$Item Ledger Entry].[Entry Type] = '1'
AND [BBI$Item Ledger Entry].[Document No_] NOT LIKE 'HP%'
GROUP BY
[BBI$Item Ledger Entry].[Location Code]
,[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Item No_]
,[BBI$Item Ledger Entry].[Variant Code]
),
ILEpadj AS
(
SELECT
[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Location Code] AS [Location]
,[BBI$Item Ledger Entry].[Item No_]
,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]
,[BBI$Item Ledger Entry].[Variant Code]
,SUM([BBI$Item Ledger Entry].[Quantity]) AS [PAdj]
FROM [BBI$Item Ledger Entry]
WHERE
[BBI$Item Ledger Entry].[Location Code]='HPGW'
AND [BBI$Item Ledger Entry].[Entry Type] = '2'
GROUP BY
[BBI$Item Ledger Entry].[Location Code]
,[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Item No_]
,[BBI$Item Ledger Entry].[Variant Code]
),
ILEnadj AS
(
SELECT
[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Location Code] AS [Location]
,[BBI$Item Ledger Entry].[Item No_]
,MAX([BBI$Item Ledger Entry].[Description]) AS [Description]
,[BBI$Item Ledger Entry].[Variant Code]
,SUM([BBI$Item Ledger Entry].[Quantity]) AS [NAdj]
FROM [BBI$Item Ledger Entry]
WHERE
[BBI$Item Ledger Entry].[Location Code]='HPGW'
AND [BBI$Item Ledger Entry].[Entry Type] = '3'
GROUP BY
[BBI$Item Ledger Entry].[Location Code]
,[BBI$Item Ledger Entry].[Entry Type]
,[BBI$Item Ledger Entry].[Item No_]
,[BBI$Item Ledger Entry].[Variant Code]
),
Transfeline AS
(
SELECT
[BBI$Transfer Line].[Transfer-to Code]
,[BBI$Transfer Line].[Item No_]
,MAX([BBI$Transfer Line].[Description]) AS [Description]
,MAX([BBI$Transfer Line].[Description 2]) AS [Description 2]
,[BBI$Transfer Line].[Variant Code]
,SUM([BBI$Transfer Line].[Quantity]) AS [UDel]
FROM [BBI$Transfer Line]
WHERE
[BBI$Transfer Line].[Transfer-to Code] = 'HPGW'
GROUP BY
[BBI$Transfer Line].[Transfer-to Code]
,[BBI$Transfer Line].[Item No_]
,[BBI$Transfer Line].[Variant Code]
),
TSEpsales AS
(
SELECT
[BBI$Trans_ Sales Entry].[Store No_]
,[BBI$Trans_ Sales Entry].[Item No_]
,[BBI$Trans_ Sales Entry].[Variant Code]
,SUM([BBI$Trans_ Sales Entry].[Quantity]) AS [PSales]
FROM [BBI$Trans_ Sales Entry]
WHERE
[BBI$Trans_ Sales Entry].[Store No_]='HPGW'
AND [BBI$Trans_ Sales Entry].[Transaction No_] NOT IN (5271,5272,5273,5278,5279,5280,5281,5282,5283,5284,5285,5286,8530,8531,8532,8533,8534,8535,8536,8537,13133,13849)
GROUP BY
[BBI$Trans_ Sales Entry].[Store No_]
,[BBI$Trans_ Sales Entry].[Item No_]
,[BBI$Trans_ Sales Entry].[Variant Code]
)
SELECT DISTINCT
BAR.[Item No_] AS [Item No_]
,BAR.[Description] AS [Description]
,BAR.[Variant Code] AS [Variant Code]
,ISNULL(ILETR.[PDel],0) AS [PDel]
,ISNULL(ILEMS.[MSales],0) AS [MSales]
,ISNULL(ILEPA.[PAdj],0) AS [PAdj]
,ISNULL(ILENA.[NAdj],0) AS [NAdj]
,ISNULL(TL.[UDel],0) AS [UDel]
,ISNULL(TSEPS.[PSales],0) AS [PSales]
,ISNULL(ILETR.[PDel],0)+ISNULL(ILEPA.[PAdj],0)+ISNULL(TL.[UDel],0)+ISNULL(ILEMS.[MSales],0)+ISNULL(ILENA.[NAdj],0)+ISNULL(TSEPS.[PSales],0) AS Total
FROM [BBI$Barcodes] BAR
LEFT JOIN [ILEtransfer] ILETR
ON ILETR.[Item No_]=BAR.[Item No_]
AND ILETR.[Variant Code]=BAR.[Variant Code]
LEFT JOIN [ILEmsales] ILEMS
ON ILEMS.[Item No_]=BAR.[Item No_]
AND ILEMS.[Variant Code]=BAR.[Variant Code]
LEFT JOIN [ILEpadj] ILEPA
ON ILEPA.[Item No_]=BAR.[Item No_]
AND ILEPA.[Variant Code]=BAR.[Variant Code]
LEFT JOIN [ILEnadj] ILENA
ON ILENA.[Item No_]=BAR.[Item No_]
AND ILENA.[Variant Code]=BAR.[Variant Code]
FULL JOIN [Transfeline] TL
ON TL.[Item No_]=BAR.[Item No_]
AND TL.[Variant Code]=BAR.[Variant Code]
FULL JOIN [TSEpsales] TSEPS
ON TSEPS.[Item No_]=BAR.[Item No_]
AND TSEPS.[Variant Code]=BAR.[Variant Code]
WHERE
ILETR.[Location]='HPGW'
AND ILETR.[Entry Type]='4'
OR ILEMS.[Entry Type]='1'
OR ILEPA.[Entry Type]='2'
OR ILENA.[Entry Type]='3'

I want to convert inner join Query in SubQuery

I am using inner join in query but I want Subquery instead of join
select [Document No_] as DocumentNo,
ledger.[Posting Date] as Date,
[Sales (LCY)] as Amount,
header.[Customer Reference] as PONo
from [Uneek Clothing Company Ltd$Cust_ Ledger Entry] ledger
inner join [Uneek Clothing Company Ltd$Sales Invoice Header] header on ledger.[Entry No_]=header.[Cust_ Ledger Entry No_]
where [Customer No_] = 'DRC01'
and [Document Type]=2
and [Sales (LCY)]!=0
and ledger.[Posting Date] between '01/01/2019' and '12/31/2019'
order by [Sales (LCY)] asc offset 0 ROWS FETCH NEXT 20 ROWS ONLY
[Entry No_] is foreign key in [Uneek Clothing Company Ltd$Sales Invoice Header] with name [Cust_ Ledger Entry No_]
if you really want to drop the join and use a subquery instead,
it will look something like this.
Since you did not use an table alias for all columns, I had to assume that no other column of table in the subquery is used anywhere else. If that is not the case then this will not work
It will also only work if the subquery returns no more then 1 row, otherwise a subquery cannot be used at all
select [Document No_] as DocumentNo,
ledger.[Posting Date] as Date,
[Sales (LCY)] as Amount,
( select header.[Customer Reference]
from [Uneek Clothing Company Ltd$Sales Invoice Header] header
where ledger.[Entry No_] = header.[Cust_ Ledger Entry No_]
) as PONo
from [Uneek Clothing Company Ltd$Cust_ Ledger Entry] ledger
where [Customer No_] = 'DRC01'
and [Document Type]=2
and [Sales (LCY)]!=0
and ledger.[Posting Date] between '01/01/2019' and '12/31/2019'
order by [Sales (LCY)] asc
offset 0 ROWS FETCH NEXT 20 ROWS ONLY

Scheduled datetime conversion error (SQL)

When trying to convert a datetime field to get rid of NULL's I am getting the error that the datetime cannot be converted.
This query is populating another database where the column datatype is date.
This job runs perfectly fine when not scheduled, but results in an error during the scheduled insert job.
Any ideas?
, (SELECT
CASE
WHEN MIN([Discount Ledger].[Posting Date]) IS NOT NULL
THEN CONVERT(VARCHAR(50), MIN([Discount Ledger].[Posting Date]), 121)
ELSE '-'
END
FROM
[Wings$Discount Ledger Entry] AS "Discount Ledger"
LEFT JOIN
[Wings$Periodic Discount] AS "MixMatchHeader" ON [Discount Ledger].[Offer No_] = [MixMatchHeader].[No_]
LEFT JOIN
[Wings$Periodic Discount Line] AS "MixMatch" ON [MixMatchHeader].[No_] = [MixMatch].[Offer No_]
LEFT JOIN
[Wings$Item_Special Group Link] AS "Special Group" ON [MixMatch].[No_] = [Special Group].[Special Group Code]
LEFT JOIN
[Wings$Store Price Group] AS "Store Price Group" ON [MixMatch].[Price Group] = [Store Price Group].[Price Group Code]
WHERE
[MixMatch].[No_] = [Item Ledger].[Item No_]
AND [MixMatch].[Variant Code] = [Item Ledger].[Variant Code]
AND [Store Price Group].[Store] = [Item Ledger].[Location Code]) AS "Discount Start Date"
You can do this using ISNULL. Since you are looking for the MIN date, I will use GETDATE() as the argument/replacement value for NULL. Unless your data has future dates, then GETDATE() should function as the MAX value. Unless you want the CASE statement to evaluate the NULL values as the MIN. In that case, you can use an empty string '' or '1900-01-01' instead of GETDATE().
, (SELECT
CASE
WHEN
MIN([Discount Ledger].[Posting Date]) IS NOT NULL
THEN CONVERT(varchar(50),MIN(ISNULL([Discount Ledger].[Posting Date],GETDATE())),121)
ELSE '-'
END
FROM [Wings$Discount Ledger Entry] AS "Discount Ledger"
LEFT JOIN [Wings$Periodic Discount] AS "MixMatchHeader" ON [Discount Ledger].[Offer No_] = [MixMatchHeader].[No_]
LEFT JOIN [Wings$Periodic Discount Line] AS "MixMatch" ON [MixMatchHeader].[No_] = [MixMatch].[Offer No_]
LEFT JOIN [Wings$Item_Special Group Link] AS "Special Group" ON [MixMatch].[No_] = [Special Group].[Special Group Code]
LEFT JOIN [Wings$Store Price Group] AS "Store Price Group" ON [MixMatch].[Price Group] = [Store Price Group].[Price Group Code]
WHERE [MixMatch].[No_] = [Item Ledger].[Item No_] AND [MixMatch].[Variant Code] = [Item Ledger].[Variant Code] AND [Store Price Group].[Store] = [Item Ledger].[Location Code])
AS "Discount Start Date"

SQL Syntax error (only when run as scheduled job)

I have just created a scheduled job to run daily, but when the job runs via the scheduler I get the error "Incorrect syntax near 'Item'. [SQLSTATE 42000] (Error 102)".
This only happens when the job is run via the scheduler, and works fine when run directly.
DELETE FROM [NAVQueriesDB].[dbo].[New Items on Order] GO
SET ANSI_WARNINGS OFF
GO
INSERT [NAVQueriesDB].[dbo].[New Items on Order] (
[Updated Date],
[Location Code],
[Order No_],
[Item No_],
[Item Description],
[Variant Code],
[Description 2],
[Outstanding Qty_],
[Order Date],
[Vendor_No],
[Vendor_Name])
SELECT
GETDATE() AS "Updated Date",
[Purch Line].[Location Code],
[Purch Line].[Document No_],
[Item].[No_],
[Item].[Description],
ISNULL([Purch Line].[Variant Code],'-') AS "Variant Code",
ISNULL([Variant].[Description 2],'-') AS "Description 2",
CAST(SUM([Purch Line].[Outstanding Quantity]) as decimal(18,0)) AS "Outstanding Qty.",
[Purch Line].[Order Date],
[Purch Line].[Buy-from Vendor No_],
[Vendor].[Name]
FROM [Wings$Item] AS "Item"
LEFT JOIN [Wings$Purchase Line] AS "Purch Line"
ON [Item].[No_] = [Purch Line].[No_]
LEFT JOIN [Wings$Item Variant] AS "Variant"
ON [Item].[No_] = [Variant].[Item No_] AND [Purch Line].[Variant Code] = [Variant].[Code]
LEFT JOIN [Wings$Vendor] AS "Vendor"
ON [Purch Line].[Buy-from Vendor No_] = [Vendor].[No_]
LEFT JOIN [Wings$Item Ledger Entry] AS "Item Ledger"
ON [Item].[No_] = [Item Ledger].[Item No_]
WHERE
[Item].[No_] > '5618%'
AND [Item].[No_] <> 'Z999999'
AND [Item Ledger].[Item No_] IS NULL
GROUP BY
[Purch Line].[Location Code],
[Purch Line].[Document No_],
[Item].[No_],
[Item].[Description],
[Purch Line].[Variant Code],
[Variant].[Description 2],
[Purch Line].[Outstanding Quantity],
[Purch Line].[Order Date],
[Purch Line].[Buy-from Vendor No_],
[Vendor].[Name]
ORDER BY
[Location Code],
[Item].[No_],
[Item].[Description],
[Purch Line].[Variant Code]
It's a bad idea (IMO) to use double quotes for your identifiers. The standard is [] if it has to be quoted (which is also why I avoid spaces in my names so that I don't have to quote them).
SQL Agent defaults to QUOTED_IDENTIFIER being set to OFF. If you change the double quotes to brackets then this should work. Alternatively (but not recommended), you could include the first line of your script to be SET QUOTED_IDENTIFIER ON.
For example, GETDATE() AS "Updated Date"... GETDATE() AS [Updated Date] is better. GETDATE() AS updated_date is best.

T-SQL - WHERE condition performance [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have variable declared like this
DECLARE #Customer_No VARCHAR(MAX)
SET #Customer_No = 'Z110073574;Z110027464;Z110229752;Z110274156;Z110170566;Z110102837;Z110074199'
The query contains condition
WHERE ((#Customer_No = '') OR ([Customer No_] IN (SELECT [No_] FROM Customer)))
If the variable #Customer_No can contains blank value (''), filtering should be skipped. In case the variable contains customers separated by semicolons, the string is parsed into table Customer and the query is filtered by values in Customer table.
The query takes 15 minutes to run. If I use just the second condition, it takes about 50 seconds.
WHERE [Customer No_] IN (SELECT [No_] FROM Customer)
I don't get it. How is this possible? Any solution? Explanation?
Thank you.
Edit: Query
SELECT DET.[Cust_ Ledger Entry No_]
, DET.[Amount]
, DET.[Entry Type]
, CLE.[Document No_]
FROM [dbo].[Company$Cust_ Ledger Entry] CLE
LEFT JOIN [dbo].[Company$Detailed Cust_ Ledg_ Entry] DET ON CLE.[Entry No_] = DET.[Cust_ Ledger Entry No_]
WHERE ((#Customer_No = '') OR (DET.[Customer No_] IN (SELECT [No_] FROM Customer)))
AND (DET.[Entry Type] = 1 OR DET.[Entry Type] = 2)
AND (CLE.[Due Date] >= #FromDueDate AND CLE.[Due Date] <= #ToDueDate)
AND CLE.[Posting Date] <= #ToDate
I think you can simply the query without loss of integrity to the following. Like Aaron said, can you post a query plan?
SELECT
DET.[Cust_ Ledger Entry No_],
DET.[Amount],
DET.[Entry Type],
CLE.[Document No_]
FROM
[dbo].[Company$Cust_ Ledger Entry] CLE
LEFT JOIN
[dbo].[Company$Detailed Cust_ Ledg_ Entry] DET
ON
CLE.[Entry No_] = DET.[Cust_ Ledger Entry No_]
WHERE
( #FAS_No = '' OR DET.[Customer No_] IN (SELECT [No_] FROM Customer) )
AND DET.[Entry Type] in (1, 2)
AND CLE.[Due Date] >= #FromDueDate
AND CLE.[Due Date] <= #ToDueDate
AND CLE.[Posting Date] <= #ToDate
It's not easy to pinpoint the exact problem without more information (table structure, # of rows, etc). However, one thing for sure is the the use of OR in TSQL reduces overall performance. If there is a way to use an AND instead of an OR than it's recommended you do so.
what you can always do it use an IF statement that check if the variable is empty. If not, you use the SELECT with the WHERE. If it is empty, you use the SELECT without the WHERE.
Another thing to check might be your indexes. That can speed things a little.
The DET in the where is killing the left so might as well join
If you pull conditions into the join then the query optimizer seems to do a better
I think this gives a the query optimizer a better chance
SELECT DET.[Cust_ Ledger Entry No_]
, DET.[Amount]
, DET.[Entry Type]
, CLE.[Document No_]
FROM [dbo].[Company$Cust_ Ledger Entry] CLE
JOIN [dbo].[Company$Detailed Cust_ Ledg_ Entry] DET
ON CLE.[Entry No_] = DET.[Cust_ Ledger Entry No_]
AND ( DET.[Entry Type] IN (1,2) )
AND ( CLE.[Due Date] >= #FromDueDate AND CLE.[Due Date] <= #ToDueDate )
AND CLE.[Posting Date] <= #ToDate
JOIN Customer
ON #Customer_No = ''
OR DET.[Customer No_] = Customer.[No_]
And try hints
I would start with INNER MERGE JOIN Customer
And please [NO_] is declared as a PK
I think you can reduce the performance by writing in
USE CTE for - (SELECT [No_] FROM Customer)
Join cte with the main table which will give you performance

Resources