subquery

How to optimize delete query (with subquery) in Oracle?

随声附和 提交于 2019-12-24 09:25:15
问题 I have query like: delete from tableA where tableA.fk in (select id from tableB where tableB.column1='somevalue' and tableB.date between date1 and date2) ; Table tableB contains near 100,000,000 records. So select id from tableB where tableB.column1='somevalue' and tableB.date between date1 and date2 returns near 1,000,000 records. As result - delete doesn't work at all - problem with size of rollback segment. I cannot increase size of segment. How it can be executed? 回答1: If you are filling

nHibernate count rows in subquery

不打扰是莪最后的温柔 提交于 2019-12-24 08:05:03
问题 How can I do something like this in nHibernate: select count(*) from (subquery) It is a rather simple query in SQL, but the solution is not so obvious in nHibernate. An obvious solution would be something along the line of: var rowcount = Session.QueryOver<Entity>() .Select(Projections.Alias(Projections.Count(Projections.SubQuery(detachedQuery)), "count")) .FutureValue<int>(); However, this results in an ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than

Optimizing mysql query (likes/dislikes)

故事扮演 提交于 2019-12-24 07:09:07
问题 My website contains pieces of content on which users can vote (like/dislike similar to reddit upvotes). When selecting an individual piece of content, I run the following subqueries to get the number of likes, the number of dislikes and the current user's vote. The votes are stored in a separate table {contentId, userId, vote} SELECT [... BUNCH OF FIELDS ...] (SELECT COUNT(*) FROM votes vt WHERE vt.cId = c.contentId AND vote = '.Constants::LIKE.') AS likes, (SELECT COUNT(*) FROM votes vt

Salesforce - Apex - query accounts based on Activity History

不羁的心 提交于 2019-12-24 04:18:15
问题 Hey Salesforce experts, I have a question on query account information efficiently. I would like to query accounts based on the updates in an activityHistory object. The problem I'm getting is that all the accounts are being retrieved no matter if there's "complete" activeHistory or not. So, Is there a way I can write this query to retrieve only accounts with activeHistory that has status="complete" and Type_for_reporting='QRC'? List<Account> AccountsWithActivityHistories = [ SELECT Id ,Name

Select Max Data from Subquery SQL, but it Show All Results from Subquery

大城市里の小女人 提交于 2019-12-24 03:54:07
问题 SELECT MAX( t.valuesum ) AS total, t.codebook FROM ( SELECT SUM( value ) AS valuesum, codebook FROM stock GROUP BY codebook ) t GROUP BY t.codebook In the subquery, the output is : b001 35, b002 20, b0003 11 I want to only see: b001 35. Please help me. 回答1: 1st solution using where condition: SELECT t1.valuesum, t1.codebook FROM ( SELECT SUM( s.value ) AS valuesum, s.codebook FROM stock s GROUP BY s.codebook ) t1 WHERE t1.valuesum in (SELECT MAX( t2.valuesum ) AS total FROM ( SELECT SUM( s2

Django: Annotation on Subquery

点点圈 提交于 2019-12-24 03:04:29
问题 I'm trying to annotate a queryset of Station s with the id of the nearest neighbouring Station using Django 2.0.3 and PostGIS (GeoDjango) functions. Simplified Station model: class Station(models.Model): name = models.CharField(max_length=128) location = models.PointField() objects = StationQuerySet.as_manager() The problem I'm having is trying to compute the closest distance, which involves annotating a subquery which refers to the location in the outer queryset. from django.db.models import

How to write this SQL sub-query?

感情迁移 提交于 2019-12-24 02:57:17
问题 I have this query which is running perfectly From this query I am selecting all restaurant 3 KM from my location this is my 1st table. SELECT foodjoint_id,foodjoint_name,open_hours,cont_no,address_line,city ( 3959 * acos( cos( radians('".$userLatitude."') ) * cos( radians( foodjoint_latitude) ) * cos( radians( foodjoint_longitude) - radians('".$userLongitude."') ) + sin( radians('".$userLatitude."') ) * sin( radians( foodjoint_latitude) ) ) ) AS distance FROM provider_food_joints HAVING

how to select the value from sub query result

[亡魂溺海] 提交于 2019-12-24 01:58:16
问题 I have the following 4 tables mentioned below and trying to extract the value ACC_NUMBER from sub query, Please help me with optimized correct syntax ACCOUNT TABLE ------------------------- |ACC_NUMBER | ACC_NAME| ------------------------- ACCOUNT_DETAILS TABLE --------------------------------- |ACC_NUMBER |DEAL_NUMBER|DEAL_TYPE| --------------------------------- DEALS TABLE ------------------------------------ |DEAL_NUMBER |DEAL_TYPE|DEP_NUMBER | ------------------------------------

Oracle ProC INSERT INTO VALUES ( (select …) )

别等时光非礼了梦想. 提交于 2019-12-24 01:54:27
问题 Running Pro*C on Oracle 10g. I am looking to do a subquery within an insert statement values clause. This sql query is fully valid and runs within TOAD with no problems, but Pro*C fails to parse the query. EXEC SQL INSERT INTO TARGET_ATTACHMENT ( TARGET_ID FILENAME ) VALUES ( :targetID, ( SELECT CREATED_FLAG from TARGET t where t.TARGET_ID = :targetID ) || '.tif' ) If I remove: ( SELECT (CREATED_FLAG || DISPLAY_ID) from TARGET t where t.TARGET_ID = :targetID ) ||**". The Pro*C compiler works

What is the difference of using HAVING vs a subquery

孤者浪人 提交于 2019-12-24 01:16:51
问题 I am new to SQL and doing the learning via datacamp. I was wondering if you can achieve the same result with 'HAVING' as with a nested 'WHERE' clause. Related: SQL - having VS where I understand that HAVING is used with aggregate functions such as min, max, .. How could I rewrite the following with HAVING?: SELECT * FROM populations WHERE year = 2015 AND life_expectancy >( SELECT AVG(life_expectancy)*1.15 FROM populations ); Suppose I have 6 columns in the table 'populations': A (character),