SQL Server 2005 PIVOT Syntax Error - sql-server

I receive the following error
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near ')'.
When I run the following query
SELECT TOP 100 *
FROM
(
SELECT [TimeStamp],[MeterID],[Value]
FROM access_AMIData) AS source
PIVOT
(
SUM(Value)
FOR MeterID IN (SELECT MeterNumber FROM access_tblFcppPvMeterList)
) as pvt
ORDER BY TimeStamp
The error comes with this line:
FOR MeterID IN (SELECT MeterNumber FROM access_tblFcppPvMeterList)
If I change this line to:
FOR MeterID IN (1,2,3,4)
It works perfectly...how can I specify a select Query inside the IN?

You can't, at least not in SQL 2005. You could include it in the WHERE clause of your inner query, but you have to hard-code the column names, unless of course you use dynamic SQL.

Related

How to execute table valued function in SQL Server

I'm getting syntax error when trying to execute table valued function.
select * from fn_security(select R.rk from dbo.LINK R)
Error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
What am I doing wrong? how to execute this?
You can't pass whole table,like the way,you are trying now..you can use cross apply to get all
you can try below
select * from dbo.LINK r
cross apply
dbo. fn_security(r.rk)

Error with FROM clause containing a subquery

I was running this query in MSSQL:
SELECT * FROM (SELECT * FROM ABC)
It gives an error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
This same command runs just fine on a DB2 database. I know this query doesn't make any sense I was just testing functionality.
So, are there certain features, eg., SELECT in FROM clause, that are not supported in MSSQL that are supported in DB2?
You just need to give the subquery an alias like so:
SELECT * FROM (SELECT * FROM ABC) subTable
Which translates to:
SELECT * FROM (SELECT * FROM ABC) as subTable
The AS is optional.

Except operation in TSQL returning error

I need to apply an EXCEPT (same as MINUS in Oracle) operation in TSQL. I tried with a simple code
select * from Table1
except
select * from Table1 where calndr_dt between '2014-10-01' and '2014-10-10'`
Each sub query is executing fine. But when joined with EXCEPT, it is returning the following error message.
Msg 103010, Level 16, State 1, Line 1
Parse error at line: 2, column: 1: Incorrect syntax near 'except'.
Remove the ORDER BY clause from the first query, if you have this clause.

How can I move one of two perfectly identical rows into a new table through a query?

MS SQL Server 2008
I Use following Query to filter duplicate rows into a single row
Query 1 - SELECT DISTINCT * FROM flatfile_old
But now i want to move those query results into a new table and i try following things
Query 2 - SELECT * INTO flatfile_new FROM Flatfile_old WHERE 1 = 2
Query 3 - INSERT INTO flatfile_new (SELECT DISTINCT * FROM flatfile_old)
But Query No 3 throws error like follows
Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword
'select'. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near
')'.
Please help
Just remove the brackets ( ... ) like this:
INSERT INTO flatfile_new
SELECT DISTINCT * FROM flatfile_old

MS SQL server 2005 - Error while querying with a column name as key

I have a table with a column name as "key". I am unable to filter based on that column
select * from myTable where key='someVal'
I get the following error
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'key'.
I cannot change the column name. How I can circumvent this issue?
It's because key is a keyword. If you have keywords as object names, you need to put them in brackets:
select * from myTable where [key]='someVal'

Resources