subquery

MySQL recursive subquery [duplicate]

淺唱寂寞╮ 提交于 2019-12-11 14:48:24
问题 This question already has answers here : Generating Depth based tree from Hierarchical Data in MySQL (no CTEs) (4 answers) Closed 6 years ago . Not sure the title accurately reflects what I need to do and this question is possibly out of the scope of Stack Overflow as I do not have much to work with, other than my problem. I will explain my scenario ... I have a reporting web application. There are three levels of users Senior Managers, Managers and Team Members. Each Manager is responsible

Performance: rank() vs sub-query. Sub query have lower cost?

冷暖自知 提交于 2019-12-11 14:37:32
问题 Inspired by this question I decided to test the rank() function, trying to see if sub query's are less efficient than rank. So I created a table: create table teste_rank ( codigo number(7), data_mov date, valor number(14,2) ); alter table teste_rank add constraint tst_rnk_pk primary key ( codigo, data_mov ); and inserted some records... declare vdata date; begin dbms_random.initialize(120401); vdata := to_date('04011997','DDMMYYYY'); for reg in 1 .. 465 loop vdata := to_date('04011997',

Fetch subquery value from cursor to parameter in PL/SQL

末鹿安然 提交于 2019-12-11 14:22:18
问题 I have below query to select the count of subquery: SELECT COUNT(*) FROM (select crs_cust.CUSTOMER_ID, subset.NEW_REFERENCE_ID FROM CRS_CUSTOMERS crs_cust INNER JOIN DAY0_SUBSET subset ON crs_cust.CUSTOMER_ID=subset.CURRENT_CUSTOMER_ID); Above query is executed from a cursor in PL/SQL, how can I fetch columns from subquery ( CUSTOMER_ID and NEW_REFERENCE_ID ) into respective parameter? The cursor is expected to have multiple records.Similiar like the following: p_Count := SELECT COUNT(*) FROM

Compute lag difference for different days

本秂侑毒 提交于 2019-12-11 14:19:08
问题 I need help computing a date difference across different rows with variable lag (specifically, rows that are not on the same day) without subqueries, joins, etc. I think this should be possible with some inline t-SQL aggregates that use OVER(PARTITION BY) clause, such as LAG , DENSE_RANK , etc., but I can't quite put a finger on it. This is for a SQL Server 2017 Developer's edition. A clarifying example: Consider a dataset with Job beginning and end dates (across various projects). Some jobs

How to use subquery to define Mysql SELECT LIMIT offset?

孤人 提交于 2019-12-11 14:12:04
问题 I have been trying to define "dynamically" the offset of a query. But when executing this query I always end up with a You have an error in your SQL syntax; When I do remplace the subquery by a number it does work fine. Is there something wrong in a query that has this shape? SELECT LengthOfStay FROM table1 LIMIT (SELECT CAST(COUNT(DISTINCT(LengthOfStay)) / 2 AS SIGNED) FROM table1 t1), 2; Ps. I casted it so I can make sure it's an integer. 回答1: You can not use a subquery as a LIMIT argument.

How can I copy all fields of one table to another, wider table in SQL Server 2005 Express?

佐手、 提交于 2019-12-11 13:49:27
问题 I know my title isn't exactly worded well, so let me clarify. I'm using SQL Server 2005 Express. I have a table that basically stores a "template," if you will. Using a car as an example, the fields would be something like: TemplateID Color Make Model Now, I have another table that represents an "instance" of the template. It contains all the fields of the template table plus some fields that are unique to an instance: InstanceID VIN SerialNumber Color Make Model I am writing a stored

SQL HAVING BETWEEN a date range

萝らか妹 提交于 2019-12-11 13:37:35
问题 I am trying to retrieve the count of records between a certain date range that is specific to the users min date and max date in another table. This is what I have so far, however it is excluding at least 13 records that I know of. Can you tell if there is an error in my logic? Thanks in advance for any input you have! SELECT rtam.dbo.wfm_process_instance.user_id AS user_id, MIN(rtam.dbo.wfm_process_instance.LOCAL_USER_START_TIME) AS Min_Date, MAX(rtam.dbo.wfm_process_instance.LOCAL_USER

nested select using count to tally each row in Postgres

时光总嘲笑我的痴心妄想 提交于 2019-12-11 13:36:46
问题 I have a table where each row is a unique order with a unique order_id, but users can have multiple rows/orders. Orders table -> order_id, user_id, orderedat (datetime), sales I am trying to return a query that calculates, for each order_id, how many previous orders the associated user has made. (Essentially, does this row represent the user's first order? 5th order? 20th order? etc.) I'm trying something like this nested select, but am getting an error "more than one row returned by a

MS Access Subquery that returns Multiple Fields

久未见 提交于 2019-12-11 13:24:48
问题 So im back again with more MS Access problems. I have a INSERT INTO query with a subquery that checks to see if the data already exists. SELECT name, course FROM foo WHERE (name, course) NOT IN (SELECT name, course FROM bar); to expound a little on what i am trying to accomplish since the above doesn't work. I am trying to select composite keys that do not exist already in the table bar. For example the following could be stored in the table bar: "John Doe" , "Calc 101" "John Doe" , "English"

Update fails because Subquery returned more than 1 value

橙三吉。 提交于 2019-12-11 12:31:51
问题 I Get the following error when i try to update my table although there's n't any sub query : Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. MY QUERY : UPDATE t1 SET t1.modified = 2 FROM TransActions AS t1 INNER JOIN Ruser R ON t1.USERID = r.USERID WHERE r.dep_code = 54 and r.dep_year =2014 and YEAR(t1.checktime) =2016 and MONTH(t1.checktime) =1 and t1.modified = 0 The data selected like