teradata

What is ROWS UNBOUNDED PRECEDING used for in Teradata?

微笑、不失礼 提交于 2019-11-28 16:42:14
I am just starting on Teradata and I have come across an Ordered Analytical Function called "Rows unbounded preceding" in Teradata. I tried several sites to learn about the function but all of them uses a complicated example explaining the same. Could you please provide me with a naive example so that I can get the basics clear. It's the "frame" or "range" clause of window functions, which are part of the SQL standard and implemented in many databases, including Teradata. A simple example would be to calculate the average amount in a frame of three days. I'm using PostgreSQL syntax for the

Can we delete duplicate records from a table in teradata without using intermediate table

半世苍凉 提交于 2019-11-28 14:27:38
Can we delete duplicate records from a multiset table in teradata without using intermediate table. Suppose we have 2 rows with values 1, 2, 3 and 1, 2, 3 in my multiset table then after delete i should have only one row i.e. 1, 2, 3. You can't unless the ROWID usage has been enabled on your system (and probablity is quite low). You can easily test it by trying to explain a SELECT ROWID FROM table; Otherwise there are two possible ways. Low number of duplicates: create a new table as result of SELECT all columns FROM table GROUP BY all columns HAVING COUNT(*) > 1; DELETE FROM tab WHERE EXISTS

How to execute dynamic SQL in Teradata

夙愿已清 提交于 2019-11-28 14:08:36
Is there any way to submit dynamically generated SQL to Teradata? I've written a query that will create the code to denormalize a table. Right now, I am pulling the code down to my client (SAS) and resubmitting it in a second step. I am not familiar with either Teradata macros or procedures; would something like that work? To illustrate, I have a table defined like this: create multiset table MYTABLE ( RECID integer generated always as identity ( start with 1 increment by 1 minvalue -2147483647 maxvalue 2147483647 no cycle ) , SNAP_DATE date format 'YYYY/MM/DD' , EMAIL_DATE date format 'YYYY

Time difference in hours and seconds over a partition window in Teradata (Sessionizing Records)

青春壹個敷衍的年華 提交于 2019-11-28 12:11:36
问题 Given a table like this: cust_id time 123 2015-01-01 12:15:05 123 2015-01-01 12:17:06 123 2015-01-02 13:15:08 123 2015-01-02 15:15:10 456 2015-01-01 10:15:05 456 2015-01-01 12:15:07 456 2015-01-01 14:11:10 I would like to calculate the time difference between each preceding record (think lag function) by cust_id . My desired output: cust_id time diff_hours diff_seconds 123 2015-01-01 12:15:05 NULL NULL 123 2015-01-01 12:17:06 0.00 121 123 2015-01-02 13:15:08 1.04 89882 123 2015-01-02 15:15:10

Find which rows have different values for a given column in Teradata SQL

寵の児 提交于 2019-11-28 07:25:34
I am trying to compare two addresses from the same ID to see whether they match. For example: Id Adress Code Address 1 1 123 Main 1 2 123 Main 2 1 456 Wall 2 2 456 Wall 3 1 789 Right 3 2 100 Left I'm just trying to figure out whether the address for each ID matches. So in this case I want to return just ID 3 as having a different address for Address Code 1 and 2. Join the table with itself and give it two different aliases ( A and B in the following example). This allows to compare different rows of the same table. SELECT DISTINCT A.Id FROM Address A INNER JOIN Address B ON A.Id = B.Id AND A.

JOIN versus EXISTS performance

廉价感情. 提交于 2019-11-28 03:39:40
问题 Generally speaking, is there a performance difference between using a JOIN to select rows versus an EXISTS where clause? Searching various Q&A web sites suggests that a join is more efficient, but I recall learning a long time ago that EXISTS was better in Teradata. I do see other SO answers, like this and this, but my question is specific to Teradata. For example, consider these two queries, which return identical results: select svc.ltv_scr, count(*) as freq from MY_BASE_TABLE svc join MY

Retrieving column and other metadata information in Teradata

那年仲夏 提交于 2019-11-28 03:23:48
问题 I have a half dozen views in SQL Server that I need to replicate in Teradata, but I haven't been able to find the TD equivalent of the SQL metadata tables. I'd like to replicate the following functionality (which I assume is fairly self-explainatory): select table_name, column_id ordinal_position, column_name, data_type, char_length char_max_length, data_precision numeric_precision, data_scale numeric_scale from user_tab_columns select name as FUNCTION_NAME from sys.objects where type_desc=

Teradata MERGE yielding no results when executed through SQLAlchemy

北城余情 提交于 2019-11-28 01:41:00
I'm attempting to use python with sqlalchemy to download some data, create a temporary staging table on a Teradata Server, then MERGEing that table into another table which I've created to permanently store this data. I'm using sql = slqalchemy.text(merge) and td_engine.execute(sql) where merge is a string similar to the below: MERGE INTO perm_table as p USING temp_table as t ON p.Id = t.Id WHEN MATCHED THEN UPDATE SET col1 = t.col1, col2 = t.col2, ... col50 = t.col50 WHEN NOT MATCHED THEN INSERT (col1, col2, ... col50) VALUES (t.col1, t.col2, ... t.col50) The script runs all the way to the

Compare 3 Consecutive rows in a table

半腔热情 提交于 2019-11-27 23:19:12
Hi I have an interesting problem. I Have an Employee Table AS Follows CREATE TABLE EMPLOYEE( EMPLOYEE_ID INTEGER, SALARY DECIMAL(18,2), PAY_PERIOD DATE) Now the tables have employees some of whom get paid monthly,some weekly, some biweekly and some daily. What we want is to find an Indicator saying 'Y' if the salary of three consecutive Pay Periods is equal. Lets take the following example. Employee Pay_Period Salary 1 01/01/2012 $500 1 08/01/2012 $200 1 15/01/2012 $200 1 22/01/2012 $200 1 29/01/2012 $700 In this case the indicator should be Yes because 3 consecutive pay periods have a salary

Teradata equivalent for lead and lag function of oracle

 ̄綄美尐妖づ 提交于 2019-11-27 23:13:40
I have been working ot see the equivalent function for Oracle lead and lag function. The oracle lead would look like LEAD(col1.date,1,ADD_MONTHS(col1.DATE,12)) OVER(Partition By tab.a,tab.b,tab.c Order By tab.a)-1 END_DATE LAG(col1.DATE + 7,1,col1.DATE-1) OVER(partition by tab.a,tab.b Order By tab.b) LAG_DATE Any better idea I believe you can take the following SQL as a basis and modify it to meet your needs: SELECT CALENDAR_DATE , MAX(CALENDAR_DATE) OVER(PARTITION BY 1 ORDER BY CALENDAR_DATE ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS Lag_ --Yesterday , MIN(CALENDAR_DATE) OVER(PARTITION BY 1