subquery

Linq to Entities many to many selection: How to force the generation of a JOIN instead of a subselect clause?

╄→гoц情女王★ 提交于 2019-12-07 10:47:16
问题 Using EF DB first I have two entities (Supplier, Product) that have a many-to-many relationship. Entity Framework does not create an entity for the associated table (SupplierProduct) as the associated table contains only the primary keys of the strong entities. I have been getting all Suppliers that do not supply a given product with the following query: var q1 = context.Suppliers.Where(s=>!s.Products.Any(p=>p.Id == 1)); The SQL produced uses an EXISTS dependent subquery similar to this:

SQL: Select transactions where rows are not of criteria inside the same table

风格不统一 提交于 2019-12-07 07:58:17
问题 I have a table with transactions: Transactions ------------ id | account | type | date_time | amount ---------------------------------------------------- 1 | 001 | 'R' | '2012-01-01 10:01:00' | 1000 2 | 003 | 'R' | '2012-01-02 12:53:10' | 1500 3 | 003 | 'A' | '2012-01-03 13:10:01' | -1500 4 | 002 | 'R' | '2012-01-03 17:56:00' | 2000 5 | 001 | 'R' | '2012-01-04 12:30:01' | 1000 6 | 002 | 'A' | '2012-01-04 13:23:01' | -2000 7 | 003 | 'R' | '2012-01-04 15:13:10' | 3000 8 | 003 | 'R' | '2012-01

SQLite update query - subquery with aliases doesn't work

喜夏-厌秋 提交于 2019-12-07 06:47:47
问题 I need to update a SQLite table. The table looks like: ID | Address | CallNumber | RefID -----+--------------------+------------------------------------------- ef78 | library | 2002/13 | 100002 no56 | Lit | 0189 | 100003 rs90 | temp | | 100003 For every column with Address = "Lit" there is a column Address = 'temp' with the same RefID. Now I need to update each Address = "temp" with the value "CallNumber" from the column with the same RefID. The updated table should look like: ID | Address |

Subquery in where clause with CriteriaQuery

本秂侑毒 提交于 2019-12-07 03:41:43
问题 Can anybody give me some hints on how to put that kind of subquery in a CriteriaQuery ? (I'm using JPA 2.0 - Hibernate 4.x ) SELECT a, b, c FROM tableA WHERE a = (SELECT d FROM tableB WHERE tableB.id = 3) - the second select will always get a single result or null. 回答1: Try something like the following example to create a subquery: CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class); Root tableA = cq.from(TableA.class); Subquery<String> sq = cq.subquery(TableB.class); Root tableB = cq

Efficiency of joining subqueries in SQL Server

别说谁变了你拦得住时间么 提交于 2019-12-07 02:35:38
问题 I have a customers and orders table in SQL Server 2008 R2. Both have indexes on the customer id (called id ). I need to return details about all customers in the customers table and information from the orders table, such as details of the first order. I currently left join my customers table on a subquery of the orders table, with the subquery returning the information I need about the orders. For example: SELECT c.id ,c.country ,First_orders.product ,First_orders.order_id FROM customers c

MySQL query to group results by date range?

允我心安 提交于 2019-12-07 00:54:12
问题 I got a system that pings to the database every 2 to 5 seconds when he is connected the application. Depending on his connection, the ping timeframe can be bigger, like 10 seconds or so. Example: Pings: 1,4,6,8,9,12,16,20,50,180,187,189,200,203,206,210 ... I want run a query to grab ranges that does not exceed 1 minute between the pings, group them, so I can tell for how long the user has been connected, and save into a new table like: Connections table: user: X | start_date: 1 | end_date: 50

Why not “Invalid column name XYZ” error in subquery; although column name is not in subquery table?

风流意气都作罢 提交于 2019-12-06 21:20:20
问题 When I run this query SELECT CustomerId FROM Stocks.dbo.Suppliers It gives me this error. Invalid column name 'CustomerId'. This error is valid as there is no column CustomerId in Suppliers table; but when I use same query in subquery it does not give any error E.g. SELECT * FROM SomeOtherDb.dbo.Customer WHERE CustomerId In( SELECT CustomerId FROM Stocks.dbo.Suppliers) Here I am expecting same error "Invalid column name" but query runs without any error. Fully qualified name is just

Passing an expression to a method in NHibernate results in Object of type 'ConstantExpression' cannot be converted to type 'LambdaExpression'

一世执手 提交于 2019-12-06 20:53:31
This problem occurs in both NHibernate 2 and 3. I have a Class A that has a member set of class B. Querying the classes directly executes nicely. But when I pass one of the expressions involving class B into a method I get the following error: System.ArgumentException: Object of type 'System.Linq.Expressions.ConstantExpression' cannot be converted to type 'System.Linq.Expressions.LambdaExpression'. As far as I can see I am passing the exact same expression into the Any() method. But for some reason they are treated differently. I have done some debugging and it looks like in the first method,

Rails Activerecord Relation: using subquery as a table for a SQL select statement

痴心易碎 提交于 2019-12-06 19:36:42
问题 Can somebody help me figure out how to write the following SQL using Rails (I'm using Rails 4) Activerecord methods? I know you can do this with find_by_sql but I'd like to preserve the active record relation. Here's the sql for a postGreSQL db that I'm trying to create: SELECT * FROM (SELECT DISTINCT ON(table_a.id) table_a.name as alias_a, table_b.id, table_b.time FROM table_1 LEFT OUTER JOIN table_b ON table_a.id = table_b.id ORDER BY table_a.id, table_b.time asc) AS subquery ORDER BY alias

NHibernate: How to perform eager subselect fetching of many children & grandchildren (object graph) in a single round-trip to the database?

…衆ロ難τιáo~ 提交于 2019-12-06 18:42:47
问题 First, please don't try to argue me out of doing the eager load - traversing the object graph and causing (by lazy loading) even more than ONE round-trip to the database is just not an option. I have a big object graph. I want to fetch the root object, plus a subset of its children, grandchildren, great-grandchildren, etc. Currently I do this by creating multiple Future objects (with Criteria) and in each one, I do SetFetchMode("...", FetchMode.Eager) - see Ayende's post and Sam's 3rd comment