subquery

In a Django subquery, can I reference the “parent” query?

半腔热情 提交于 2019-12-08 02:07:30
问题 It's simple to create subqueries in Django ORM (just use a QuerySet as part of another query), but is it possible for that subquery to reference fields in the "parent" (outer, main) query? For a full example of what I'm trying to achieve, see this working SQL Fiddle. I broke it down into two questions (other one here). In this case, I have a model Whole that represents a value that must be reached. Several Part s contribute to it with a (calculated) value of their own. I want to retrieve all

How to execute sub query in if exists condition?

左心房为你撑大大i 提交于 2019-12-07 23:58:05
问题 declare @qry varchar(100) declare @cnt int set @qry = ' where ' if exists( select * from ARTICLE_MANAGE +@qry+ article_id=65) BEGIN select top 1* from ARTICLE_MANAGE order by article_id desc END ELSE BEGIN select * from ARTICLE_MANAGE order by article_id desc END This is the query. '@qry' is changed by what we passed to the query 回答1: DECLARE @qry VARCHAR(100); DECLARE @cnt INT; set @qry = ' where ' DECLARE @ExeQuery VARCHAR(MAX); SET @ExeQuery='if exists( select * from ARTICLE_MANAGE '+@qry+

Select one row with MAX(column) for known other several columns without subquery

此生再无相见时 提交于 2019-12-07 20:09:22
问题 My table contains votes of users for different items. It has the following fields: id, user_id, item_id, vote, utc_time I understand how to get the last vote of #user# for #item# , but it uses subquery: SELECT votes.*, items.name, items.price FROM votes JOIN items ON items.id = votes.item_id WHERE user_id = #user# AND item_id = #item# AND utc_time = ( SELECT MAX(utc_time) FROM votes WHERE user_id = #user# AND item_id = #item# ) It works, but it looks quite stupid to me... There should be a

NHibernate (+ FluentNhibernate) : Join two detached tables

百般思念 提交于 2019-12-07 17:56:54
问题 'tI encounter problems to create a join on two entities with a common property but they are not map together. Say you have an entity Article which contains a property FamilyCode and an entity Family with properties Code and Label. In my mappings, Article doesn't reference Family and i don't want to change that (to keep compatibility with others internal and legacy methods). So, i can't translate the below query in Nhibernate : SELECT f.Code, f.Label FROM Article a INNER JOIN Family f ON a

Reuse Subquery from Select Expression in WHERE-Clause

馋奶兔 提交于 2019-12-07 16:32:59
问题 Of course it isn't possible to write SELECT (some subselect) AS blah FROM t WHERE blah = 'const' What is the best way to do this? SELECT (some subselect) FROM t WHERE (some subselect) = 'const' ? View? Stored Function? HAVING? other? 回答1: you can move (some subselect) as table in the FROM : SELECT s.blah FROM t, (some subselect) s WHERE t.id = s.id AND s.blah = 'const' 来源: https://stackoverflow.com/questions/5720983/reuse-subquery-from-select-expression-in-where-clause

Sequelize Top level where with eagerly loaded models creates sub query

白昼怎懂夜的黑 提交于 2019-12-07 15:55:31
I'm running an into issue where Sequelize creates a subquery of the primary model and then joins the includes with that subquery instead of directly with the primary model table. The query conditions for the include(s) ends up inside the subquery's WHERE clause which makes it invalid. I have shortened names down trying to keep this compact hopefully without losing any relevant info. Environment: Nodejs: 6.11.3 Sequelize: 3.23.6 => Updated to 4.38.1 and problem persists MySql: 5.7.23 Code snip models: I.model: models.I.hasMany(models.II); models.I.belongsTo(models.CJ); models.I.belongsTo(models

Oracle: How to create a function returning values for a “SELECT * FROM tab WHERE name IN (function())”

半城伤御伤魂 提交于 2019-12-07 15:24:47
问题 I have a problem which I can't solve. Maybe you have an idea about how to solve it. I do have a given parameter-table like this: P_VALUE P_NAME ----------- ---------- X85 A_03 XH1 A_04 XH2 A_04 XH3 A_04 C84 A_05 As you can see there are parameters with multiple entries. At the moment this parameters are used in this way: SELECT * FROM tablex WHERE code IN (SELECT p_value FROM parameter_table WHERE p_name LIKE 'A_04'); As the query is very big these parameter sub-select are used very often. I

SQL aggregate function subquery

孤街浪徒 提交于 2019-12-07 14:01:35
问题 What I want to do is count the number of rows returned by a subquery, essentially the following: select pp.prop_id, COUNT((select employee_id from employee e where e.ao1_hours > 0)) from proposal_piece pp group by pp.prop_id order by pp.prop_id Here is my error message: Cannot perform an aggregate function on an expression containing an aggregate or a subquery. Why does this not work? If select is just returning a bunch of employee_id's with a filtering criteria why can't I count the number

Nested SELECT clause in SQL Compact 3.5

爷,独闯天下 提交于 2019-12-07 13:11:14
问题 In this post "select with nested select" I read that SQL Compact 3.5 (SP1) support nested SELECT clause. But my request not work: t1 - table 1 t2 - table 2 c1, c2 = columns select t1.c1, t1.c2, (select count(t2.c1) from t2 where t2.id = t1.id) as count_t from t1 Does SQL Compact 3.5 SP1 support nested SELECT clause in this case? Update: SQL Compact 3.5 SP1 work with this type of nested request: SELECT ... from ... where .. IN (SELECT ...) SELECT ... from (SELECT ...) 回答1: Thank all for help

Selecting top N values within a group in a column using R

廉价感情. 提交于 2019-12-07 13:02:21
问题 I need to select top two values for each group[yearmonth] value from the following data frame in R. I have already sorted the data by count and yearmonth.How can I achieve that in following data? yearmonth name count 1 201310 Dovas 5 2 201310 Indulgd 2 3 201310 Justina 1 4 201310 Jolita 1 5 201311 Shahrukh Sheikh 1 6 201311 Dovas 29 7 201311 Justina 13 8 201311 Lina 8 9 201312 sUPERED 7 10 201312 John Hansen 7 11 201312 Lina D. 6 12 201312 joanna1st 5 回答1: Or using data.table ( mydf from