subquery

How can I loop through a table within a stored procedure?

江枫思渺然 提交于 2019-12-20 02:32:16
问题 This question evolved from this one. I have two tables that I need to query and glean some calculated sums from; I need a result set based on units -- one row for each Unit, with calculated data for them folded into that row. The two tables contain the following pertinent members: CustomerCategoryLog : Unit varchar(25) MemberNo varchar(10) Category varchar(50) Subcategory varchar(50) BeginDate Datetime EndDate Datetime ReportingMonthlySales : Unit (VarChar) MemberNo (VarChar) MonthlySales

Sum top 5 values in MySQL

人走茶凉 提交于 2019-12-20 01:57:46
问题 I have a MySQL table where I store results from a racing championship, so that every rows contains -among other data- every driver's position in a certain race. I want to get the sum of a certain driver's top 5 placings (for instance, if a driver's best positions were 1,2,2,4,5, I'd like MySQL to return 14). What I'm trying to do is this: SELECT driver, SUM(position) FROM results WHERE (race, season, position) IN (SELECT race, season, position FROM results WHERE driver = "Vettel" ORDER BY

SQL Server left joining

孤人 提交于 2019-12-19 19:49:20
问题 I'm trying to make left join in one query, but it seems that I'm wrong somewhere. table machines -------------- machineID FaNo Barcode RoutingCode Name table log ------------- logID lineBarcode machineBarcode In the log table there are records on the machines and the lines. On one line there can be many different machines and machines from the same type. The machine type is routingCode , so I'm interested in selecting all the machines in the line and group them. Only machines with different

How do you programmatically turn off eager fetching with hibernate?

流过昼夜 提交于 2019-12-19 18:50:53
问题 I have in my mapping an association to an eagerly loaded collection (lazy="false" fetch="subselect"). How can I turn that off programmatically with Hibernate when I do a query? 回答1: In fact, it is supposed to be the other way around. You turn it off in the mapping, and activate it on specific use cases with a "fetch" in the query. That's the way the Hibernate team sees it. There is no way in Hibernate to create a request that specifies "no-fetch" for a property... 回答2: I had a situation that

How do you programmatically turn off eager fetching with hibernate?

时间秒杀一切 提交于 2019-12-19 18:50:22
问题 I have in my mapping an association to an eagerly loaded collection (lazy="false" fetch="subselect"). How can I turn that off programmatically with Hibernate when I do a query? 回答1: In fact, it is supposed to be the other way around. You turn it off in the mapping, and activate it on specific use cases with a "fetch" in the query. That's the way the Hibernate team sees it. There is no way in Hibernate to create a request that specifies "no-fetch" for a property... 回答2: I had a situation that

Oracle: Using subquery in a trigger

Deadly 提交于 2019-12-19 09:10:46
问题 How can I work around the Oracle's limitation of not allowing subqueries in triggers. Here's an example trigger I'm trying to create, but am unable to because I can't use a subquery. CREATE OR REPLACE TRIGGER trigger_w_subquery AFTER UPDATE OR INSERT ON project_archiving FOR EACH ROW WHEN (old.archiving_status <> new.archiving_status AND new.archiving_status = 1 AND (SELECT offer FROM projects WHERE projnum = :new.projnum) IS NULL ) BEGIN INSERT INTO offer_log (offer, status, date) VALUES

MYSQL subquery SELECT in JOIN clause

女生的网名这么多〃 提交于 2019-12-19 06:30:12
问题 Ok... well I have to put the subquery in a JOIN clause since it selects more than one column and putting it in the SELECT clause does not allow that as it gives me an error of an operand. Anywho, this is my query: SELECT c.id, c.title, c.description, c.icon, p.id as topic_id, p.title AS topic_title, p.date, p.username FROM forum_cat c LEFT JOIN ( SELECT ft.id, ft.cat_id, ft.title, fp.date, u.username FROM forum_topic ft JOIN forum_post fp ON fp.topic_id = ft.id JOIN user u ON u.user_id = fp

Hibernate criteria api 'Select in'

梦想的初衷 提交于 2019-12-19 06:17:23
问题 is it possible to create a 'select in'-query with the hibernate critiria api ? Example : I have two tables in a 1:n relation, company and department select * from company c where c.id in (select company_id from department d where d.departmentname = 'HR' and d.location = 'xyz') 回答1: You can use for this DetachedCriteria DetachedCriteria subCriteria= DetachedCriteria.forClass(Departament.class); subCriteria.add(Property.forName("departmentname ").eq("HR")); subCriteria.add(Property.forName(

How to select most frequent value in a column per each id group?

我与影子孤独终老i 提交于 2019-12-19 05:51:09
问题 I have a table in SQL that looks like this: user_id | data1 0 | 6 0 | 6 0 | 6 0 | 1 0 | 1 0 | 2 1 | 5 1 | 5 1 | 3 1 | 3 1 | 3 1 | 7 I want to write a query that returns two columns: a column for the user id, and a column for what the most frequently occurring value per id is. In my example, for user_id 0, the most frequent value is 6, and for user_id 1, the most frequent value is 3. I would want it to look like below: user_id | most_frequent_value 0 | 6 1 | 3 I am using the query below to get

How to write this (left join, subquery ) in Laravel 5.1?

那年仲夏 提交于 2019-12-19 02:46:11
问题 How to write this query in Laravel 5.1: SELECT p.id, p.title, p.created_at, p.updated_at, u.name, COALESCE(c.comments_count, 0) AS comments_count, COALESCE(pl.status_sum, 0) AS status_sum FROM posts p LEFT OUTER JOIN users u ON u.id = p.user_id LEFT OUTER JOIN ( SELECT pl.post_id, SUM(pl.status) AS status_sum FROM postslikes pl GROUP BY pl.post_id ) pl ON pl.post_id = p.id LEFT OUTER JOIN ( SELECT c.post_id, COUNT(*) as comments_count FROM comments c GROUP BY c.post_id ) c ON c.post_id = p.id