Incorrect syntax near the keyword 'rowcount' - sql

I have this code:
merge dbo.tblProblems as target
using (
select max(problemID), StationName, problemCode, max(ProblemCreateDate), count(*)
from dbo.tblProblems
group by StationName, problemCode
) as source(id, StationName, problemCode, maxdate, rowcount)
on (
target.problemID = source.problemID
and target.StationName = target.StationName
and target.problemCode = target.problemCode
)
when matched then
update set ProblemCreateDate = maxdate, probCount = rowcount
when not matched then delete ;
But it yields this error:
Msg 156, Level 15, State 1, Line 6Incorrect syntax near the keyword 'rowcount'.
And this version of the query:
;WITH n AS
(
SELECT problemID, StationName, problemCode, ProblemCreateDate, probCount
c = COUNT(*) OVER (PARTITION BY StationName, problemCode),
rn = ROW_NUMBER() OVER
(
PARTITION BY StationName, problemCode ORDER BY ProblemCreateDate DESC, problemID DESC
)
FROM dbo.tblProblems
)
--SELECT problemID, StationName, problemCode, ProblemCreateDate, c
-- FROM n WHERE rn = 1;
UPDATE n SET probCount = c
WHERE rn = 1;
Yields this error:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '='.

For the first query, rowcount is a reserved keyword. Use a different word (e.g. row_count) or surround it in square brackets:
[rowcount]
For the second query, you need a comma after probCount:
SELECT problemID, StationName, problemCode, ProblemCreateDate, probCount,
------------------------------------------------------------------------^
c = COUNT(*) OVER (PARTITION BY StationName, problemCode),
You didn't quite adapt my code sample correctly (the trailing comma is there).

Related

SQL Server - Incorrect syntax near the keyword 'AS'

