subquery

Selecting COUNT from different criteria on a table

主宰稳场 提交于 2019-12-20 10:28:35
问题 I have a table named 'jobs'. For a particular user a job can be active, archived, overdue, pending, or closed. Right now every page request is generating 5 COUNT queries and in an attempt at optimization I'm trying to reduce this to a single query. This is what I have so far but it is barely faster than the 5 individual queries. Note that I've simplified the conditions for each subquery to make it easier to understand, the full query acts the same however. Is there a way to get these 5 counts

SQL Server 2005 error when grouping using subquery

好久不见. 提交于 2019-12-20 06:40:02
问题 Using SQL Server 2005 I'm trying to group based on a case statement with a subquery, but I'm getting an error ("Each GROUP BY expression must contain at least one column reference. "). I can work round it quite easily, but can anyone explain the error? I've got a column reference to #header.header. create table #header (header int) create table #detail (header int, detail int) insert into #header values (1) insert into #header values (2) insert into #header values (3) insert into #detail

Select max value in subquery in SQL

假装没事ソ 提交于 2019-12-20 06:39:13
问题 I have a query like below: select * from (select centre_name, sum(qty) as number1 from (select exchange_from_centre_id as cenid, count(exchange_from_centre_id) as qty from as2.exchange group by exchange_from_centre_id union all select exchange_to_centre_id as cenid, count(exchange_to_centre_id) as qty from as2.exchange group by exchange_to_centre_id), as2.centre c where c.centre_id = cenid group by centre_name); and this is the result: Name of the centre and the number of exchange Alice

SQL : How can I use sub query in a query with group by section?

笑着哭i 提交于 2019-12-20 06:33:01
问题 How can I use sub query in a query with group by section? I use SQL Server 2008 R2 AND Delphi 2010 I receive this error: Cannot perform an aggregate function on an expression containing an aggregate or a sub query. Like this query : select t1.sen, sum(t1.d1)as d1, sum(t1.d2)as d2, sum(t1.d1+t1.d2) as d_sum, Round((sum((1000*(t1.d1+t1.d2))/(9500-( select sum(t2.t_shab+t2.t_rooz) from tbl1 t2 where FCode=81 AND DCode=1 AND t2.sen<=t1.sen )))),1) as SSS from tbl1 t1 where FCode = 81 AND DCode =

Strange paginate behaviour when paginating a subquery in flask

南笙酒味 提交于 2019-12-20 06:26:32
问题 I am working in a flask project (my very first project in flask) and I am having a problem when trying to paginate a query that contains a subquery. Just to put you in the picture I have three models: User, Magazine and Article. Each User is subscribed to several Magazines, each Article belongs to a Magazine. I am using queries and paginating the results in several parts of the code without any problem except when I am using a subquery. Below is the code where I am retrieving all the users

How can I modify this query with two Inner Joins so that it stops giving duplicate results?

ぐ巨炮叔叔 提交于 2019-12-20 04:09:23
问题 EDIT: I will leave the post here as is, but what I really needed to accomplish needed to be reposted. I didn't explain the problem well enough. After trying again with quite a different starting point, I was able to get the query that I needed. That is explained here. ORIGINAL QUESTION: I'm having trouble. I have looked at similar threads, and I am unable to find a solution specific to this query. The database is very large, and group by seems to slow it down immensely. The problem is I am

Oracle SQL correlated update

a 夏天 提交于 2019-12-20 03:17:20
问题 I got three tables: t1.columns: a,c t2.columns: a,b t3.columns: b,c,d Now what I want is to update t1.c with t3.d. But I can't just update t1 from t3 using t1.c = t3.c I also have to go though t3.b = t2.b and t1.a = t2.a. I've tried something like this: UPDATE table1 t1 SET t1.c = (select t3.d from table2 t2, table3 t3 where t2.b = t3.b and t1.a = t2.a) WHERE EXISTS ( SELECT 1 FROM table2 t2, table3 t3 WHERE t1.c = t3.c and t1.a = t2.a); This code generates error-msg: ORA-01427: single-row

MySql variables and php

杀马特。学长 韩版系。学妹 提交于 2019-12-20 03:16:49
问题 I am getting an error with this in php. What is the correct way to format this string to pass to mysql_query() in php? SELECT count(*) FROM agents INTO @AgentCount; SELECT user_agent_parsed, user_agent_original, COUNT( user_agent_parsed ) AS thecount, COUNT( * ) / ( @AgentCount) AS percentage FROM agents GROUP BY user_agent_parsed ORDER BY thecount DESC LIMIT 50; In php, here is how I set up the $query $query = " SELECT count(*) FROM agents INTO @AgentCount; SELECT user_agent_parsed, user

PostgreSQL subquery with syntax error gives a valid result

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 02:46:29
问题 What is happening here? I got two tables, test1 and test2: create table test1 (id1 int4 primary key); create table test2 (id2 int4 primary key); As expected, this query: select id1 from test2; produces a syntax error: ERROR: column "id1" does not exist LINE 1: select id1 from test2; However, when I try to execute this query: select * from test1 where id1 in (select id1 from test2); PostgreSQL doesn't complain, executes the query and gives me: id1 ----- (0 rows) Is there any logic in this? Or

Subqueries in CakePHP 3?

半世苍凉 提交于 2019-12-20 02:37:25
问题 I have two tables products and product_categories that are associated through a third table, products_categories_products , according to CakePHP BelongsToMany conventions (Edit: these associations are established in ProductsTable.php and ProductCategoriesTable.php ). I want to generate a list of product categories, using the image from the best selling products to represent each category. I can achieve my desired outcome using the following function: public function findImages(Query $query,