subquery

How do I select the Count(*) of an nHibernate Subquery's results

半腔热情 提交于 2019-12-19 01:17:45
问题 I need to do the following for the purposes of paging a query in nHibernate: Select count(*) from (Select e.ID,e.Name from Object as e where...) I have tried the following, select count(*) from Object e where e = (Select distinct e.ID,e.Name from ...) and I get an nHibernate Exception saying I cannot convert Object to int32. Any ideas on the required syntax? EDIT The Subquery uses a distinct clause, I cannot replace the e.ID,e.Name with Count(*) because Count(*) distinct is not a valid syntax

The multi-part identifier could not be bound - SubQuery

若如初见. 提交于 2019-12-18 18:56:55
问题 Schema: create table TableA (A1 int) create table TableB (B1 int, B2 int) create table TableC (C1 int) Problematic query: SELECT * FROM TableA a INNER JOIN TableB b ON b.B1=a.A1 INNER JOIN (SELECT TOP 1 * FROM TableC c WHERE c.C1=b.B1 ORDER BY c.C1) d ON d.C2=b.B2 INNER JOIN OtherTable ON OtherTable.Foo=d.C1 Building this schema and running the query in SQLFiddle under SQL Server 2008 results in: The multi-part identifier "b.B1" could not be bound.: SELECT * FROM TableA a INNER JOIN TableB b

The multi-part identifier could not be bound - SubQuery

梦想与她 提交于 2019-12-18 18:56:26
问题 Schema: create table TableA (A1 int) create table TableB (B1 int, B2 int) create table TableC (C1 int) Problematic query: SELECT * FROM TableA a INNER JOIN TableB b ON b.B1=a.A1 INNER JOIN (SELECT TOP 1 * FROM TableC c WHERE c.C1=b.B1 ORDER BY c.C1) d ON d.C2=b.B2 INNER JOIN OtherTable ON OtherTable.Foo=d.C1 Building this schema and running the query in SQLFiddle under SQL Server 2008 results in: The multi-part identifier "b.B1" could not be bound.: SELECT * FROM TableA a INNER JOIN TableB b

Hibernate Subselect vs Batch Fetching

半城伤御伤魂 提交于 2019-12-18 12:24:34
问题 Hibernate provides (at least) two options for getting around the N+1 query problem. The one is setting the FetchMode to Subselect, which generates a select with a IN-clause and a subselect within this IN-clause. The other is to specify a BatchSize, which generates a select with a IN-clause containing the parents' IDs. Both work but I find that the Subselect option often runs into performance problems due to the query for the parents being complex. On the other hand, with a large BatchSize

How to reuse a result column in an expression for another result column

一笑奈何 提交于 2019-12-18 10:58:22
问题 Example: SELECT (SELECT SUM(...) FROM ...) as turnover, (SELECT SUM(...) FROM ...) as cost, turnover - cost as profit Sure this is invalid (at least in Postgres) but how to achieve the same in a query without rewriting the sub-query twice? 回答1: Like so: SELECT turnover, cost, turnover - cost as profit from ( (SELECT SUM(...) FROM ...) as turnover, (SELECT SUM(...) FROM ...) as cost ) as partial_sums 回答2: You could reuse the query like this: WITH TURNOVER AS ( SELECT SUM(...) FROM ...) ), COST

MySQL: Returning multiple columns from an in-line subquery

烂漫一生 提交于 2019-12-18 10:04:36
问题 I'm creating an SQL statement that will return a month by month summary on sales. The summary will list some simple columns for the date, total number of sales and the total value of sales. However, in addition to these columns, i'd like to include 3 more that will list the months best customer by amount spent. For these columns, I need some kind of inline subquery that can return their ID, Name and the Amount they spent. My current effort uses an inline SELECT statement, however, from my

WHERE col1,col2 IN (…) [SQL subquery using composite primary key]

谁说我不能喝 提交于 2019-12-17 23:38:48
问题 Given a table foo with a composite primary key (a,b) , is there a legal syntax for writing a query such as: SELECT ... FROM foo WHERE a,b IN (SELECT ...many tuples of a/b values...); UPDATE foo SET ... WHERE a,b IN (SELECT ...many tuples of a/b values...); If this is not possible, and you could not modify the schema, how could you perform the equivalent of the above? I'm also going to put the terms "compound primary key", "subselect", "sub-select", and "sub-query" here for search hits on

Subquery in select clause with JPA Criteria API

独自空忆成欢 提交于 2019-12-17 22:34:33
问题 I'm trying, as in title, to insert a subquery in select clause like in this simple SQL: SELECT id, name, (select count(*) from item) from item this is obviously only a mock query just to make my point. (The point would be to get the last invoice for each item returned by the query.) I've tried this: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Tuple> c = cb.createTupleQuery(); Root<Item> item= c.from(Item.class); Subquery<Long> scount = c.subquery(Long.class); Root<Item>

comma-separated list as a result of select statement in Oracle [duplicate]

会有一股神秘感。 提交于 2019-12-17 16:30:21
问题 This question already has answers here : How can I combine multiple rows into a comma-delimited list in Oracle? [duplicate] (11 answers) Closed 5 years ago . I have a table named "person". It contains person's id and it's parent id (only one parent is possible). As a result of a query, I want a table with first column - a person id, and a second column - a list of it's children id's. How exactly to do this? I've read about listagg function, but I'm not sure if it is appropriate for my purpose

How to alias a field or column in MySQL?

女生的网名这么多〃 提交于 2019-12-17 07:21:47
问题 I'm trying to do something like this. But I get an unknown column error: SELECT SUM(field1 + field2) AS col1, col1 + field3 AS col3 from core Basically, I want to just use the alias so that I won't need to perform the operations performed earlier. Is this possible in mysql? 回答1: Consider using a subquery, like: SELECT col1 , col1 + field3 AS col3 FROM ( SELECT field1 + field2 as col1 , field3 from core ) as SubQueryAlias 回答2: select @code:= SUM(field1 + field2), @code+1 from abc; But, please