[sqlalchemy] Re: Is there any way do select top 100 results from a query in SQLAlchemy?

2018-04-17 Thread Jonathan Vanasco


On Tuesday, April 17, 2018 at 5:58:32 PM UTC-4, Jonathan Vanasco wrote:
>
>
> So .limit() is .top()
>

Clarified: `limit()` is essentially the same as if there were a `top()`, 
because it will emit `TOP` for the query.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: Is there any way do select top 100 results from a query in SQLAlchemy?

2018-04-17 Thread Jonathan Vanasco
SqlAlchemy doesn't natively support `TOP` because it's not part of standard 
sql.  IIRC, only mssql uses it -- and uses it instead of 'limit'.

The SqlAlchemy dialect for mssql will adapt .limit() to emit `TOP` instead 
of `LIMIT`.

http://docs.sqlalchemy.org/en/latest/dialects/mssql.html#limit-offset-support

So .limit() is .top()

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: Is there any way do select top 100 results from a query in SQLAlchemy?

2018-04-17 Thread su-sa


On Tuesday, 17 April 2018 23:17:27 UTC+2, su-sa wrote:
>
> Hi everyone!
>
> I am trying to build the following query with SQLAlchemy:
>
> "select top 100 s_acctbal, s_name, n_name, p_partkey, p_mfgr, s_address, 
> s_phone, s_comment from system.part, "
> "system.supplier, system.partsupp, system.nation, system.region where 
> p_partkey = ps_partkey "
> "and s_suppkey = ps_suppkey and p_size = 15 and p_type like '%BRASS' and 
> s_nationkey = n_nationkey "
> "and n_regionkey = r_regionkey and r_name = 'EUROPE' and ps_supplycost = 
> (select min(ps_supplycost) "
> "from system.partsupp, system.supplier, system.nation, system.region 
> where p_partkey = ps_partkey "
> "and s_suppkey = ps_suppkey and s_nationkey = n_nationkey and n_regionkey 
> = r_regionkey and r_name = 'EUROPE')"
> " order by s_acctbal desc, n_name, s_name, p_partkey;")
>
> But unfortunately, I am unable to figure out how to impose top 100 on the 
> query,
> This is what I have upto now:
>
> q2 = session.query(Supplier.s_acctbal, Supplier.s_name, Nation.n_name, 
> Part.p_partkey, Part.p_mfgr,
>Supplier.s_address, Supplier.s_phone, 
> Supplier.s_comment).filter(Part.p_partkey == Partsupp.ps_partkey,
>Supplier.s_suppkey== Partsupp.ps_suppkey, Part.p_size == 
> 15, Part.p_type.like('%BRASS'),
>Supplier.s_nationkey == Nation.n_nationkey, 
> Nation.n_regionkey == Region.r_regionkey,
>Region.r_name == 
> 'Europe').order_by(Supplier.s_acctbal.desc(), Nation.n_name, Part.p_partkey)
>
> It might be that I making some other mistake as well, because I get no result 
> from the query upto now.
>
>
I am aware that I could use .limit(100), but was still curious to know if 
there is somehow a way to use top instead. 

>
> I would be really thankful if anyone of you could help me.
>
> Best Wishes,
> Sugandha
>
>
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Is there any way do select top 100 results from a query in SQLAlchemy?

2018-04-17 Thread su-sa
Hi everyone!

I am trying to build the following query with SQLAlchemy:

"select top 100 s_acctbal, s_name, n_name, p_partkey, p_mfgr, s_address, 
s_phone, s_comment from system.part, "
"system.supplier, system.partsupp, system.nation, system.region where 
p_partkey = ps_partkey "
"and s_suppkey = ps_suppkey and p_size = 15 and p_type like '%BRASS' and 
s_nationkey = n_nationkey "
"and n_regionkey = r_regionkey and r_name = 'EUROPE' and ps_supplycost = 
(select min(ps_supplycost) "
"from system.partsupp, system.supplier, system.nation, system.region where 
p_partkey = ps_partkey "
"and s_suppkey = ps_suppkey and s_nationkey = n_nationkey and n_regionkey = 
r_regionkey and r_name = 'EUROPE')"
" order by s_acctbal desc, n_name, s_name, p_partkey;")

But unfortunately, I am unable to figure out how to impose top 100 on the query,
This is what I have upto now:

q2 = session.query(Supplier.s_acctbal, Supplier.s_name, Nation.n_name, 
Part.p_partkey, Part.p_mfgr,
   Supplier.s_address, Supplier.s_phone, 
Supplier.s_comment).filter(Part.p_partkey == Partsupp.ps_partkey,
   Supplier.s_suppkey== Partsupp.ps_suppkey, Part.p_size == 15, 
Part.p_type.like('%BRASS'),
   Supplier.s_nationkey == Nation.n_nationkey, 
Nation.n_regionkey == Region.r_regionkey,
   Region.r_name == 
'Europe').order_by(Supplier.s_acctbal.desc(), Nation.n_name, Part.p_partkey)

It might be that I making some other mistake as well, because I get no result 
from the query upto now.

I would be really thankful if anyone of you could help me.

Best Wishes,
Sugandha


-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.