nested-queries

Simplify nested case when statement

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 03:35:33
问题 Below is my current SELECT CASE statement: SELECT CASE WHEN edition = 'STAN' AND has9 = 1 THEN '9' WHEN edition = 'STAN' AND has8 = 1 THEN '8' WHEN edition = 'STAN' AND has7 = 1 THEN '7' WHEN edition = 'STAN' AND hasOLD = 1 THEN 'OLD' WHEN edition = 'SUI' AND has_s9 = 1 THEN 'S9' WHEN edition = 'SUI' AND has_s8 = 1 THEN 'S8' ELSE 'S7' END AS version I do not always want to repeat the edition = 'xxx' condition, such as CASE WHEN edition = 'STAN' AND has9 = 1 THEN '9' ELSE WHEN has8 = 1 THEN '8

Are SELECT type queries the only type that can be nested?

橙三吉。 提交于 2019-12-17 07:54:49
问题 Is it possible to have an non-select query (update, insert, delete) embedded into another query? Something like (an insert inside a select) A single query: select such,and,such from .... where .... insert into .... ; 回答1: Basic answer There are CTEs (Common Table Expressions) in Postgres (like in any major modern RDBMS except MySQL). Since version 9.1 that includes data-modifying CTEs. Those can be "nested". Update: MySQL 8.0 finally adds CTEs. Unlike subqueries CTEs pose as optimization

How to solve the following case?

泄露秘密 提交于 2019-12-13 09:25:13
问题 Consider the following question: We have 3 tables, 1. Theatre ( theatre_Id, thatre_name ) 2. ShowTime ( showTimeId, theatre_Id, movie_id ) 3. Movies ( movie_id, movie_name ) Now same movie name can also have different movieId's sort of dependent on the reel. Eg: [1, HarryPotter], [2, HarryPotter], [3, Pirates of Carr] Now we need to find movie name which has showtime on all theatre locations ? Is it nested correlated query ? 回答1: If you better phrase the question: What are the names of movies

MYSQL insert after sub query with count

落爺英雄遲暮 提交于 2019-12-12 11:34:31
问题 I'm trying to INSERT some data into a table but only when the subquery COUNT is > 0. This is what I have so far. INSERT INTO users_friends (userId, friendId) VALUES (77, 100) WHERE (SELECT COUNT(id) FROM users WHERE email = 'a@g.com') > 0 Both queries work independently FYI. This should be a simple fix hopefully. Cheers 回答1: SQLFiddle demo if there are records 'a@g.com' SQLFiddle demo if there are NOT records 'a@g.com' INSERT INTO users_friends (userId, friendId) SELECT 77, 100 FROM users

Show Nested query result in outer query

本小妞迷上赌 提交于 2019-12-12 02:27:04
问题 I have a table say MBR_Role . Now it has member id as key (Primary constraint). Correspondng to each member id, there can be different type of adresses based on two indicators. I have two indicators, Ind1 and Ind2. So my table is like - Mbr ID Ind1 Ind2 Country City Postal_code 1 Y N UK London 11111 1 N Y Spain Madrid 22222 1 N N US New York 33333 2 Y N France Paris 44444 Indicator 1 gives residential address, Indicator 2 gives office address. I want both these addresses in the result for

Fastest AWS DB service for large media nested metadata querying

六眼飞鱼酱① 提交于 2019-12-11 15:44:24
问题 I'm trying to determine what might be the best suited (mainly in terms of speed) database service for querying metadata of media content such as images/videos/audio located on AWS S3. Currently I'm looking at DynamoDB and Redshift, but there may be better alternatives I haven't considered. Example use case: I have millions of images (and cropped sections of images) ran through a web of machine learning full-image classification, bounding-box object detection, and pixel segmentation (RLE pixel

trying to get MySQL data into nested json file for d3?

随声附和 提交于 2019-12-11 09:49:49
问题 I am trying to get MySQL database data into json format. My file needs to be in the format of nested parents and children. (see snippet) This is because I am trying to use a d3 data visualization (collapsible tree layout) that requires that the json data be formatted in a certain way. Below I have attached a snippet of what I the json file to look like. I am having some issues trying to write nested sql statements to do this. I have also attached a visual of my database to see what I am

How to select last 30 days dates in MySQL?

丶灬走出姿态 提交于 2019-12-10 17:22:36
问题 Can I list somehow the dates of last 30 days in MySQL ? Not from a table! For example I think about like this: SELECT date WHERE date BETWEEN SUBDATE(NOW(), INTERVAL 30 DAY) AND NOW(); Is that possible? 回答1: I hacked this together from someone else's code, but it seems to work: SELECT DATE_FORMAT(m1, '%d %b %Y') FROM ( SELECT SUBDATE( NOW() , INTERVAL 30 DAY) + INTERVAL m DAY AS m1 FROM ( select @rownum:=@rownum+1 as m from (select 1 union select 2 union select 3 union select 4) t1, (select 1

SUM values in SQL starting from a specific point in another table

 ̄綄美尐妖づ 提交于 2019-12-10 09:49:15
问题 I have a table that lists the index/order, the name, and the value. For example, it looks like this: TABLE1: ID | NAME | VALUE 1 | A | 2 2 | B | 5 3 | C | 2 4 | D | 7 5 | E | 0 Now, I have another table that has a random list of NAMEs. It'll just show either A, B, C, D, or E. Depending on what the NAME is, I wanted to calculate the SUM of all the values that it will take to get to E. Does that make sense? So if for example, my table looks like this: TABLE2: NAME D B A I'd want another column

SUM values in SQL starting from a specific point in another table

爷,独闯天下 提交于 2019-12-05 21:26:34
I have a table that lists the index/order, the name, and the value. For example, it looks like this: TABLE1: ID | NAME | VALUE 1 | A | 2 2 | B | 5 3 | C | 2 4 | D | 7 5 | E | 0 Now, I have another table that has a random list of NAMEs. It'll just show either A, B, C, D, or E. Depending on what the NAME is, I wanted to calculate the SUM of all the values that it will take to get to E. Does that make sense? So if for example, my table looks like this: TABLE2: NAME D B A I'd want another column next to NAME that'll show the sum. So D would have 7 because the next event is E. B would have to be