subquery

JPQL / QueryDSL: join subquery and get aliased column

▼魔方 西西 提交于 2019-12-29 05:24:05
问题 I'm trying to get an average for a count on a groupBy by joining with a subquery. Don't know if that the right way to go at all but I couldn't anything about subqueries other than the mysema doc. Scenario: How many orders per product did a customer do on average? Meaning: A Customer orders products. So a customer ordered a specific product a number of times (count). What's the average number of orders that customer placed for any product? Might sound a bit hypothetical, in fact it's just part

Can you define “literal” tables in SQL?

久未见 提交于 2019-12-29 04:28:05
问题 Is there any SQL subquery syntax that lets you define, literally, a temporary table? For example, something like SELECT MAX(count) AS max, COUNT(*) AS count FROM ( (1 AS id, 7 AS count), (2, 6), (3, 13), (4, 12), (5, 9) ) AS mytable INNER JOIN someothertable ON someothertable.id=mytable.id This would save having to do two or three queries: creating temporary table, putting data in it, then using it in a join. I am using MySQL but would be interested in other databases that could do something

What SQL databases support subqueries in CHECK constraints?

点点圈 提交于 2019-12-28 06:34:04
问题 What SQL databases, if any, support subqueries in CHECK constraints? At present and as far as I know, Oracle, MySQL, and PostgreSQL do not. EDIT (Clarification based on initial answers.) I'm looking for something like this: CREATE TABLE personnel ( ..., department VARCHAR(64) NOT NULL, salary NUMERIC NOT NULL, CHECK (salary >= (SELECT MIN(p.salary) FROM payranges p WHERE p.dept = department) AND salary <= (SELECT MAX(p.salary) FROM payranges p WHERE p.dept = department) ) UPDATE MS Access and

T-SQL Subquery Max(Date) and Joins

最后都变了- 提交于 2019-12-28 03:35:18
问题 I'm trying to join multiple tables, but one of the tables has multiple records for a partid with different dates. I want to get the record with the most recent date. Here are some example tables: Table: MyParts Partid Partnumber Description 1 ABC-123 Pipe 2 ABC-124 Handle 3 ABC-125 Light Table: MyPrices Partid Price PriceDate 1 $1 1/1/2005 1 $2 1/1/2007 1 $3 1/1/2009 2 $2 1/1/2005 2 $4 1/1/2006 2 $5 1/1/2008 3 $10 1/1/2008 3 $12 1/1/2009 If I was just wanted to find the most recent price for

In which sequence are queries and sub-queries executed by the SQL engine?

家住魔仙堡 提交于 2019-12-28 02:51:41
问题 Hello I made a SQL test and dubious/curious about one question: In which sequence are queries and sub-queries executed by the SQL engine? the answers was primary query -> sub query -> sub sub query and so on sub sub query -> sub query -> prime query the whole query is interpreted at one time There is no fixed sequence of interpretation, the query parser takes a decision on fly I choosed the last answer (just supposing that it is most reliable w.r.t. others). Now the curiosity: where can i

MySQL - SELECT WHERE field IN (subquery) - Extremely slow why?

故事扮演 提交于 2019-12-27 12:37:29
问题 I've got a couple of duplicates in a database that I want to inspect, so what I did to see which are duplicates, I did this: SELECT relevant_field FROM some_table GROUP BY relevant_field HAVING COUNT(*) > 1 This way, I will get all rows with relevant_field occuring more than once. This query takes milliseconds to execute. Now, I wanted to inspect each of the duplicates, so I thought I could SELECT each row in some_table with a relevant_field in the above query, so I did like this: SELECT *

SQL subSelect Statement issue in iReport

馋奶兔 提交于 2019-12-25 18:46:08
问题 I am using iReport to write custom reports and have ran into an issue. I am attempting to alter an existing report by adding the line of code that is: LEFT JOIN (select info from customfieldview where cftableid = 97022306 and recordid = so.id and cfname = 'SIN') The field and table names are all correct but for whatever reason it is saying that my "WHERE" statement is messed up. Which with iReport, it's debugger usually says the issue is the line below the actual problem and the line before

SQL query based on subquery. Retrieve transactions with data > threshold

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 16:50:13
问题 My db table is called transactions and is like this: Name | Date (DateTime) | Type | Stock | Volume | Price | Total Tom 2014-05-24 12:00:00 Sell Barclays 100 2.2 220.0 Bob 2014-04-13 15:00:00 Buy Coca-Cola 10 12.0 120.0 varchar DateTime varchar varchar int float float My initial problem was to remove from the table ALL the transactions that belong to a user whose first transaction is later than a certain threshold. My query was: DELETE FROM transactions WHERE name NOT IN (SELECT name FROM

How to express count(distinct) with subquery in MySQL?

左心房为你撑大大i 提交于 2019-12-25 15:50:54
问题 A query results a certain number. The query is: select count(distinct case when (A or B or C) and D then table_a.field1 else null end) from table_a left join table_b on table_b.x = table_a.y group by table_a.y ; where A, B, C and D are given conditions. Now, written in this form: select sum((select count(1) from table_b where table_b.x = table_a.y and ((A or B or C) and D) )) from table_a left join table_b on table_b.x = table_a.y group by table_a.y ; the result does not match the one we got

Combine instructions with a subquery in SQLite

吃可爱长大的小学妹 提交于 2019-12-25 09:33:49
问题 I have a SQLite table that looks like this: ID_TABLE POINTS_A_TABLE POINTS_B_TABLE id number id_a points_a id_b points_b -------------- ---------------- ---------------- smith 1 smith 11 smith 25 gordon 22 gordon 11 gordon NULL butch 3 butch 11 butch 26 sparrow 25 sparrow NULL sparrow 44 white 76 white 46 white NULL With the following command SELECT id, avg(points_a) FROM (SELECT id_a AS id, points_a FROM points_a_table UNION ALL SELECT id_b AS id, points_b FROM points_b_table) GROUP BY id