subquery

WHERE value IS NOT IN (subquery)

懵懂的女人 提交于 2020-01-02 02:04:26
问题 I've been struggling with this query. I have two tables. One with coupons and Invoicenumbers. One with Invoicenumbers and customer names. I need to get the customers who have not used a given coupon. Here are the tables: Promotion table: Promotions Invoice | Coupon ---------------- 1 | couponA 2 | couponB 3 | couponB Orders Table: Orders Invoice | Customer ------------------ 1 | Jack 2 | Jack 3 | Jill So Jack has used coupons A and B. And Jill has only used coupon B. If my query were select

query with count subquery, inner join and group

梦想的初衷 提交于 2020-01-01 05:15:12
问题 I'm definitely a noob with SQL, I've been busting my head to write a complex query with the following table structure in Postgresql: CREATE TABLE reports ( reportid character varying(20) NOT NULL, userid integer NOT NULL, reporttype character varying(40) NOT NULL, ) CREATE TABLE users ( userid serial NOT NULL, username character varying(20) NOT NULL, ) The objective of the query is to fetch the amount of report types per user and display it in one column. There are three different types of

using not with sub-queries

被刻印的时光 ゝ 提交于 2019-12-31 05:43:05
问题 I have been asked to display results in my sql server database for the following question What software packages are not installed on any HP computers? I have tried the following but I am still getting results for the PACKNAME Manta but that package is installed on an HP computer. What am I missing? select * from package where PACK in ( select PACK from software where TAGNUM in ( select tagnum from PC where comp NOT in ( select comp from computer where MFRNAME = 'HP'))) I have attached an

MySQL ERROR 1349 What I am missing?

懵懂的女人 提交于 2019-12-30 11:38:11
问题 I am getting a MySQL 1349 error, but it appears as though the error is incorrect: ERROR 1349: View's SELECT contains a subquery in the FROM clause Can I not have ANY subqueries when creating a view? Here is my SQL: CREATE VIEW `wordpress`.`ffi_be_v_book_details` AS ( SELECT ffi_be_courses. * , COALESCE( `Total` , 0 ) AS `Total` FROM `ffi_be_courses` LEFT JOIN ( SELECT * , COUNT( `Course` ) AS `Total` FROM ffi_be_courses RIGHT JOIN ( SELECT `Course` FROM `ffi_be_bookcourses` LEFT JOIN `ffi_be

PostgreSQL use value from previous row if missing

江枫思渺然 提交于 2019-12-30 10:09:09
问题 I have a following query: WITH t as ( SELECT date_trunc('hour', time_series) as trunc FROM generate_series('2013-02-27 22:00'::timestamp, '2013-02-28 2:00', '1 hour') as time_series GROUP BY trunc ORDER BY trunc ) SELECT DISTINCT ON(trunc) trunc, id FROM t LEFT JOIN ( SELECT id, created, date_trunc('hour', created) as trunc_u FROM event ORDER BY created DESC ) u ON trunc = trunc_u which yields the following result: "2013-02-27 22:00:00"; "2013-02-27 23:00:00";2 "2013-02-28 00:00:00";5 "2013

What is wrong in my nested SUBQUERY predicate?

淺唱寂寞╮ 提交于 2019-12-30 06:38:09
问题 I have the data model you can see below, and a nested SUBQUERY predicate, but in somehow it just not works. Any idea how to correct it? I figured out, this here down is working finally: [NSPredicate predicateWithFormat:@"SUBQUERY(bs, $B, SUBQUERY($B.cs, $C, $C.ds.name != \"xxx\").@count > 0).@count > 0"]; 回答1: Ok, so here is the working solution: [NSPredicate predicateWithFormat:@"SUBQUERY(bs, $B, SUBQUERY($B.cs, $C, $C.ds.name != \"xxx\").@count > 0).@count > 0"]; 来源: https://stackoverflow

What is wrong in my nested SUBQUERY predicate?

纵饮孤独 提交于 2019-12-30 06:38:05
问题 I have the data model you can see below, and a nested SUBQUERY predicate, but in somehow it just not works. Any idea how to correct it? I figured out, this here down is working finally: [NSPredicate predicateWithFormat:@"SUBQUERY(bs, $B, SUBQUERY($B.cs, $C, $C.ds.name != \"xxx\").@count > 0).@count > 0"]; 回答1: Ok, so here is the working solution: [NSPredicate predicateWithFormat:@"SUBQUERY(bs, $B, SUBQUERY($B.cs, $C, $C.ds.name != \"xxx\").@count > 0).@count > 0"]; 来源: https://stackoverflow

Is it possible to use a Stored Procedure as a subquery in SQL Server 2008?

杀马特。学长 韩版系。学妹 提交于 2019-12-30 05:39:05
问题 I have two stored procedures, one of which returns a list of payments, while the other returns a summary of those payments, grouped by currency. Right now, I have a duplicated query: the main query of the stored procedure that returns the list of payments is a subquery of the stored procedure that returns the summary of payments by currency. I would like to eliminate this duplicity by making the stored procedure that returns the list of payments a subquery of the stored procedure that returns

SQL Server DELETE is slower with indexes

蹲街弑〆低调 提交于 2019-12-30 04:47:09
问题 I have an SQL Server 2005 database, and I tried putting indexes on the appropriate fields in order to speed up the DELETE of records from a table with millions of rows ( big_table has only 3 columns), but now the DELETE execution time is even longer ! (1 hour versus 13 min for example) I have a relationship between to tables, and the column that I filter my DELETE by is in the other table. For example DELETE FROM big_table WHERE big_table.id_product IN ( SELECT small_table.id_product FROM

SUBQUERY in core data

非 Y 不嫁゛ 提交于 2019-12-29 06:31:12
问题 I can't understand what's wrong with my predication. I have the next database scheme: DataItem(color_ids) <->> (dataItem)Color Where Color contains colorID(int). I tried to get all DataItems that contain colorID == 5. I have used the next predicate: SUBQUERY(color_ids, $sub, $sub.colorID==5).@count > 0 Thanks for your help. 回答1: You don't need SUBQUERY for this. In fact, you almost never need SUBQUERY ; it is extremely rare to find a situation where it is the correct thing to use. You can do