I'm getting the following error message :
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ')'.
while trying to run the following query :
select count(*) as count from
(select distinct
POSITION,
ACQUIESCEMENT,
CODE_LIMITE_NATURELLE
from avoisine where num_dossier=2 and indice='29' )
What is the problem with my query ?
Thanks in advance.
You were missing alias name
select count(*) as [count] from
(select distinct
POSITION,
ACQUIESCEMENT,
CODE_LIMITE_NATURELLE
from avoisine where num_dossier=2 and indice='29' ) A -- Missing
Related
I've got a practice question for a SQL user exam that is asking me to return three column from a combination of four different tables, but I'm only returning the first row in time for patient ID's that are in a group by statement.
Here's what I have:
select p.patient_nm as 'Patient Name', d.code as 'Diagnosis Code', convert(date, e.start_dts) as 'Encounter Date'
from
(with added_row_number as (select *, row_number() over(partition by patient_id order by start_dts) as row_number
from encounters
)
select encounter_id, patient_id, start_dts
from added_row_number
where row_number=1) e
left join edw_emr_ods.patients p
on e.patient_id=p.patient_id
left join edw_emr_ods.encounter_diagnoses ed
on e.encounter_id=ed.encounter_id
left join edw_emr_ods.diagnoses d
on ed.diagnosis_id=d.diagnosis_id
It seems to have a problem with this portion of the code, which I got and adapted from some other site, when I searched for info on how to return just the first row in a 'group-by' group
with added_row_number as (select *, row_number() over(partition by patient_id order by start_dts) as row_number
from encounters
)
select encounter_id, patient_id, start_dts
from added_row_number
where row_number=1
I don't get any errors when I run just this piece, and it returns the bare minimum I need to join the three other tables, so I can return the three required variables. Here's what it looks like when I run just that piece:
But these are the errors I'm getting when I run the whole thing:
Msg 156, Level 15, State 1, Line 280
Incorrect syntax near the keyword 'with'.
Msg 319, Level 15, State 1, Line 280
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 102, Level 15, State 1, Line 285
Incorrect syntax near ')'.
I've tried inserting a lone semicolon and a couple different places, but none of them work.
This code seems like it should be one of the most computationally efficient ways of returning this table.
If this isn't the way to go, please let me know what the best code would be, preferably without the use of a temporary table.
Thanks
EDIT: based on a comment, I'm trying to change that smaller piece of code into a subquery, but am still getting an error:
(select encounter_id, patient_id, start_dts, row_number() over(partition by patient_id order by start_dts) visitnum
from edw_emr_ods.encounters
where visitnum=1) e
Produces the error:
Msg 207, Level 16, State 1, Line 298
Invalid column name 'visitnum'.
If I omit the where clause, the subquery works, but it returns all of the rows, when I need just those when 'visitnum' is equal to 1.
with is a common table expression, and it can't be used inside a sub-query. Just convert it to a regular sub-query.
And you can't reference a column alias in the same context as you create it, aside for in an order by. So you need an additional sub-query/derived table.
(
select encounter_id, patient_id, start_dts
from (
select encounter_id, patient_id, start_dts, row_number() over (partition by patient_id order by start_dts) visitnum
from edw_emr_ods.encounters
) e1
where visitnum = 1
) e
Assuming a table level with columns level_id and level_name
I have a virtual table in my SQL Server 2016 query:
This is not a real query - I've simplified it as much as possible to illustrate the error
SELECT
LEVEL .level_id,
LEVEL .level_name
FROM
LEVEL,
((SELECT
LEVEL_ID AS lev_sum_level_id
FROM
LEVEL
GROUP BY
level_id) AS lev_sum
JOIN
(SELECT
LEVEL_ID AS lev_det_level_id
FROM
LEVEL
GROUP BY
level_id) AS lev_det ON (lev_sum_level_id = lev_det_level_id)
) AS totals
The syntax error is on the line AS totals.
Msg 156, Level 15, State 1, Line 35
Incorrect syntax near the keyword 'AS'
Why is SQL Server not allowing this syntax? Seems fine if I simplify the virtual table query. Postgres allows it as-is
I think you can try to add a select so that you can complete the syntax of the statement
Please try
SELECT
T1.MiscID,
T2.lev_sum_level_id
FROM
Miscs AS T1,
(
SELECT * FROM
(
SELECT
MiscID AS lev_sum_level_id
FROM
Miscs
GROUP BY
MiscID
) AS lev_sum
JOIN
(
SELECT
MiscID AS lev_det_level_id
FROM
Miscs
GROUP BY
MiscID
) AS lev_det
ON( lev_sum_level_id = lev_det_level_id )
) AS T2
The following runs well in SSMS. However in Power BI, I am getting this error and do not know why:
Microsoft SQL: Incorrect syntax near ';'.
Incorrect syntax near ')'.
Database = AdventureWorks
Query is:
;WITH Totals AS
(
SELECT
ProductCategoryID, COUNT(*) AS TotalProducts
FROM
SalesLT.Product
GROUP BY
ProductCategoryID
),
Flat AS
(
SELECT
PC.ParentProductCategoryID, PC.ProductCategoryID,
PC.[Name], Totals.TotalProducts
FROM
SalesLT.ProductCategory PC
INNER JOIN
Totals ON (PC.ProductCategoryID = Totals.ProductCategoryID)
),
Grand AS
(
SELECT
Flat.ParentProductCategoryID,
SUM(Flat.TotalProducts) AS [GrandTotal]
FROM
Flat
GROUP BY
Flat.ParentProductCategoryID
)
SELECT
Grand.ParentProductCategoryID, Grand.GrandTotal, PC.[Name]
FROM
Grand
INNER JOIN
SalesLT.ProductCategory PC ON (Grand.ParentProductCategoryID = PC.ProductCategoryID);
I'm working with SQL Server 2005, I'm new to SQL so bear with me.
The following aliased SQL query is giving me the following error:
The multi-part identifier "EquipmentDescription.DESCRIPTION" could not be bound.
SQL:
WITH somerows as
(
SELECT
Mastertable.ID, Mastertable.foo1, Mastertable.foo2,
Mastertable.foo3, EquipmentDescription.DESCRIPTION,
ROW_NUMBER() OVER (ORDER BY Mastertable.ID) AS SeqValue
FROM
EquipmentDescription
LEFT JOIN
MasterTable ON EquipmentDescription.foo1 = MasterTable.foo1
ORDER BY
EquipmentDescription.DESCRIPTION
)
SELECT *
FROM somerows
WHERE SeqValue BETWEEN 0 and 20
Background: Mastertable has 60,000+ records. I'm using WITH...as...etc to request 20 records at a time on the server side.
The DESCRIPTION column of EquipmentDescription for the purposes of design is not included in Mastertable. It is a requirement to include DESCRIPTION in the final select.
Any ideas on what I'm doing wrong?
This works:
SELECT * FROM
(
SELECT
ROW_NUMBER() OVER(ORDER BY ID) AS NUMBER,
Mastertable.ID, Mastertable.foo1, Mastertable.foo2,
Mastertable.foo3, EquipmentDescription.DESCRIPTION,
FROM
EquipmentDescription
RIGHT JOIN
MasterTable ON EquipmentDescription.foo1 = MasterTable.foo1
) AS t_MasterTable
WHERE
NUMBER BETWEEN 0 and 20
ORDER BY ID
I want a Sql Script Trigger on after insert, Which can Delete all Rows in table except Last Two Rows.
And I Want Similar Code in Entity linq too Please...
I know my question is cheap for you experts but i really do not know how to code it...
So help me please?
I Use This Code But Has error in syntax :
select * from TblGold where id not in (
select id from TblGold
order by id desc
limit 2
)
The Error Is :
Msg 102, Level 15, State 1, Line 4 Incorrect syntax near 'limit'.
By the way, I know how to write a trigger but do not know how to Delete All Records Except 2 Last record.
And nither Linq Codes.
WITH cte
AS
(
SELECT ROW_NUMBER() OVER(ORDER BY Id DESC) AS RowNumber, *
FROM TblGold
)
DELETE TblGold
FROM
TblGold T
INNER JOIN cte ON cte.Id = T.Id
WHERE RowNumber > 2