May 22, 2014

SQL UNION vs UNION ALL

In both cases, columns on both sides of the union should have the same datatype.

UNION removes duplicates.
UNION ALL keeps duplicates.

May 13, 2014

Teradata SQL INDEX function

This is equivalent to ANSI SQL POSITION function.

Example:
SELECT INDEX('Teradata', 'd');    Returns 5  
SELECT INDEX('Teradata', 'er');   Returns 2  
SELECT INDEX('Teradata', 'z');    Returns 0

May 5, 2014

Teradata SQL Syntax error: All expressions in a derived table must have an explicit name.

The below statement threw this error: SQL Syntax error: All expressions in a derived table must have an explicit name.

select v1.* from dim_y v1
join (
select nk_record_id, count(*)  from dim_y
group by nk_record_id
having count(*) > 1
) v2
on v1.nk_record_id = v2.nk_record_id

Corrected version:

select v1.* from dim_y v1
join (
select nk_record_id, count(*) cnt  from dim_y
group by nk_record_id
having count(*) > 1
) v2
on v1.nk_record_id = v2.nk_record_id