I have previously used ntile function in MS SQL in a project in R, but I need to change the database of the project in Mysql now. As per my knowledge Ntile is a window function in Sql Server,which is not present in Mysql. I have googled about this found the below link about ntile function documentation in Mysql.
Mysql Ntile Documentation
I have tried the same way as described but still facing the error.
My query:
SELECT *, NTILE(10) OVER w AS 'ntile10'
FROM test_table_name w AS (ORDER BY res_rate);
Any idea about the problem?
Is there any solution to overcome it?
You are missing WINDOW keyword:
SELECT *, NTILE(10) OVER w AS ntile1
FROM test_table_name
WINDOW w AS (ORDER BY res_rate);
or:
SELCT *, NTILE(10) OVER(ORDER BY res_rate) AS ntile1
FROM test_table_name;
Related
Problem Statement
There is a custom function (UDF in javascript) written in snowflake.
This custom statement operates over a table with the window function over (partition by col name order by col name)
GET_TEST1() is the name custom function
Syntax:
select GET_TEST1(Col1, Col2) over (partition BY col3,col4 order by col6+col7, col8 DESC) from table name WHERE IND='Y'
Error:
SQL compilation error: Invalid function type [???] for window function.
I am stuck with this, I went through multiple articles but am not able to resolve it can some one help me.
UPDATES
I have gone through multiple articles and all of them convey
over(partition by....order by....) will not work on custom functions.
But the same query works in Netezza but not in Snowflake. Can some one guide me how to resolve this issue.
When I do that on access, SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN (1,2,5))
GROUP BY RMonturesImp.N°Fac;
but when I do this
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE NOT (RMonturesImp.N°Fac IN Requête2)
GROUP BY RMonturesImp.N°Fac;
it doesn't work (it shows 1,2,5 indeed) although the result of Requête2 (which is a query) is also (1,2,5). I can't understand this!
Thanks in advance
It's quite easy. The IN (1,2,5)) must be explicit as SQL will not evaluate an expression not to say a function to obtain the values for IN.
So build your SQL in code creating the string, or pull the values from a (temp) table.
Try this:
SELECT RMonturesImp.N°Fac
FROM RMonturesImp, Rpartielun
WHERE RMonturesImp.N°Fac NOT IN (Select N°Fac From Requête2)
GROUP BY RMonturesImp.N°Fac;
I'm making this request but I get an error that
OVER is not suported by sql
SELECT FIRST_VALUE( JrnlStock.StockInitial) over (ORDER BY JrnlStock.date ASC),JrnlStock.Reference
where JrnlStock.Date > #Du
Maybe syntax is not right. Where is the FROM clause ?
https://msdn.microsoft.com/en-us/library/hh213018.aspx
It used to be that I could fire a query like the following in a query window/sql command line, against MS SQL server:
SELECT foo1, foo2, * from bar
Basically show the specified columns followed by rest of the columns. But MySQL does not allow this; throws back a syntax error at me. Is there an alternative syntax to do this in MySQL? Note that I am NOT trying to do this in code (where it has no practical use); I am using it for firing random queries against my DB to look up information.
Just declare the table on the SELECT CLAUSE.
SELECT foo1, foo2, bar.* from bar;
OR
SELECT b.foo1, b.foo2, b.* from bar b;
;-)
If you name the table (either by using the full name, or by using an alias like below), you can actually get it to work (tested for version 5.5.31)
SELECT b.foo1, b.foo2, b.* FROM bar b
I have a library That generates a CRUD with pagination using mysql and created a query with group_by, but the $ this-> db-> count_all_results () is not counted correctly.
I made an adjustment by changing the function of the CodeIgniter for an alternative, using the previous select
select count (*) from ('. $ this-> db-> _compile_select ().') x'
Anyone have other suggestions?
You must especify other params after the count_all() function.
Look this:
$this->db->group_by('my_table_col');
$total = $this->db->count_all('my_table');
Cleber.