This question already has answers here:
SQL 2008 VS 2012 Error: Incorrect syntax near the keyword 'COMPUTE'
(3 answers)
Closed 4 years ago.
I am trying to execute this simple statement on Northwind database
USE Northwind
SELECT productid, orderid,quantity
FROM [order details]
ORDER BY productid, orderid
COMPUTE SUM(quantity)
GO
But I this can't execute , I got this error
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'COMPUTE'.
The COMPUTE clause is no longer supported in SQL Server 2012. The documentation suggests using ROLLUP instead.
Simple way to do compute:
Select columnname, count(*)
From tablename
Group by columnname
Order by columnname
Select 'SUM' , COUNT(columnname) from tablename
Related
I'm busy teaching myself SQL Server, using SQL Server Management Studio. Here's the code producing the error:
SELECT SalesPersonID, COUNT(SalesOrderID), YEAR(OrderDate) AS SalesYear
FROM Sales.SalesOrderHeader
GROUP BY SalesYear;
Why does it throw this error?
Invalid column name 'SalesYear'.
Invalid column name 'SalesYear'.
This column will not be there in table SalesOrderHeader
SELECT SalesPersonID, COUNT(SalesOrderID), YEAR(OrderDate) AS SalesYear
FROM Sales.SalesOrderHeader GROUP BY YEAR(OrderDate)
This has to do with logical query processing model..Sales year is called an alias here and as per logical query processing model,below are the operators that are executed in sequence..
1 FROM
2 WHERE
3 GROUP BY
4 HAVING
5 SELECT
5.1 SELECT list
5.2 DISTINCT
6 ORDER BY
7 TOP / OFFSET-FETCH
so ,in above steps ,group by will be executed in 3rd stage and your select will be executed in 5th stage..
This means your alias(which is in select stage) ,will be visible to operators following select (after 5),but not before them..
I would suggest taking a book which teaches basics well and i found Itzik Ben-Gan books to be extremely helpfull T-SQL Fundamentals Third Edition
You can try the following query and test to get your expected result.
DECLARE #SalesOrderHeader TABLE(SalesPersonID INT,SalesOrderID INT,OrderDate DATETIME)
INSERT INTO #SalesOrderHeader
SELECT 1,101,'20-Mar-2018' UNION ALL SELECT 1,102,'21-Mar-2018' UNION ALL SELECT 1,102,'21-Mar-2018' UNION ALL SELECT 2,202,'21-Mar-2018'
SELECT * FROM #SalesOrderHeader
SELECT SalesPersonID AS SalesPersonID, COUNT(SalesOrderID) NoOfOrder, YEAR(OrderDate) AS SalesYear
FROM #SalesOrderHeader
GROUP BY SalesPersonID,YEAR(OrderDate);
Thanks.
This question already has answers here:
GROUP BY / aggregate function confusion in SQL
(5 answers)
Closed 5 years ago.
I have SQL code like this :
SELECT
TB_DataProperti.*,
TBL_Rating.ISNULL(AVG(Rating), 0) AverageRating
FROM
TB_DataProperti
INNER JOIN
TBL_Rating ON TB_DataProperti.Kode_Properti = TBL_Rating.Kode_Properti
GROUP BY
TB_DataProperti.JudulListing
ORDER BY
AverageRating DESC
I get this error:
Msg 8120, Level 16, State 1, Line 3
Column 'TB_DataProperti.Kode_Properti' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I just want to select all data columns using *, because I have many columns
Problem is You are trying to use aggregate function of one table and group by on another table.The rule is if you are using aggregate function with another column then that column should use in group by.Still try this I hope this is useful.
SELECT
TB_DataProperti.*,
ISNULL(AVG(TBL_Rating.Rating), 0) over (partition by TBL_Rating.Kode_Properti) as AverageRating
FROM
TB_DataProperti
INNER JOIN
TBL_Rating ON TB_DataProperti.Kode_Properti = TBL_Rating.Kode_Properti
ORDER BY
AverageRating DESC
This question already has an answer here:
How can I implement this plan in C# with SQL command?
(1 answer)
Closed 7 years ago.
I'm write this query:
INSERT INTO Customer_Care_Database_Analysis_Center
SELECT code_markaz, name_markaz
FROM Customer_Care_Database_Analysis_TEMP
WHERE NOT EXISTS (SELECT 1
FROM Customer_Care_Database_Analysis_TEMP ti
WHERE ti.code_markaz = Customer_Care_Database_Analysis_Center.code_markaz);
but when I run that query I get this error:
Msg 4104, Level 16, State 1, Line 6
The multi-part identifier "Customer_Care_Database_Analysis_Center.code_markaz" could not be bound.
What happened? How can I solve that?
Not sure what's happening with the answers, but I would assume you want to insert the rows that don't exist in the table (field code_markaz) and that you can do this way:
INSERT INTO Customer_Care_Database_Analysis_Center (code_markaz, name_markaz)
SELECT code_markaz, name_markaz
FROM Customer_Care_Database_Analysis_TEMP tmp
WHERE NOT EXISTS (SELECT 1
FROM Customer_Care_Database_Analysis_Center c
WHERE c.code_markaz = tmp.code_markaz)
I also added the columns in the insert statement. If there's only 2 columns in the table it will work without them.
The table being inserted into (Customer_Care_Database_Analysis_Center) does not have a column called code_markaz.
Name the target columns:
INSERT INTO Customer_Care_Database_Analysis_Center (some_col_1, some_col_2)
SELECT code_markaz, name_markaz
FROM ...
The error comes due to Customer_Care_Database_Analysis_Center table not reference in you not exist inner query.
As above suggested you have to give either alias or join that table.
...FROM Customer_Care_Database_Analysis_TEMP t
JOIN Customer_Care_Database_Analysis_Center ccdac
NOT EXISTS (
SELECT 1
FROM Customer_Care_Database_Analysis_TEMP ti
WHERE ti.code_markaz = CCDAC.code_markaz);
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
This question already has answers here:
When to use with clause in sql
(2 answers)
Closed 9 years ago.
Please anybody can help me to understand the use of WITH clause
WITH T(Id) AS
(SELECT DISTINCT
[IO].[IncidentQuestionId]
FROM
[dbo].[IncidentValue] AS IV
INNER JOIN
[dbo].[IncidentOption] AS [IO] ON [IV].[IncidentOptionId] = [IO].[Id]
WHERE
[IV].[IncidentId] = 45
)
SELECT
IQ.*, IC.[Name] AS [IncidentCategory]
FROM
IncidentQuestion] AS IQ
INNER JOIN
T ON [T].[Id] = IQ.[Id]
INNER JOIN
[dbo].[IncidentCategory] AS IC ON IQ.[IncidentCategoryId] = IC.[Id]
WHERE
IQ. [IsOption] = 0
ORDER BY
IC.[OrderId] ASC, IQ.[OrderId] ASC
I have the query above which uses the with clause but im not aware of the use of with clause.
thanx
It's a syntax for Common Table Expressions. Read documentation Using Common Table Expressions
It is used to create a Common Table Expression, which returns a result of a SELECT statement which then can be used in yet another SQL statement.
I use them to produce better readable SQL code.
For sometimes,WITH clause is used to force a query to use an Index.