SQL Server 2016 virtual tables - Incorrect syntax 'AS' - sql-server

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

Related

Power BI query error : Microsoft SQL: Incorrect syntax near ';'. Incorrect syntax near ')'

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

Microsoft Sql Server Management Studio invalid in the select list

I was trying to search from my table and use group by but I was receiving error while executing the query. below is my query and the error I am getting:
select * from loans where ac_no='100001' group by ac_no, branch
and the error:
Msg 8120, Level 16, State 1, Line 1
Column 'loans.gl_no' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Please, what am I doing wrong
If you are trying to aggregate something...
select ac_no, branch, count(*)
from loans
where ac_no = '100001'
group by
ac_no
, branch
If you are just trying to select rows, you do not need group by
select *
from loans
where ac_no = '100001'
If you are trying to select distinct rows...
select distinct *
from loans
where ac_no = '100001'

Why i get error when want insert to table in sql server?

I'm beginner in write query,i write this query for insert any data to table:
insert into ScoreTable (UserID,Score,PhoneNumber,date_)
select id,sum(Duration) / 60 ,phoneNumber,Date_ from CDR p
where p.phoneNumber=phoneNumber
but when i execute the that query,i get this error:
Msg 8120, Level 16, State 1, Line 27
Column 'CDR.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How can i solve that problem?
Your issue is with the select statement. Run that independently and you will get the same issue. Because you're aggregating (SUM) you require a group by clause. Give this a go;
SELECT p.id
,sum(p.Duration) / 60
,p.phoneNumber
,p.Date_
FROM CDR p
WHERE p.phoneNumber = phoneNumber
GROUP BY p.id, p.phoneNumber, p.Date
Also remember to use your table aliases in the queries to avoid ambiguity.

SQL-Server 2005: multi-part identifier could not be bound

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

Enable to count the rows of a subquery SQL Server 2008

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

Resources