This is my code
WITH cte AS
(
SELECT
nr_CRM,
CLASSIFIC,
COUNT(*) OVER (PARTITION BY nr_CRM) AS total_cnt
FROM tmp_usr..tb
)
SELECT
COUNT(DISTINCT(nr_CRM) AS CRM_total
FROM
cte
WHERE
total_cnt = 3
AND CLASSIFIC = 'GOLD'
AND dt_Visita >= '2021-01-25' AND dt_Visita <= '2021-02-26'
It was written on SQLite, but now I need to use SQL Server and it doesn't work.
On line 10, it displays the following error:
Incorrect syntax near the keyword 'AS'.
How can I adjust it?
there is an extra opening parenthesis :
WITH cte AS (
SELECT
nr_CRM,
CLASSIFIC,
COUNT(*) OVER (PARTITION BY nr_CRM) as total_cnt
FROM tmp_usr..tb
)
SELECT
COUNT(DISTINCT nr_CRM) AS CRM_total -- <-- here
FROM cte
WHERE
total_cnt = 3
AND CLASSIFIC = 'GOLD'
AND dt_Visita >= '2021-01-25'
and dt_Visita <= '2021-02-26'

SELECT query using group by

I don't know why the following query is not working:
SELECT whs_code, pdt_code,case_dt_yyyymmdd, fresh_frozen_status,
SUM(qty_cases_on_hand)-Qty, SUM(qty_weight_on_hand)-Wt, operation
FROM
(
SELECT whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation,SUM(qty_cases_on_hand) AS Qty, SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='RU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
)
WHERE operation='SU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation`
The error is :
Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'WHERE'.
To make it simple to understand what i am trying to do here, please see the example
I need data as result of
(SELECT x FROM tbl_table Where column y='SU')-(SELECT x FROM tbl_table Where column y='RU')
SELECT ru.whs_code,
ru.pdt_code,
ru.case_dt_yyyymmdd,
ru.fresh_frozen_status,
ru.operation,
ru.Qty - su.Qty AS Qty_Diff,
ru.Wt - su.Wt AS Wt_Diff
FROM
(
SELECT whs_code,
pdt_code,
case_dt_yyyymmdd,
fresh_frozen_status,
operation,
SUM(qty_cases_on_hand) AS Qty,
SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='RU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
) ru,
(
SELECT whs_code,
pdt_code,
case_dt_yyyymmdd,
fresh_frozen_status,
operation,
SUM(qty_cases_on_hand) AS Qty,
SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='SU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
) su
WHERE ru.whs_code = su.whs_code
AND ru.pdt_code = su.pdt_code
AND ru.case_dt_yyyymmdd = su.case_dt_yyyymmdd
AND ru.fresh_frozen_status = su.fresh_frozen_status
AND ru.operation = su.operation;

Trying to calculate cummulative sum in tsql

What am I doing wrong here?
The result is an error, saying:
Msg 102, Level 15, State 1, Line 3 Incorrect syntax near 'order'.
Msg 156, Level 15, State 1, Line 25 Incorrect syntax near the keyword
'as'.
select *
, Antal + Normtid as Flextid
, SUM(antal) OVER (PARTITION BY transdate ORDER BY tekst)
, x = row_number() over (partition by åruge order by tekst)
from
(
select *
,
(
select b.antal
from bi.dbo.Table_pg_FlextidsopgørelseGlUdgave b
where b.tekst = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.tekst
and b.transdate = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.transdate
and b.åruge = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.åruge
and b.type = 'Normtid'
) as Normtid
from
bi.dbo.Table_pg_FlextidsopgørelseGlUdgave
where type = 'afholdt'
and tekst = 'fs'
--and åruge = '201501'
) as data
order by tekst, transdate
Regards
Peter
It is obvious that you have inappropriate version of Sql Server. Cumulative sums with order by clause like:
SUM(antal) OVER (PARTITION BY transdate ORDER BY tekst)
are only available from Sql Server 2012+.
Actually I can reproduce those errors on Sql Server 2008:
This is on Sql Server 2012:
Notice how the error message changes.
The way you are getting data from derived table is not correct..
EX:
create table
sales
(
id int
)
insert into sales
values
(1),
(2),
(3)
derived table should always have table alias and parent table should refer using from
----this is valid
select
* from
(
select id+1 as id1
from sales
) b
--this is not valid
select
*
(select
id from sales
)b
--Above is valid when you have a subquery say
select
id,(select t1.name from table t1 where t1.id=t2.id)as custname
from table t2
coming to your question..this is not valid.I see both table types are same
select *
,
(
select b.antal
from bi.dbo.Table_pg_FlextidsopgørelseGlUdgave b
where b.tekst = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.tekst
and b.transdate = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.transdate
and b.åruge = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.åruge
and b.type = 'Normtid'
) as Normtid
from
bi.dbo.Table_pg_FlextidsopgørelseGlUdgave
where type = 'afholdt'
and tekst = 'fs'
--and åruge = '201501'
So you can write something like below
select *
, Antal + Normtid as Flextid
, SUM(antal) OVER (PARTITION BY transdate ORDER BY tekst)
, x = row_number() over (partition by åruge order by tekst)
from
(
select * from
(
select b.antal
from bi.dbo.Table_pg_FlextidsopgørelseGlUdgave b
where b.tekst = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.tekst
and b.transdate = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.transdate
and b.åruge = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.åruge
and b.type = 'Normtid'
and b.type='afholdt'
and b.tekst = 'fs'
) as Normtid
order by tekst, transdate

Over(Partition by ) Column not being recognized. Invalid column

When I execute the following query:
SELECT
MH.MemberKey,
ROW_NUMBER() OVER(PARTITION BY MH.MemberKey ORDER BY MH.MemberKey ASC) AS "NewRow",
MH.FirstName,
MH.LastName,
MH.BirthDate,
MH.AddressLine1,
MH.AddressLine2,
MH.AddressLine3,
MH.City AS MemberCity,
MH.StateCode AS MemberState,
MH.ZipCode AS MemberZip
FROM MembershipHistory MH
WHERE NewRow = 1;
I get the following error:
Msg 207, Level 16, State 1, Line 36
Invalid column name 'NewRow'.
I tried keeping AS, removing AS, removing ""....nothing seems to work. It just wont recognize the column.
The evaluation of the row_number function in the SELECT is done after the WHERE clause has been applied so the alias is not recognized, you can use a subquery to get the result:
select
MH.MemberKey,
NewRow,
MH.FirstName,
MH.LastName,
MH.BirthDate,
MH.AddressLine1,
MH.AddressLine2,
MH.AddressLine3,
MH.City AS MemberCity,
MH.StateCode AS MemberState,
MH.ZipCode AS MemberZip
from
(
SELECT
MH.MemberKey,
ROW_NUMBER() OVER(PARTITION BY MH.MemberKey ORDER BY MH.MemberKey ASC) AS NewRow,
MH.FirstName,
MH.LastName,
MH.BirthDate,
MH.AddressLine1,
MH.AddressLine2,
MH.AddressLine3,
MH.City AS MemberCity,
MH.StateCode AS MemberState,
MH.ZipCode AS MemberZip
FROM MembershipHistory MH
) MH
WHERE NewRow = 1;
In SQL Server the Logical Processinf Order of the SELECT Statement is (from MSDN Docs):
FROM
ON
JOIN
WHERE
GROUP BY
WITH CUBE or WITH ROLLUP
HAVING
SELECT
DISTINCT
ORDER BY
TOP
Another approach is to use a CTE:
;WITH x AS (SELECT MH.whatever, ROW_NUMBER() ... AS NewRow FROM dbo.table)
SELECT * FROM x WHERE NewRow = 1;

Works in SQLFiddle Not in SQL Server 2012

When I run this Query in SQL Fiddle it runs perfectly:
;with cte as
(SELECT
analysisvalue.analysisid,
heatname,
analysistime,
sampletype,
grade,
productid,
element,
value
FROM
dbo.AnalysisValue
INNER JOIN
dbo.CAnalysis
ON
dbo.AnalysisValue.AnalysisID = dbo.CAnalysis.AnalysisID
WHERE
heatname = 'A7M0066'
)
SELECT
*
FROM
S_analysis s
CROSS JOIN (SELECT TOP 1 analysistime
FROM cte
ORDER BY analysisid desc
) c
WHERE s.heat_no = 'A7M0066' OR
(s.analysis_datetime BETWEEN c.analysistime AND DATEADD(hh, 2, c.analysistime ))
However when I run it in SQL Server 2012, I receive this error with the leading semi-colon:
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ';'.
And this error when without the semicolon:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'with'.
Instead of a CTE, you could use a #temp table and see if the query is valid:
SELECT
analysisvalue.analysisid,
heatname,
analysistime,
sampletype,
grade,
productid,
element,
value
INTO #temptable
FROM
dbo.AnalysisValue
INNER JOIN
dbo.CAnalysis
ON
dbo.AnalysisValue.AnalysisID = dbo.CAnalysis.AnalysisID
WHERE
heatname = 'A7M0066';
SELECT *
FROM
S_analysis s
CROSS JOIN (SELECT TOP 1 analysistime
FROM #temptable
ORDER BY analysisid desc
) c
WHERE s.heat_no = 'A7M0066' OR
(s.analysis_datetime BETWEEN c.analysistime AND DATEADD(hh, 2, c.analysistime ))