Netezza To snowflake migration of function Percent Rank - snowflake-cloud-data-platform

We are trying to migrate the SQL queries from Netezza to Snowflake. And got stuck in Percent_rank function of how differently they are used in both. below is the query getting fired in Netezza
Netezza code
Select
percent_rank(x.id,x.rate) within group(order by k.date)
from emp_sales x, emp_order k
where x.inv = k.inv;
Snowflake Converted Code
Select
percent_rank() over (partition by x.id,x.rate order by k.date)
from emp_sales x, emp_order k
where x.inv = k.inv;
However It is throwing error.
k.date is not a valid group by function.
I am stuck in this as not able to figure out how to replicate this. Please suggest.

The date/timestamp column should be transformed to integer to be used in this context:
SELECT
PERCENT_RANK() OVER (PARTITION BY x.id,x.rate
ORDER BY DATE_PART(EPOCH_SECOND,k.date))
FROM emp_sales x
JOIN emp_order k
ON x.inv = k.inv;

Related

Can you use aggregate functions with Snowflake's connect by?

I basically want to replicate this (from oracle):
https://databaseline.tech/how-to-multiply-across-a-hierarchy-in-oracle-part-1/
In Snowflake using connect by. First of all, I haven't found any documentation that tells me if I can (or how to) use aggregate functions with connect by.
Second, I've tried to replicate the code from the post above in Snowflake:
SELECT LEVEL
,o.id
,o.yield
,(
SELECT ROUND( EXP(SUM(LN(i.yield))), 2 ) -- only two significant digits
FROM hierarchy_example i
START WITH i.id = o.id -- start with the row from the outer query
CONNECT BY i.id = PRIOR i.prior_id -- reverse direction of PRIOR to go back up
) AS yield_to_step
,connect_by_root o.id AS root_id
,sys_connect_by_path(o.id, '->') as path
FROM hierarchy_example o
START WITH o.prior_id IS NULL -- start with anchor rows
CONNECT BY o.prior_id = PRIOR o.ID -- go down hierarchy
But when I run it I get this error:
000603 (XX000): SQL execution internal error:
Processing aborted due to error 370001:416518821; incident 8391142.
So does anyone how to do this in Snowflake or if it is even possible?

Microsoft SQL Server: Error with Group By

I'm new to Microsoft SQL Server 2014. I run this SQL code:
SELECT TOP(10) 'DBSG' as seek_entity, *
FROM DBSG..PM00200
and get this result:
Next, I want to find out total line items for that entity with code below.
WITH vw_pm00200_all AS
(
SELECT TOP(10)
'DBSG' as seek_entity, *
FROM
DBSG..PM00200
)
SELECT
seek_entity,
COUNT(*) AS total
FROM
vw_pm00200_all
GROUP BY
1
Sadly, I get this error. I have no idea why it failed.
Msg 164, Level 15, State 1, Line 9
Each GROUP BY expression must contain at least one column that is not an outer reference.
Lastly, please advise is Microsoft SQL Server based on Transact-SQL?
It looks like you are running into this problem here: Each GROUP BY expression must contain at least one column that is not an outer reference
As the answer points out, grouping by a constant literal is pointless as it is the same for all results. Count(*) will return the same result as Count(*) with a GROUP BY.
If this is just test code and you plan on using a CASE statement (with different values) in place of the string literal, you may have better luck.
Yes, T-SQL is Microsoft SQL Server's flavor of SQL.

I imported two tables into Microsoft SQL Server but one of them is not being detected by intellisense

I Imported 2 tables into Microsoft SQL Server, I am able to perform operations on both the tables but the second table is not being detected by intellisense. I am able to perform queries using the second table, except the below one.
The code that I am trying to execute is this.
SELECT
dea.continent, dea.location, dea.date, dea.population,
vac.new_vaccinations,
SUM(CONVERT(int, vac.new_vaccinations)) OVER (PARTITION BY dea.Location
ORDER BY dea.location, dea.Date) AS RollingPeopleVaccinated,
--, (RollingPeopleVaccinated/population)*100
FROM
PortfolioProject..CovidDeaths dea
JOIN
PortfolioProject..CovidVaccinations vac ON dea.location = vac.location
AND dea.date = vac.date
WHERE
dea.continent IS NOT NULL
ORDER BY
2, 3
I am getting the error that I need to convert the data type of a column, I am doing it using this code SUM(CONVERT(int, vac.new_vaccinations)) but that is not being detected.
What could be the reason?

ORACLE to MSSQL using SSMS Import Wizard with Query to Update Rows

I have a situation which prevent me of updating rows in a table in MSSQL getting the data from ORACLE. I can INSERT fine from ORACLE to MS SQL using a SELECT statement like:
SELECT XRECORDACTIVATIONDATE, XRECORDCUTOFFDATE, XRECORDREVIEWDATE,
XRECORDFILINGDATE, XNOLATESTREVISIONDATE, XNEWREVISIONDATE, XDATERECEIVEDDOC,
XINACTIVEDATE, DCREATEDATE, DINDATE, DRELEASEDATE, DLASTMODIFIEDDATE
FROM STELLENT.V_EXPORT_TO_MSSQL V
But when I try to update the rows based on an unique ID using:
UPDATE D
SET D.XRECORDACTIVATIONDATE = V.XRECORDACTIVATIONDATE
FROM DBO.DOCUMENT D
INNER JOIN STELLENT."V_EXPORT_TO_MSSQL" V ON D.DID = V.DID
I get the following error:
ORA-00933: SQL command not properly ended
(System.Data.OracleClient)
DBO.DOCUMENT is a MSSQL table.
STELLENT.V_EXPORT_TO_MSSQL is a View in ORACLE
I might be writing wrong the query I will appreciate some help. thank you.
Lukasz is correct - a select statement is a lot different from an insert statement.
The ORA-00933 error means your query is not formed properly. This is because in Oracle, the database expects queries to follow a certain format/standard. Typically, queries within Oracle will have a form of SELECT [columns] FROM [tables] WHERE [conditions]. This can vary - for example if you wanted to select all data from a table, that query might look like "SELECT * FROM [table];" and the WHERE clause can be omitted because you do not need to define a condition for the database to return all rows. While queries can vary in form, in general, they will follow some type of format.
You are receiving this error because your query does not conform to the expected form, and it is because you have an INNER JOIN that directly follows your FROM clause. To fix this, I would recommend creating a query that you use to select the records you want to update, and then using that select statement to form your update statement by replacing the "SELECT" with "UPDATE".
For more on SQL Standards and how to format your queries, I would recommend taking a look at Oracle documentation. https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_1001.htm#SQLRF52344

How to pull a ms access report data to a query?

I am trying to pull data from a MS Access report (repInvoices and form frmInvoice) and keep it in MS Access query(qryQuote). I know how it can be done from form but not from a report. I did research in stack exchange and google but I didn't find anything that is helpful. Can any of you help me out? Following is the query that I had been trying to work on. I kept form on following example:
SELECT MAX([oi].[Quote])
FROM ORDER_Shipment AS os
INNER JOIN ORDER_Items AS oi
ON os.Id = oi.Id
AND oi.MaterialId = [Reports]![repInvoices]![MaterialId]
WHERE os.InvoiceNumber= CAST('& Me.InvoiceID' AS VARCHAR)
Two last lines in above query is the place where I need to get data from the report.Also, I know both of above are not correct right now.
[Reports]![repInvoices]![MaterialId] It gives me an error JOIN expression not supported.
Also, I have to use report InvoiceID here CAST('& Me.InvoiceID' AS VARCHAR)

Resources