sql

Explain SQL query using UML diagram? [closed]

让人想犯罪 __ 提交于 2021-02-16 13:38:29
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 9 days ago . Improve this question I have to present some complex queries and PL/SQL codes to people who don't know anything about my project. I need to explain how tables are connected, how conditions are affecting the outcome, joins, loops, etc. Is there a way to document this SQL and PL/SQL

Is there any significance in the order of table in sql join statement

旧时模样 提交于 2021-02-16 09:47:13
问题 Is there any significance in the order of table in sql join statement. For example SELECT dept_name, emp_name FROM Employee INNER JOIN Department ON Employee.dept_id = Department.dept_id and SELECT dept_name, emp_name FROM Department INNER JOIN Employee ON Employee.dept_id = Department.dept_id Is there is any performance advantage in the order of tables? 回答1: No there isn't. Most (if not all) DBMS's use of a Cost based optimizer. The order in which you specify your statements does not affect

How can I get the INSERTED and UPDATED rows for an UPSERT operation in postgres

烈酒焚心 提交于 2021-02-16 09:34:09
问题 I've got an UPSERT Operation like this: INSERT INTO people (SELECT * FROM people_update) ON CONFLICT (name,surname) DO UPDATE SET age = EXCLUDED.age , street = EXCLUDED.street , city = EXCLUDED.city , postal = EXCLUDED.postal WHERE (people.age,people.street,people.city,people.postal) IS DISTINCT FROM (EXCLUDED.age,EXCLUDED.street,EXCLUDED.city,EXCLUDED.postal) RETURNING case when xmax::text::int > 0 then 'updated' else 'inserted' end,name,surname,age,street,city,postal; (name,surname) is a

SQL: Comparing two counts from different Tables

风格不统一 提交于 2021-02-16 09:34:05
问题 I have 3 Tables one with every Country in the World and its token NAME CODE Afghanistan AFG Albania AL Algeria DZ American Samoa AMSA Andorra AND Angola ANG Anguilla AXA (...) And and a Table of all lakes and another one of all mountains in these countries. LAKE CODE Bodensee A Neusiedlersee A Lake Prespa AL Lake Ohrid AL Lake Skutari AL Lake Eyre AUS Lake Jindabyne AUS Lake Hume AUS Lake Eucumbene AUS Lake Hume AUS Lake Burley Griffin AUS (...) MOUNTAIN CODE Hochgolling A Hochgolling A

How to split array into rows in Postgresql

本秂侑毒 提交于 2021-02-16 08:40:32
问题 When running this query: SELECT id,selected_placements FROM app_data.content_cards I get a table like this: +----+-------------------------------+ | id | selected_placements | +----+-------------------------------+ | 90 | {162,108,156,80,163,155,NULL} | +----+-------------------------------+ | 91 | {} | +----+-------------------------------+ What I want to do now is get this same information but with the arrays splitted into rows so I get a result like this: +----+---------------------+ | id

How to split array into rows in Postgresql

北战南征 提交于 2021-02-16 08:38:42
问题 When running this query: SELECT id,selected_placements FROM app_data.content_cards I get a table like this: +----+-------------------------------+ | id | selected_placements | +----+-------------------------------+ | 90 | {162,108,156,80,163,155,NULL} | +----+-------------------------------+ | 91 | {} | +----+-------------------------------+ What I want to do now is get this same information but with the arrays splitted into rows so I get a result like this: +----+---------------------+ | id

Working of Merge in SAS (with IN=)

烈酒焚心 提交于 2021-02-16 05:31:39
问题 I have two dataset data1 and data2 data data1; input sn id $; datalines; 1 a 2 a 3 a ; run; data data2; input id $ sales x $; datalines; a 10 x a 20 y a 30 z a 40 q ; run; I am merging them from below code: data join; merge data1(in=a) data2(in=b); by id; if a and b; run; Result: (I was expecting an Inner Join result which is not the case) 1 a 10 x 2 a 20 y 2 a 30 z 2 a 40 w Result from proc sql inner join. proc sql; select data1.id,sn,sales,x from data2 inner join data1 on data1.hh_id; quit;

Working of Merge in SAS (with IN=)

故事扮演 提交于 2021-02-16 05:31:09
问题 I have two dataset data1 and data2 data data1; input sn id $; datalines; 1 a 2 a 3 a ; run; data data2; input id $ sales x $; datalines; a 10 x a 20 y a 30 z a 40 q ; run; I am merging them from below code: data join; merge data1(in=a) data2(in=b); by id; if a and b; run; Result: (I was expecting an Inner Join result which is not the case) 1 a 10 x 2 a 20 y 2 a 30 z 2 a 40 w Result from proc sql inner join. proc sql; select data1.id,sn,sales,x from data2 inner join data1 on data1.hh_id; quit;

Converting NOT IN to NOT EXISTS

心不动则不痛 提交于 2021-02-16 05:03:32
问题 Having a nightmare of a time understanding the usage of NOT EXISTS, primarily how to convert my NOT IN solution below so that I can actually understand how I achieved the results. Have several articles on askTom, the oracle forums and stackoverflow, but can't find anything that clearly helps understand this problem. My apologies if I have missed it through my noobish searching. SELECT s.S_Fname, s.S_Lname FROM STUDENT s WHERE s.S_Sex = 'F' AND S.S_Id NOT IN(SELECT e.S_Id FROM ENROLLMENT e

sql case when end as

旧时模样 提交于 2021-02-15 13:02:01
CASE 可能是 SQL 中被误用最多的关键字之一。虽然你可能以前用过这个关键字来创建字段,但是它还具有更多用法。例如,你可以在 WHERE 子句中使用 CASE 。 首先让我们看一下 CASE 的语法。在一般的 SELECT 中,其语法如下: SELECT CASE WHEN < A > THEN < somethingA > WHEN < B > THEN < somethingB > ELSE < somethingE > END as < myColumnSpec > 在上面的代码中需要用具体的参数代替尖括号中的内容。下面是一个简单的例子: USE pubs GO SELECT Title, CASE WHEN price IS NULL THEN ' Unpriced ' WHEN price < 10 THEN ' Bargain ' WHEN price BETWEEN 10 and 20 THEN ' Average ' ELSE ' Gift to impress relatives ' END as ' Price Range ' FROM titles ORDER BY price GO 这是 CASE 的典型用法,但是使用 CASE 其实可以做更多的事情。比方说下面的 GROUP BY 子句中的 CASE : SELECT ' Number of