MEDIAN Function Examples | Teradata Vantage - 17.10 - Examples - Advanced SQL Engine - Teradata Database

Teradata Vantageā„¢ - Time Series Tables and Operations

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Release Date
July 2021
Content Type
Programming Reference
Publication ID
B035-1208-171K
Language
English (United States)

Example: Using the MEDIAN Function

/*PTI Table*/
SELECT $TD_TIMECODE_RANGE, $TD_GROUP_BY_TIME, BUOYID, MEDIAN(TEMPERATURE), COUNT(*)
FROM OCEAN_BUOYS
WHERE TD_TIMECODE BETWEEN TIMESTAMP '2014-01-06 08:00:00' AND TIMESTAMP '2014-01-06 10:30:00'
GROUP BY TIME (MINUTES(30) AND BUOYID)
ORDER BY 3,2,1;

/*Non-PTI Table*/
SELECT $TD_TIMECODE_RANGE, $TD_GROUP_BY_TIME, BUOYID, MEDIAN(TEMPERATURE), COUNT(*)
FROM OCEAN_BUOYS_NONPTI
WHERE TIMECODE BETWEEN TIMESTAMP '2014-01-06 08:00:00' AND TIMESTAMP '2014-01-06 10:30:00'
GROUP BY TIME (MINUTES(30) AND BUOYID)
USING TIMECODE(TIMECODE)

The results are the same for both tables:

TIMECODE_RANGE GROUP BY TIME (MINUTES(10)) BUOYID Median (TEMPERATURE) Count(*)
('2014-01-06 08:00:00.000000+00:00', '2014-01-06 08:30:00.000000+00:00') 1 0 5.45000000000000E 001 5
('2014-01-06 09:00:00.000000+00:00', '2014-01-06 09:30:00.000000+00:00') 3 1 7.45000000000000E 001 6
('2014-01-06 10:00:00.000000+00:00', '2014-01-06 10:30:00.000000+00:00') 5 44 5.30000000000000E 001 11

Example: Reporting Groups with an Odd Number of Values Using the HAVING Clause

/*PTI Table*/
SELECT $TD_TIMECODE_RANGE, $TD_GROUP_BY_TIME, BUOYID, MEDIAN(TEMPERATURE), COUNT(*)
FROM OCEAN_BUOYS
WHERE TD_TIMECODE BETWEEN TIMESTAMP '2014-01-06 08:00:00' AND TIMESTAMP '2014-01-06 10:30:00'
GROUP BY TIME (MINUTES(30) AND BUOYID)
HAVING COUNT(*) MOD 2 = 1 AND COUNT(*) > 2
ORDER BY 3,2,1;

/*Non-PTI Table*/
SELECT $TD_TIMECODE_RANGE, $TD_GROUP_BY_TIME, BUOYID, MEDIAN(TEMPERATURE), COUNT(*)
FROM OCEAN_BUOYS_NONPTI
WHERE TIMECODE BETWEEN TIMESTAMP '2014-01-06 08:00:00' AND TIMESTAMP '2014-01-06 10:30:00'
GROUP BY TIME (MINUTES(30) AND BUOYID)
USING TIMECODE(TIMECODE)
HAVING COUNT(*) MOD 2 = 1 AND COUNT(*) > 2
ORDER BY 3,2,1;

The results are the same for both tables:

TIMECODE_RANGE GROUP BY TIME (MINUTES(10)) BUOYID Median (TEMPERATURE) Count(*)
('2014-01-06 08:00:00.000000+00:00', '2014-01-06 08:30:00.000000+00:00') 1 0 5.45000000000000E 001 5
('2014-01-06 10:00:00.000000+00:00', '2014-01-06 10:30:00.000000+00:00') 5 44 5.30000000000000E 001 11