Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sql Query ::

WITH CTE (
		  HatchNumber,DeckNumber,orgzn,Destination,loadStatus,NumofPallets,row_num
) AS
(
SELECT 
        T1.HatchNumber,T1.DeckNumber,T1.orgzn,T1.Destination,T1.loadStatus,T1.NumofPallets,
        ROW_NUMBER() OVER (
							PARTITION BY 
						    T1.HatchNumber,T1.DeckNumber,T1.orgzn,T1.Destination,T1.loadStatus,T1.NumofPallets
							ORDER BY 
						   T1.HatchNumber,T1.DeckNumber,T1.orgzn,T1.Destination,T1.loadStatus,T1.NumofPallets
						  ) AS row_num
FROM 
    (
	   Select S.HatchNumber,
	   S.DeckNumber,
	   S.orgzn,
	   S.pallet_id,
	   S.Destination,
	   S.loadStatus, 
	   (Select Count(Distinct(Pallet_id)) from WMS.Stock where HatchNumber=S.hatchNumber AND DeckNumber=S.DeckNumber and orgzn=S.orgzn And LoadStatus='DP') As'NumofPallets'
 
       from WMS.Stock S 
	   where S.HatchNumber IS NOT NULL and S.DeckNumber IS NOT NULL and S.LoadStatus='DP' and S.ship_no='200045'
	) as T1
)


Syntax Error::

Msg 102, Level 15, State 1, Line 20
Incorrect syntax near ')'.

What I have tried:

Tried with syntax editor to Remove syntax error while deleting duplicate records by using Row_Number function in Commen Type Expression (CTE) of SQL
Posted
Updated 29-Dec-20 20:14pm

1 solution

This:
SQL
Select Count(Distinct(Pallet_id))

should be writen as:
SQL
Select Count(Distinct Pallet_id)


You need to write final select:
SQL
;WITH CTE (<<fields>>)
AS
(
  --content
)
--final select
SELECT...
FROM CTE


For further details, please see:
WITH common_table_expression (Transact-SQL) - SQL Server | Microsoft Docs[^]
Mastering Common Table Expression or CTE in SQL Server[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900