subquery

Using alias in subquery

末鹿安然 提交于 2019-12-11 17:58:36
问题 I'm running the following query to get the open positions on a portfolio: SELECT SUM(trades.quantity) as total_quantity, SUM(trades.price) as total_cost, SUM(trades.price)/SUM(trades.quantity) as cost_per_share, trades.ticker, tickers.code FROM (trades) LEFT JOIN tickers ON trades.ticker = tickers.id GROUP BY tickers.code HAVING total_quantity > 0 ORDER BY tickers.code I'd like to add an extra column to show the weightening of a position, i.e.: total_cost/SUM(total_cost) -- Dividing any given

How to refactor select subqueries into joins?

故事扮演 提交于 2019-12-11 17:57:37
问题 I have an extremely ugly complex query that returns results for a race based on a series of groupings. I have a table that contains registration information (name, etc), and a table that contains finish times. I need to get a place for each race, class, and division (they are sub groups of each other). What I have working right now is this: Select reg.name, r.fin_time ( select count(*)+1 from ( Select temp_reg.id as regid, temp_reg.race_id as in_race, temp_res.division as in_div, COALESCE

correlated query /subquery VS join query

喜欢而已 提交于 2019-12-11 17:53:03
问题 can we always convert a usual subquery/correlated subquery to join type query? 回答1: yes and no... Yes, in that JOIN/EXISTS/IN usually give the same plan and are often the same No, in that if the EXISTS table has > 1 row for the main table you'll need DISTINCT to avoid dupes I'd stick with an EXISTS type query if this is relevant and you don't need data from the EXISTS table 回答2: Yes, but it means that what you JOIN to can appear in the result set. A subquery in either an EXISTS or IN clause

MySQL - Subquery in SELECT clause

耗尽温柔 提交于 2019-12-11 17:27:09
问题 I am trying to use the results of a query in the SELECT clause. I query the information_schema to get the column names to select. I then use CONCAT and GROUP_CONCAT to format the results into the following form: table1.column1,table1.column2,table1.column3,table1.column4 Here is the statement: SELECT Group_concat(DISTINCT Concat("table1.", column_name)) FROM information_schema.columns WHERE table_name = "table1"; Now, I would like to use these results in a query like this: SELECT (SELECT

Correlated SubQuery SQL to LINQ

守給你的承諾、 提交于 2019-12-11 17:13:30
问题 select * from Table1 where TC in (select TC from Table2 where Application in ('AAA'))` help me in converting above query to LINQ. 回答1: Without where Application in ('AAA') part this looks quite simple: from t1 in db.Table1s where db.Table2s.Select(t2 => t2.TC).Contains(t1.TC) from t1 in db.Table1s UPDATE (How wrong I was!) List<string> myCollection = new List<string> { "AAA" }; from t1 in db.Table1s where db.Table2s.Where(t2 => myCollection.Contains(t2.Application)).Select(t2 => t2.TC)

Subquery aggregate function with SUM(CASE SUBQUERY)

只愿长相守 提交于 2019-12-11 17:08:35
问题 I'm getting an error while executing my query Cannot perform an aggregate function on an expression containing an aggregate or a subquery. Code: SELECT S.id, SUM(CASE WHEN sc.coverage IN (SELECT number FROM ArrayOfIntegersFromString(@dynamicData)) THEN 1 ELSE 0 END) as sm FROM Storefronts s LEFT JOIN StorefrontCoverages sc ON s.id = sc.storefront LEFT JOIN Vendors v ON s.vendor = v.Id WHERE ( v.active = 1 AND s.approved = 1 AND s.status = 1 ) GROUP BY S.id HAVING SUM(CASE WHEN sc.coverage IN

MySQL count on precondition?

六眼飞鱼酱① 提交于 2019-12-11 17:02:10
问题 Consider the scheme below id user tag created 1 foo tag1 2018-09-01 2 foo tag2 2018-09-01 3 bar tag1 2018-09-02 4 bar tag2 2018-09-03 5 kkk tag2 2018-09-05 6 qqq tag1 2018-09-12 How to find the number of unique user, which after the row 'tag1' there is a 'tag2' followed in future row of that user? The answer is 2 (foo,bar), based on the above query 回答1: You can use aggregation to get the list of users: select user from t group by user having min(case when tag = 'tag1' then created end) < max

Postgres where clause over two columns

五迷三道 提交于 2019-12-11 15:33:47
问题 Database - I am working on in Postgres 9.6.5 I am analyzing the data from US Airport Authority (RITA) about the flights arrival and departures. This link (http://stat-computing.org/dataexpo/2009/the-data.html) lists all the columns in the table. The table has following 29 columns No Name Description 1 Year 1987-2008 2 Month 1-12 3 DayofMonth 1-31 4 DayOfWeek 1 (Monday) - 7 (Sunday) 5 DepTime actual departure time (local, hhmm) 6 CRSDepTime scheduled departure time (local, hhmm) 7 ArrTime

MySQL subtract from isolated subquery

流过昼夜 提交于 2019-12-11 15:23:59
问题 I have a table containing inventory ID | Product ID | In_Transit | Quantity | Cumulative Quantity =====+================+==============+==============+==================== 1 | 1 | 0 | 1000 | 1000 2 | 1 | 0 | 1 | 1001 3 | 1 | 1 | 54 | 1055 4 | 1 | 1 | 1 | 1056 So the total inventory for product id 1 is '1056' I get this using a SELECT MAX(ID) subquery join with the table to get its cumulative quantity which is 1056. I would like to get the Inventory total (subtracting all the amounts in

ORDER BY is being ignored in subquery join?

依然范特西╮ 提交于 2019-12-11 14:48:47
问题 I have 3 tables: users, projects, and files. Here's the relevant columns: users: [userid](int) projects: [userid](int) [projectid](int) [modified](datetime) files: [userid](int) [projectid](int) [fileid](int) [filecreated](datetime) I'm using a query to list all projects, but I also want to include the most recent file from another table. My approach to this was using a subquery to join on. Here's what I came up with, but my problem is that it's returning the oldest file: SELECT * FROM