subquery

SQL query performance question (multiple sub-queries)

限于喜欢 提交于 2019-12-13 03:48:01
问题 I have this query: SELECT p.id, r.status, r.title FROM page AS p INNER JOIN page_revision as r ON r.pageId = p.id AND ( r.id = (SELECT MAX(r2.id) from page_revision as r2 WHERE r2.pageId = r.pageId AND r2.status = 'active') OR r.id = (SELECT MAX(r2.id) from page_revision as r2 WHERE r2.pageId = r.pageId) ) Which returns each page and the latest active revision for each, unless no active revision is available, in which case it simply returns the latest revision. Is there any way this can be

Alternative to Case when with subqquery to avoid error (Illegal expression in When clause within case statement)

醉酒当歌 提交于 2019-12-13 03:41:48
问题 My table structure looks similiar to this Customer_id Country item_type Order_Size Dates Codes A401 US Fruit Small 3/14/2016 11 A401 US Fruit Big 5/22/2016 12 A401 US Vegetable Small 7/12/2016 11 B509 US Vegetable Small 3/25/2015 92 B509 US Vegetable Big 3/15/2014 11 B509 US Vegetable Small 3/1/2014 34 A402 CA Fruit Small 3/14/2016 56 A402 CA Fruit Big 5/22/2016 76 A402 CA Fruit Small 7/12/2016 85 A403 CA Vegetable Small 7/12/2016 11 A403 CA Vegetable Small 3/25/2015 16 A403 CA Vegetable Big

Querying using subqueries on a SQLite database approx 10x slower using newer versions of System.Data.SQLite/sqlite3.dll

半世苍凉 提交于 2019-12-13 03:39:38
问题 (See update below) I am having an issue of slow query performance when querying a very simplistic Sqlite datatable of about 500,000 rows from within a C#.Net application (~5sec). I have tried the exact same query on exactly the same database using LinqPad, as well as 2 database browsers (both based on QtSql), and it runs 10x faster (~0.5secs). Same query, same db, different apps, only mine doesn't run fast. It makes negligible difference whether I'm returning values or just a Count(*). I've

Oracle Select Max Date on Multiple records

旧巷老猫 提交于 2019-12-13 03:36:32
问题 I've got the following SELECT statement, and based on what I've seen here: SQL Select Max Date with Multiple records I've got my example set up the same way. I'm on Oracle 11g. Instead of returning one record for each asset_tag, it's returning multiples. Not as many records as in the source table, but more than (I think) it should be. If I run the inner SELECT statement, it also returns the correct set of records (1 per asset_tag), which really has me stumped. SELECT outside.asset_tag,

Oracle to_char subquery in Trigger

拈花ヽ惹草 提交于 2019-12-13 03:35:33
问题 I have a table (Meeting) with date type attribute (MeetDate) and another varchar2 type attribute (WorkWeek). I'm trying to do an After trigger to fill in the WorkWeek field based on the MeetDate value using the to_char function. Tried the following codes separately and they compile without errors but when I try to insert a row with Null for WorkWeek, it gives me a 'mutating trigger/function may not see it' error. What am I doing wrong here? thanks in advance to any help. --Code 1 Create or

MySQL - Correct approach event counting

女生的网名这么多〃 提交于 2019-12-13 03:05:32
问题 I want to list users which have a particular event count but I'm confused on which approach to take. This is the database table: CREATE TABLE `event` ( `event_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `visitor_id` int(11) DEFAULT NULL, `key` varchar(200) DEFAULT NULL, `value` text, `label` varchar(200) DEFAULT '', `datetime` datetime DEFAULT NULL, PRIMARY KEY (`event_id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; INSERT INTO `event` (`event_id`, `visitor_id`, `key`, `value`,

MySQL select from subquery order

笑着哭i 提交于 2019-12-13 03:00:43
问题 If I have following table: CREATE TABLE `docs` ( `id` int(6) unsigned NOT NULL, `rev` int(3) unsigned NOT NULL, `content` varchar(200) NOT NULL, -- PRIMARY KEY (`id`) ) and execute following query: select * from ( select * from docs order by rev desc ) as `rows` will the order of returned rows be the same as order of inner query? Can this be guaranteed, generally speaking? 回答1: yes, If you are only using select * from ( select * from docs order by rev desc ) as `rows` then it will be same as

sql - sub-queries calculate score

感情迁移 提交于 2019-12-13 02:50:53
问题 I have a code that calculate an score for users using different table I wrote this via php in Codeigniter and sql but this have a big problem that is too slow public function getTopUsers($request) { // return $request; $query = $this->db->query(" SELECT * FROM users WHERE user_is_block = 0 AND user_is_paid = 1 ORDER BY id ASC" )->result_array(); foreach($query as $key=>$value) { unset($query[$key]['user_token']); unset($query[$key]['user_confirmation_token']); unset($query[$key]['user

SQL Server how to select from a list of tables using information_schema.columns or sys.tables?

ε祈祈猫儿з 提交于 2019-12-13 02:36:11
问题 putting into Table_List all tables of the database as rows. SELECT [table_name] INTO Table_List FROM INFORMATION_SCHEMA.TABLES selecting from multiple tables with a table SELECT [common column in each table ] FROM [Table_List] This query doesn't work. I also tried with putting the rows into a list of strings (table names) and then using FROM to select them. But didn't work as well. Does someone know if there is a way to use the SELECT * FROM tables names stored into a table or list? I need

Doctrine2 subquery

旧街凉风 提交于 2019-12-13 02:28:10
问题 I'm trying to write a subquery in doctrine2, to sort a table ordered by date column of another table. (let's say I'm querying table A, which has an id column, and B has an a_id and a date, in the subquery b.a_id = a.id) I'm using query builder, and the addSelect method, but since I can't use LIMIT in my query I get this error: SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row This error is true, but how could I limit this query to 1 row, if LIMIT is not allowed in