subquery

How to chain or combine scopes with subqueries or find_by_sql

ⅰ亾dé卋堺 提交于 2020-01-05 04:20:12
问题 I would like to perform a query like SELECT * FROM ( SELECT * FROM products ORDER BY price ASC ) AS s GROUP BY item; which return the cheapest of all products for each item. Using this subquery is good because it can run in O(N logN) time. So I can find this with find_by_sql, but it would be nice to be able to chain it with other scopes for Product. Anyone know how to either write this as a scope or chain scoped and find_by_sql? 回答1: You should be able to do something like Product.from("

How do you get a count of a subquery in Active Record?

回眸只為那壹抹淺笑 提交于 2020-01-04 06:33:24
问题 I'm migrating an SQL query to Active Record. Here is a simplified version: SELECT count(*) FROM ( SELECT type, date(created_at) FROM notification_messages GROUP BY type, date(created_at) ) x I'm not sure how to implement this in Active Record. This works, but it's messy: sql = NotificationMessage. select("type, date(created_at) AS period"). group("type", "period"). to_sql NotificationMessage.connection.exec_query("SELECT count(*) FROM (#{sql}) x") Another possibility is to do the count in

Oracle Order by not working for Subquery from DUAL

*爱你&永不变心* 提交于 2020-01-04 06:05:43
问题 Hi all when I executed this query somehow its throwing the following error - ORA-00907: missing right parenthesis . But if you remove the order by 1 from SELECT 2 FROM DUAL order by 1 its working. Did I miss something out here or its ORACLE limitation SELECT (CASE WHEN EXISTS (SELECT 1 FROM DUAL) THEN (SELECT 4 FROM dual) ELSE (SELECT 2 FROM DUAL order by 1 ) END) AS DELEGATOR FROM dual Below is a working code with order by 1 removed SELECT (CASE WHEN EXISTS (SELECT 1 FROM DUAL) THEN (SELECT

PostgreSQL join across 2 databases

 ̄綄美尐妖づ 提交于 2020-01-04 06:02:12
问题 I am new to PostgreSQL. I have 2 databases in PostgreSQL 9.0, db1 and db2, and with db2 I have read only access. I want to create a stored function that would be otherwise easily accomplished with a JOIN or a nested query, something PostgreSQL can't do across databases. In db1, I have table1 where I can query for a set of foreign keys keys that I can use to search for records in a table2 in db2, something like: SELECT * from db2.table2 WHERE db2.table2.primary_key IN ( SELECT db1.table1

sql group_concat and subquery

喜你入骨 提交于 2020-01-04 05:32:11
问题 I have 2 mysql tables: car_model: id (int) (Primary Key) title (varchar) id_brand (int) FK to car_brand table car__car_model: - relation many to many id_model (int) id_advice_model (int) In car__car_model there are the following data: (id_model) (id_advice_model) 12 12 12 45 12 67 12 78 13 13 13 12 13 67 13 105 And I want to fetch this data like this: 12 12,45,67,78 13 13.12.67,105 I use group_concat and group by like this: SELECT ccm.id_model,group_concat(ccm.id_advice_model) FROM car__car

Are SQL ANY and SOME keywords synonyms in all SQL dialects?

妖精的绣舞 提交于 2020-01-03 07:18:34
问题 In Postgres, ANY and SOME are synonyms when used on the right hand side of a predicate expression. For example, these are the same: column = ANY (SELECT ...) column = SOME (SELECT ...) This is documented here: http://www.postgresql.org/docs/9.1/static/functions-subquery.html#FUNCTIONS-SUBQUERY-ANY-SOME I have observed ANY and SOME to be supported by at least these SQL DBMSs: DB2 Derby H2 HSQLDB Ingres MySQL Oracle Postgres SQL Server Sybase ASE Sybase SQL Anywhere Can I safely assume that all

forming a subquery inside mysql?

我的梦境 提交于 2020-01-03 06:40:13
问题 List the customer number, customer name, and sales rep that have the same rep number as customer number 282. This will need to be a subquery and do not test for sales rep 35 explicitly. Let MySQL do the work for you. Insert your query and results here. use premier_products; select customer.customer_num,customer.customer_name,rep.first_name (SELECT rep_num from rep,customer where rep.rep_num = customer.rep_num and customer_num = 282) from customer,rep; im confused with how to form a subquery

How to solve this using a subquery in a from clause?

南笙酒味 提交于 2020-01-03 05:23:12
问题 Display author, title, retail and retail price of all books whose retail price is the highest for the specific author. I have the query below. I'm kinda confused how to do a subquery in a from clause. select lname, fname, title, retail from author natural join bookauthor natural join books where retail=(select max(retail) from books); Below is the data from the database that I'm using FNAME LNAME TITLE RETAIL ---------- ---------- ------------------------------ ---------- SAM SMITH BODYBUILD

Basic SQL sub-query

谁都会走 提交于 2020-01-02 10:04:34
问题 I've got a table called products. Basically I need to search using the same word within various fields like this which works fine. SELECT `id`, `product-id`, `country`, `name`, `description`, `branch`, `stock`, `price` FROM `products` WHERE `name` LIKE "%car%" OR `description` LIKE "%car%" OR `branch` LIKE "%car%" OR `product-id` LIKE "%car%" The problem is that now I want a different query. I'd like to show all the cars from an specific country only, plus the additional fields. So if I run

How to convert dependent subquery to join for better performance?

不问归期 提交于 2020-01-02 07:43:24
问题 I have a database that stores "themes" and every theme is associated with a whole bunch of images (=screenshots of these themes). Now I want to display the latest 10 themes and for every theme I only want to get one single image from the database (the one with the lowest ID). Currently my query looks like this (I am using a subquery): SELECT DISTINCT t.theme_id, t.theme_name, theme_date_last_modification, image_id, image_type FROM themes t, theme_images i WHERE i.theme_id = t.theme_id AND t