how do I fix this coding issue? - sql

WITH nl_sentcount
AS(
Select nl_id, count(user_id) as sent_num
from sheet1$
Where event_type='nlsent'
group by nl_id
)
Why the error shows as
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.

You need a select statement after the WITH to do something:
WITH nl_sentcount AS (
Select nl_id, count(user_id) as sent_num
from sheet1$
Where event_type='nlsent'
group by nl_id
)
select *
from nl_sentcount;

Related

Incorrect syntax near ')' Error in the CTE

I wrote the following CTE (Common Table Expression)
WITH PRODUCTION_CTE(ShortProdNo,BoatRefNumber,ProdNo, CustomerPoNumber,LoadDate, Trailer, VeadaBuilding)
AS
(
SELECT
FBS.BoatNumber AS ShortProdNo,
UOD.BoatRefNumber AS BoatRefNumber,
FBS.ProdNo AS ProdNo,
UOD.CustomerPoNumber AS CustomerPoNumber,
FBS.Shipped AS LoadDate,
FBS.TruckNum AS Trailer,
(CASE
WHEN Rtrim(UOD.CustomerPoNumber)='VEADA-VS1' THEN 'Bldg10'
ELSE 'Bldg4'
END) AS VeadaBuilding
FROM SysproCompanyV.dbo.FlatBenningtonShipping as FBS
INNER JOIN SysproCompanyV.dbo.UsrOrderDetails as UOD
ON FBS.BoatNumber=UOD.BoatRefNumber)
I am getting the following Error from the above CTE:
Msg 102, Level 15, State 1, Line 17
Incorrect syntax near ')'.
I am not sure why it is happening as Inner Joins are allowed in the CTE, all the parenthesis are closed and the names are correctly declared.
You need to call the CTE (i.e. PRODUCTION_CTE) IMMEDIATE after declaration :
;with PRODUCTION_CTE as (
. . .
)
select pc.*
from PRODUCTION_CTE pc

Error in SQL Server query - inner join

I am trying to add Percentile value for each record as new column. But i am getting error in my SQL query. Can anyone please help to solve it.
Error message:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'inner'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'a'.
Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from
[Country_table1$] b where [Country] = 'AUSTRALIA'
inner join
(
select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country]
ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
from [Country_table1$]
GROUP BY [MSCI_Price_idx],[Country]
) a
ON a.[Country] = b.[Country]
You have your where statement in the wrong place. Joins are formally part of the from statement and thus come before criteria. To have your criterion at the bottom check the correct table you use the alias.
Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from
[Country_table1$] b
inner join
(
select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country]
ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
from [Country_table1$]
GROUP BY [MSCI_Price_idx],[Country]
) a
ON a.[Country] = b.[Country]
where b.[Country] = 'AUSTRALIA'

Get MAX value from a TOP number of records

I have the following MS SQL Query
select top 10 RegisterTime
from orderNotifications
order by RegisterTime asc
How can I get the max RegisterTime of that query ?
I have tried this
select max(RegisterTime) in (
select top 10 RegisterTime
from orderNotifications
order by RegisterTime asc
)
But I am getting
Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword
'in'.
Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the
keyword 'order'.
Make your TOP 10 query a subquery:
SELECT MAX(RegisterTime)
FROM (SELECT TOP 10 RegisterTime
FROM orderNotifications
ORDER BY RegisterTime
)sub

Howto query average of most recent data in sqlserver

I want to find the average of the most recent x results. Is it possible to use an aggregate function on a limited number of results with sql server?
Here is what I have tried and the errors I get:
select avg(top(100) convert(float, AnalysisData))
from tab
order by DatePerformed desc
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword
'top'.
select AVG(data)
from (
select top(100) convert(float, AnalysisData) As data
from tab
order by DatePerformed desc
);
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.
This is sql server 2008
In your second example, you need an alias name for the subquery:
select AVG(data)
from (
select top 100 convert(float, AnalysisData) As data
from tab
order by DatePerformed desc
) Top100Records;
You probably don't need the parenthesis around 100 either.

SQL [Hard query - to make or to avoid]

SELECT Name,
( NOT (ID_ListGroupParIzm
IN (SELECT ID_Param
FROM TbUserParam
WHERE ID_User=:ID_User
)
)
) Visi
FROM CfgListParIzm
WHERE ID_ListGroupParIzm=:ID_ListGroupParIzm
Errors :
Message 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword "NOT".
Message 102, Level 15, State 1, Line 2
Incorrect syntax near the construction ":".
added :
I'll try to explain what I want with it.
I need a name from one table and a BOOL value for each Node wich will be false if ID_ListGroupParIzm IN (SELECT ID_Param FROM TbUserParam
WHERE ID_User=:ID_User
And ID_ListGroupParIzm (from CfgListParIzm ) = ID_Param (from TbUserParam)
forget to say :(
btw : looking like select can't return logics value . . .
How to get my purpose then :(
added a try :
SELECT Name,
COALESCE(
(
SELECT TOP 1 0
FROM TbUserParam
WHERE TbUserParam.ID_User = :ID_User
AND TbUserParam.ID_Param = CfgListParIzm.ID_ListParIzm
), 1) Visi
FROM CfgListParIzm
WHERE CfgListParIzm.ID_ListGroupParIzm = :ID_ListGroupParIzm
error :
Message 102, Level 15, State 1, line 6
Incorrect syntax near the construction ":".
But ... sure >_< I need to remake it as a Procedure, Thank you.
SELECT Name,
COALESCE(
(
SELECT TOP 1 0
FROM TbUserParam
WHERE ID_User = :ID_User
AND ID_ListGroupParIzm = :ID_ListGroupParIzm
), 1) Visi
FROM CfgListParIzm
WHERE ID_ListGroupParIzm = :ID_ListGroupParIzm