I have to convert one oracle query to snowflake,which has a where clause LEVEL > 1. Could you please suggest me the best option.
Thanks.
I don't think it's an exact match, but the closest thing is the "start with" clause of Snowflake's connect by:
SELECT <column_list> [ , <level_expression> ]
FROM <data_source>
START WITH <predicate>
CONNECT BY [ PRIOR ] <col1_identifier> = [ PRIOR ] <col2_identifier>
[ , [ PRIOR ] <col3_identifier> = [ PRIOR ] <col4_identifier> ]
...
...
You can provide a where clause on the start with predicate, but without the "where" keyword. You can read more about it here: https://docs.snowflake.com/en/sql-reference/constructs/connect-by.html
There is level in snowflake. The differences from Oracle are:
In snowflake it's neccesary to use prior with connect by expression.
And you can't just select level - there should be any existing column in the select statement.
Example:
SELECT LEVEL, dummy FROM
(select 'X' dummy ) DUAL
CONNECT BY prior LEVEL <= 3;
LEVEL DUMMY
1 X
2 X
3 X
4 X
Related
I get an execution error in following SQL script:
SELECT TOP 1 PERCENT
a.accode, a.voucherdate, a.credit, a.Debit,
SUM(a.Debit) OVER (ORDER BY [a.accode],[a.voucherdate]) AS rdr
FROM
VoucherMain AS a
ORDER BY
a.accode, a.voucherdate
Error message
Incorrect syntax near 'order'
Can anyone tell me what's wrong with my syntax?
The problem is that you need SQL Server 2012 and above. Okay, I added the "and above" for future visitors, but compare 2008 OVER CLAUSE with 2012 OVER CLAUSE.
The 2008 version has this important note:
When used in the context of a ranking window function, <ORDER BY
Clause> can only refer to columns made available by the FROM clause.
An integer cannot be specified to represent the position of the name
or alias of a column in the select list. <ORDER BY Clause> cannot be
used with aggregate window functions.
In SQL Server 2008, you can only use the OVER clause to partition aggregate functions, not apply an order:
Ranking Window Functions
< OVER_CLAUSE > :: =
OVER ( [ PARTITION BY value_expression , ... [ n ] ]
< ORDER BY_Clause> )
Aggregate Window Functions
< OVER_CLAUSE > :: =
OVER ( [ PARTITION BY value_expression , ... [ n ] ] )
Note that there's no <ORDER BY Clause> for the aggregates.
I want to see the definition of a table in SQL Server.
Running this query from SQLPro for MSSQL is OK
SELECT TOP 100 * FROM dbo.[ATRESMEDIA Resource Time Registr_];
but when I run this one
exec sp_columns dbo.[ATRESMEDIA Resource Time Registr_];
I got this error:
Msg 102, Level 15, State 1.
Incorrect syntax near '.'. (Line 3)
dont use schema dbo.
exec sp_columns [ATRESMEDIA Resource Time Registr_];
why? because, following are the parameters accepted by sp_columns stored proc:
sp_columns [ #table_name = ] object
[ , [ #table_owner = ] owner ]
[ , [ #table_qualifier = ] qualifier ]
[ , [ #column_name = ] column ]
[ , [ #ODBCVer = ] ODBCVer ]
source: msdn
update:
Martin's explanation as in comment:
Strings in SQL Server are delimited by single quotes - as a parameter to a stored proc in very limited circumstances it will allow you to skip the quotes but the dot breaks that. exec sp_columns 'dbo.[ATRESMEDIA Resource Time Registr_]'; wouldn't give the syntax error - but that wouldn't be what the proc expects anyway as the schema would need to be the second param
select the table name in the query window
and press the below key combination
Alt +F1 or
Alt+Fn+F1 will bring the table definition
I'm looking at a section of "A Guide to the SQL Standard" (C.J.Date/Hugh Darwen) concerning LEFT JOIN and it gives the following syntax:
table-reference [ NATURAL ] outer-join-type
JOIN table-reference
[ ON conditional-expression
| USING ( column-commalist ) ]
What is USING?
Is it useful?
Is it implemented in SQL-Server?
No, it's not supported by SQL Server, see FROM:
<joined_table> ::=
{
<table_source> <join_type> <table_source> ON <search_condition>
| <table_source> CROSS JOIN <table_source>
| left_table_source { CROSS | OUTER } APPLY right_table_source
| [ ( ] <joined_table> [ ) ]
}
As to what it is? It's a way of performing a join between two tables where the column names in both tables exactly match, and as a convenience, it allows you to name the columns only once. Compare that to using ON to achieve the same:
ON
table1.columnA = table2.columnA AND
table1.columnB = table2.columnB AND
table1.columnC = table2.columnC
with USING it's just:
USING (columnA,columnB,columnC)
No, the USING clause for joins isn't supported in SQL Server (or Sybase). You'll need to continue to use ON clauses.
There's a request on MSDN connect to have it implemented here - but as it was suggested in 2006 and not implemented yet, I wouldn't hold your breath!
I get an execution error in following SQL script:
SELECT TOP 1 PERCENT
a.accode, a.voucherdate, a.credit, a.Debit,
SUM(a.Debit) OVER (ORDER BY [a.accode],[a.voucherdate]) AS rdr
FROM
VoucherMain AS a
ORDER BY
a.accode, a.voucherdate
Error message
Incorrect syntax near 'order'
Can anyone tell me what's wrong with my syntax?
The problem is that you need SQL Server 2012 and above. Okay, I added the "and above" for future visitors, but compare 2008 OVER CLAUSE with 2012 OVER CLAUSE.
The 2008 version has this important note:
When used in the context of a ranking window function, <ORDER BY
Clause> can only refer to columns made available by the FROM clause.
An integer cannot be specified to represent the position of the name
or alias of a column in the select list. <ORDER BY Clause> cannot be
used with aggregate window functions.
In SQL Server 2008, you can only use the OVER clause to partition aggregate functions, not apply an order:
Ranking Window Functions
< OVER_CLAUSE > :: =
OVER ( [ PARTITION BY value_expression , ... [ n ] ]
< ORDER BY_Clause> )
Aggregate Window Functions
< OVER_CLAUSE > :: =
OVER ( [ PARTITION BY value_expression , ... [ n ] ] )
Note that there's no <ORDER BY Clause> for the aggregates.
SELECT * FROM agency
INNER JOIN TUser
[agency].[dbo].[Matrix_Branch_ID]=[TUser].[dbo].[client_id]
Microsoft SQL Server Managment Studio gives me:
SQL Server Management Studio gives me:
Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near '.'.
Edit
After fixing the syntax errors with
SELECT * FROM agency
INNER JOIN TUser
ON dbo.agency.Matrix_Branch_ID=dbo.TUser.client_id
SQL Server Management Studio now gives me:
Msg 208, Level 16, State 1, Line 1
Invalid object name 'agency'.
Msg 208, Level 16, State 1, Line 1
Invalid object name 'TUser'.
If you're running this in SSMS - are you in the right database; the one that contains those two tables?
You can see that current database you're in when a query window is active - both in a drop-down on the toolbar, as well as the query window's footer.
You're missing the ON keyword See the <joined table> grammar in FROM (Transact-SQL)
<joined_table> ::= {
<table_source> <join_type> <table_source> ON <search_condition>
| <table_source> CROSS JOIN <table_source>
| left_table_source { CROSS | OUTER } APPLY right_table_source
| [ ( ] <joined_table> [ ) ] }
Also the [dbo] between what I presume is the table and fields names is wrong. See Using Identifiers As Object Names
this should work
SELECT * FROM agency
INNER JOIN TUser
ON [agency].[Matrix_Branch_ID]=[TUser].[client_id]
Are you selecting the correct database? SQL Server defaults to using the master database, which is not the one you probably want.
You probably want to qualify the schema on the table, not on the items you are selecting. For instance:
Use [Database_name]
SELECT * FROM dbo.agency
INNER JOIN dbo.TUser ON agency.Matrix_Branch_ID=TUser.client_id