ansi-sql

Dynamic SQL using config tables

放肆的年华 提交于 2020-01-17 05:29:12
问题 I have a table which contains a list of dynamic SQL views that needs to be created SEEDING_TABLE ------------- KEYVALUE|VIEW_TO_BE_CREATED|FROMTABLE|NOOFCOLS 1|A|A1|3 2|B|B1|4 3|C|C1|5 The other table which contains the actual column names for the above seeding table ORDERCOLS_FORVIEW KEYVALUE|FROMTABLE|COLSAVAILABLE 1|A1|NUM1 1|A1|NUM2 1|A1|NUM3 2|B1|NUM1 2|B1|NUM2 2|B1|NUM3 2|B1|NUM4 3|C1|NUM1 3|C1|NUM2 3|C1|NUM3 3|C1|NUM4 3|C1|NUM5 Definition of the table FROMTABLEs as follows A1 ->

Postgres Next/Previous row SQL Query

自古美人都是妖i 提交于 2020-01-10 23:17:13
问题 I have the following table structures in a Postgres 9.1 database but the ideal solution should be DB agnostic if possible: Table: users |id|username| |1 |one | |2 |two | |3 |three | Table: items |id|userid|itemname|created | |1 |1 |a |timestamp| |2 |1 |b |timestamp| |3 |1 |c |timestamp| |4 |2 |d |timestamp| |5 |2 |e |timestamp| |6 |2 |f |timestamp| |7 |3 |g |timestamp| |8 |3 |h |timestamp| |9 |3 |i |timestamp| I have a query (for a view) which provides the next and previous item.id. e.g. View

Postgres Next/Previous row SQL Query

て烟熏妆下的殇ゞ 提交于 2020-01-10 23:15:23
问题 I have the following table structures in a Postgres 9.1 database but the ideal solution should be DB agnostic if possible: Table: users |id|username| |1 |one | |2 |two | |3 |three | Table: items |id|userid|itemname|created | |1 |1 |a |timestamp| |2 |1 |b |timestamp| |3 |1 |c |timestamp| |4 |2 |d |timestamp| |5 |2 |e |timestamp| |6 |2 |f |timestamp| |7 |3 |g |timestamp| |8 |3 |h |timestamp| |9 |3 |i |timestamp| I have a query (for a view) which provides the next and previous item.id. e.g. View

Count unique ids in a rolling time frame

与世无争的帅哥 提交于 2020-01-10 04:01:04
问题 I have a simple table as bellow with lots of IDs and dates. ID Date 10R46 2014-11-23 10R46 2016-04-11 100R9 2016-12-21 10R91 2013-05-03 ... ... I want to formulate a query which counts the unique IDs for a rolling time frame of dates, for example ten days. Meaning that for each date it should give me the number of unique IDs between that date and 10 days back. Result should look something like this. UniqueTenDays Date 200 2014-11-23 324 2014-11-24 522 2014-11-25 532 2014-11-26 ... ...

Sql Ansi to manage DateTime values

北城余情 提交于 2020-01-05 10:32:04
问题 I'm developing a multi-database system. I want the difference between two dates in seconds. In SQL Server I got: DATEDIFF(second,stardate,enddate) In MySql : TIME_TO_SEC(TIMEDIFF(stardate,enddate)) My question: Does Sql Ansi have functions to manage DateTime values? i.e.: There are datetime functions generic for all databases? 回答1: According to SQL:1999, date1-date0 should give you a value of type INTERVAL, a struct from which you should be able to extract YEAR, MONTH, DAY, etc. I've never

Sql Ansi to manage DateTime values

喜你入骨 提交于 2020-01-05 10:31:25
问题 I'm developing a multi-database system. I want the difference between two dates in seconds. In SQL Server I got: DATEDIFF(second,stardate,enddate) In MySql : TIME_TO_SEC(TIMEDIFF(stardate,enddate)) My question: Does Sql Ansi have functions to manage DateTime values? i.e.: There are datetime functions generic for all databases? 回答1: According to SQL:1999, date1-date0 should give you a value of type INTERVAL, a struct from which you should be able to extract YEAR, MONTH, DAY, etc. I've never

ANSI Support of Select Count SQLStatements

不想你离开。 提交于 2020-01-03 02:00:06
问题 I'm wondering if there is a list of supported Select Count SQL statements per the ANSI standard? The below three variations are what I know of. Can the where clause be used on all three below? SELECT COUNT(*) AS RowCount FROM table_name SELECT COUNT(ColumnName) AS RowCount FROM table_name SELECT COUNT(DISTINCT ColumnName) AS RowCount FROM table_name 回答1: The SQL standard that almost all DBMS's use is the ANSI 92 standard, which can be found at http://www.contrib.andrew.cmu.edu/~shadow/sql

Ansi SQL type casting

帅比萌擦擦* 提交于 2020-01-01 09:39:54
问题 Is it allowed to cast types in ANSI SQL like in postgres for example: SELECT SUM( CAST(qnty AS int) - CAST(reserve AS int) ) AS sum ... qnty and reserve are character columns. 回答1: The CAST expression was added in SQL-92. You can see it for example in this draft. 来源: https://stackoverflow.com/questions/1530003/ansi-sql-type-casting

Standard alternative to CONNECT BY?

家住魔仙堡 提交于 2019-12-23 09:49:59
问题 I'm trying to convert some Oracle SQL queries to work with (in theory) any SQL database. Some of the queries are hierarchical in nature and are written using CONNECT BY. Is there a standard SQL alternative to Oracle's START WITH...CONNECT BY syntax? Or is there some recommended process I should follow to convert the hierarchical queries? 回答1: In Oracle 11gR2 they support recursion in Common Table Expressions (what most Oracle people know as sub-querying factoring, i.e. the WITH clause). As

Early (or re-ordered) re-use of derived columns in a query - is this valid ANSI SQL?

时光毁灭记忆、已成空白 提交于 2019-12-23 09:41:31
问题 Is this valid ANSI SQL?: SELECT 1 AS X ,2 * X AS Y ,3 * Y AS Z Because Teradata (12) can do this, as well as this (yes, crazy isn't it): SELECT 3 * Y AS Z ,2 * X AS Y ,1 AS X But SQL Server 2005 requires something like this: SELECT X ,Y ,3 * Y AS Z FROM ( SELECT X ,2 * X AS Y FROM ( SELECT 1 AS X ) AS X ) AS Y 回答1: No, it's not valid ANSI. ANSI assumes that all SELECT clause items are evaluated at once. And I'd've written it in SQL 2005 as: SELECT * FROM (SELECT 1 AS X) X CROSS APPLY (SELECT