how to use with and inner join in Sql server query? - inner-join

Why this query must be incorrect ? :
with a as
(
select *
from justification_game_publisher
inner join justifications
on justifications.id = justification_id
where game_id=1594
)
select *
from games_publisher_class
inner join a
on a.game_id = games_publisher_class.game_id
and a.publisher_id = games_publisher_class.publisher_id
sql server says :
Msg 319, Level 15, State 1, Line 2: 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.

I think you have additional sql expression before.
Try
;with a as (....
instead of
with a as (....
I hope this should help :)

Related

Amazon-Quicksight - Syntax for SQL Server CTE

I am inserting a custom SQL into QS to query my DB; the query contains a CTE and some selection from the same like below:
with cte as
(
.....
)
select *
from cte;
Whenever I run the query in QS, I get a syntax error.
sourceErrorMessage: Incorrect syntax near the keyword 'with'
Any help on the syntax or whether CTEs are supported or not by QS would be helpful.
Thank you.

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.

SQL Server 2005 PIVOT Syntax Error

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.

tsql : how do you query with dashes in column names?

I have the query
select * from products p, products_temp t
where p.ManufacturerPartNumber = t.[INV-PRICE-VENDOR-PART]
where the column names have dashes in them which SQL Server 2005 seems to automatically add brackets to. What is the correct way of accessing this in a query? I've tried with brackets and without the brackets and just end up with errors.
the error I get from sql mgmt studio is
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'where'.
thanks in advance
that is because you have repeated WHERE twice in your statement. Got nothing to do with the square brackets which you will need because of the dashes.
use "current" join syntax:
SELECT
*
from products p
INNER JOIN products_temp t ON p.ManufacturerPartNumber = t.[INV-PRICE-VENDOR-PART]

Resources