sql-standards

Are SQL ANY and SOME keywords synonyms in all SQL dialects?

妖精的绣舞 提交于 2020-01-03 07:18:34
问题 In Postgres, ANY and SOME are synonyms when used on the right hand side of a predicate expression. For example, these are the same: column = ANY (SELECT ...) column = SOME (SELECT ...) This is documented here: http://www.postgresql.org/docs/9.1/static/functions-subquery.html#FUNCTIONS-SUBQUERY-ANY-SOME I have observed ANY and SOME to be supported by at least these SQL DBMSs: DB2 Derby H2 HSQLDB Ingres MySQL Oracle Postgres SQL Server Sybase ASE Sybase SQL Anywhere Can I safely assume that all

COALESCE or CASE more efficient and/or standard

时间秒杀一切 提交于 2019-12-31 01:57:07
问题 In terms of x compared to y. Does x comply with sql standards better? [apologies if subjective] Is x more efficient than y? Or are these scripts completely different and to be used in different contexts? x SELECT * FROM a INNER JOIN b ON COALESCE(b.columntojoin, b.alternatecolumn) = a.columntojoin y SELECT * FROM a INNER JOIN b ON (case when b.columntojoin is null then b.alternatecolumn else b.columntojoin end) = a.columntojoin 回答1: COALESCE is essentially a shorthanded CASE statement. Both

Authoritative SQL standard documentation

坚强是说给别人听的谎言 提交于 2019-12-18 19:35:15
问题 I'm curious to know some more details about the various SQL standard's, i.e. SQL-92 , SQL:99 , SQL:2003 , SQL:2008 etc. There is a short and useful overview on Wikipedia, with links to very expensive documents. Why are those documents not open to public? Can I find some open and free information? Please, don't post links you found from Google. I'm interested in somewhat authoritative documentation only. 回答1: Quoting from one of my web sites: We all love open source software. Wouldn’t it be

Logical Processing Order or SQL Standard in WHERE clause

六月ゝ 毕业季﹏ 提交于 2019-12-11 08:21:20
问题 I was asked couple days ago about logical processing order of the SELECT statement and more specifically about aliases and where clause and I'm not sure about one issue. If we have a query like: SELECT name AS first_name FROM people WHERE first_name = 'Alan'; The reason why usage of aliases in WHERE clause will generate error is really logical processing order of the SELECT statement, or rather syntax parsing issue, or maybe is the rule from SQL standard? 回答1: It is the rule from the SQL

When to use the table operator APPLY

时间秒杀一切 提交于 2019-12-10 18:57:17
问题 I'm trying to understand the table operator APPLY . Here is the example: CREATE TABLE #y ( Name char(8), hoursWorked int); GO INSERT INTO #y VALUES ('jim',4); INSERT INTO #y VALUES ('michael',40); INSERT INTO #y VALUES ('raj',1000); INSERT INTO #y VALUES ('jason',7); INSERT INTO #y VALUES ('tim',50); GO CREATE TABLE #x ( Name char(8),game char(8), NumBets int); GO INSERT INTO #x VALUES ('jim','chess',4); INSERT INTO #x VALUES ('jim','BG',10); INSERT INTO #x VALUES ('jim','draughts',100);

SQL Server - conditional aggregation with correlation

馋奶兔 提交于 2019-12-03 15:30:20
问题 Background: The original case was very simple. Calculate running total per user from highest revenue to lowest: CREATE TABLE t(Customer INTEGER NOT NULL PRIMARY KEY ,"User" VARCHAR(5) NOT NULL ,Revenue INTEGER NOT NULL); INSERT INTO t(Customer,"User",Revenue) VALUES (001,'James',500),(002,'James',750),(003,'James',450), (004,'Sarah',100),(005,'Sarah',500),(006,'Sarah',150), (007,'Sarah',600),(008,'James',150),(009,'James',100); Query: SELECT *, 1.0 * Revenue/SUM(Revenue) OVER(PARTITION BY

SQL Server - conditional aggregation with correlation

本秂侑毒 提交于 2019-12-03 05:08:07
Background: The original case was very simple. Calculate running total per user from highest revenue to lowest: CREATE TABLE t(Customer INTEGER NOT NULL PRIMARY KEY ,"User" VARCHAR(5) NOT NULL ,Revenue INTEGER NOT NULL); INSERT INTO t(Customer,"User",Revenue) VALUES (001,'James',500),(002,'James',750),(003,'James',450), (004,'Sarah',100),(005,'Sarah',500),(006,'Sarah',150), (007,'Sarah',600),(008,'James',150),(009,'James',100); Query: SELECT *, 1.0 * Revenue/SUM(Revenue) OVER(PARTITION BY "User") AS percentage, 1.0 * SUM(Revenue) OVER(PARTITION BY "User" ORDER BY Revenue DESC) /SUM(Revenue)

COALESCE or CASE more efficient and/or standard

耗尽温柔 提交于 2019-12-02 02:44:03
In terms of x compared to y. Does x comply with sql standards better? [apologies if subjective] Is x more efficient than y? Or are these scripts completely different and to be used in different contexts? x SELECT * FROM a INNER JOIN b ON COALESCE(b.columntojoin, b.alternatecolumn) = a.columntojoin y SELECT * FROM a INNER JOIN b ON (case when b.columntojoin is null then b.alternatecolumn else b.columntojoin end) = a.columntojoin COALESCE is essentially a shorthanded CASE statement. Both are exactly the same. There is also ISNULL in SQL Server (differs in other DBMSs), but that's actually a non

BigQuery standard SQL: how to group by an ARRAY field

余生颓废 提交于 2019-12-02 00:06:57
问题 My table has two columns, id and a . Column id contains a number, column a contains an array of strings. I want to count the number of unique id for a given array, equality between arrays being defined as "same size, same string for each index". When using GROUP BY a , I get Grouping by expressions of type ARRAY is not allowed . I can use something like GROUP BY ARRAY_TO_STRING(a, ",") , but then the two arrays ["a,b"] and ["a","b"] are grouped together, and I lose the "real" value of my

BigQuery standard SQL: how to group by an ARRAY field

本秂侑毒 提交于 2019-12-01 20:54:58
My table has two columns, id and a . Column id contains a number, column a contains an array of strings. I want to count the number of unique id for a given array, equality between arrays being defined as "same size, same string for each index". When using GROUP BY a , I get Grouping by expressions of type ARRAY is not allowed . I can use something like GROUP BY ARRAY_TO_STRING(a, ",") , but then the two arrays ["a,b"] and ["a","b"] are grouped together, and I lose the "real" value of my array (so if I want to use it later in another query, I have to split the string). The values in this field