Teradata : DBC Query to find Number of Rows processed/affected By username - teradata

I'm trying to get the data row count affected/processed by a query in a BTEQ for the corresponding username in Teradata.
I tried DBC.QryLog but it didn't gave me the expected result.
For Ex:
The Log file looks like below.
bteq << EOF
.logon DBC/UserName,Password;
*** Logon successfully completed.
*** Teradata Database Release is 17.05.05.12
*** Teradata Database Version is 17.05.05.12
*** Transaction Semantics are BTET.
*** Session Character Set Name is 'ASCII'.
*** Total elapsed time was 1 second.
Database Default;
*** New default database accepted.
*** Total elapsed time was 1 second.
Insert into Table1
(
Col1,
Col2,
Col3
)
Select
Col1,
Col2,
Col3
From Table0;
*** Insert completed. 73695 rows added.
*** Total elapsed time was 17 seconds.
Update Table1
From Table0 T0
Set
Col4 = coalesce(T0.Col1,T0.Col2);
*** Update completed. 73484 rows changed.
*** Total elapsed time was 2 seconds.
.LOGOFF
*** You are now logged off from the DBC.
.EXIT 0
*** Exiting BTEQ...
*** RC (return code) = 0
Expected Answer:
Username : UserName
Insert : 73695
Update : 73484

Related

how to use case when to create a new column in table B to show if data from table A also in table B

When I try following code it returns attached error code
select acct_key,
(case when acct_key in (
select acct_key
from table_A
)
then 1 else 0 end) as acct_indicator
from table_B;
Executed as Single statement. Failed [804 : 08S01] Socket communication failure for Packet transmit Wed May 25 12:01:18 CDT 2022 socket orig=VTDSSBP.rw.discoverfinancial.com local=/10.2.181.125:61593 remote=VTDSSBPcop2.rw.discoverfinancial.com/170.217.201.43:1025 keepalive=false nodelay=false receive=65536 send=64512 linger=10 traffic=0 concurrent=3 contimeout=10000 conwait=1000 connecttime=38 connecttotaltime=38 connectattempts=1 connectfailures=0 reconnectattempts=0 recoverable=false redrive=false failurecache={} cid=1221d607 sess=106395528 java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(Unknown Source) at java.net.SocketOutputStream.write(Unknown Source) at ...
attempt 1:
select a.acct_key,
case when exists (
select acct_key
from table b
where a.acct_key = b.acct_key
)
then 1
else 0
end as acct_indicator
from table a;
failed as
it returns Illegal expression in When Clause of case expression
attempt 2:
select a.acct_key,
case when b.acct_key is not null
then 'yes'
else 'no'
end as acct_ind
from table a
left join table b
on a.acct_key = b.acct_key;
succeed

Errors in triggers

This is my trigger:
CREATE OR REPLACE TRIGGER trg_CheckStaffID
BEFORE INSERT ON ASSIGN
FOR EACH ROW
BEGIN
DECLARE id integer := 0;
SET id := (select count(*) from (select staffid from staff where staffid ='T2');
IF (id=0) THEN
RAISE_APPLICATION_ERROR(-20000,'Please Enter A Valid Staff ID');
END IF;
END;
/
And this is the error message I get:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
( - + case mod new not null
continue avg count current exists max min prior sql stddev
sum variance execute forall merge time timestamp interval
date
pipe
PLS-00103: Encountered the symbol "IF"
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
Invalid syntax on different places; DECLARE section should be before BEGIN. There's no SET command in PL/SQL.
Here's code that compiles; whether it does what you meant, can't tell. (I'm creating dummy tables, just to make sure that trigger creation wouldn't fail).
SQL> create table assign (id number);
Table created.
SQL> create table staff (staffid varchar2(2));
Table created.
SQL> create or replace trigger trg_checkstaffid
2 before insert on assign
3 for each row
4 declare
5 id integer := 0;
6 begin
7 select count(*)
8 into id
9 from (select staffid
10 from staff
11 where staffid ='T2');
12
13 if id = 0 then
14 raise_application_error(-20000, 'Please Enter A Valid Staff ID');
15 end if;
16 end;
17 /
Trigger created.
SQL>

peculiar teradata clause, Column_Name >= ''

I have a statament wrote for teradata by someone who don't work here anymore, so i can't ask him directly.
In this statament, the last clause in the where is : ... and Column_Name >= '' .I have no clue about what should that clause do.
If it matter Column_name is a [decimal](7,0) NULL.
Can someone explain to me which case are accepted and which are refused by that clause ?
to me it seams that should allow trough everything since everything is major or equal to null
When you compare a string to a numeric value the string is converted to a FLOAT, in your case the empty string '' is treated as 0, so this is just a stupid way to check for Column_Name >= 0 and filters negative values and NULL. You never know if this was actually the intention of the guy who wrote it :)
When >='' clause is applied on a character column then it will restrict the null records.
If the table has 2 columns one Region_cd & rank_nr and the data in the table is as below:-
select * from databasename.tablename;
*** Query completed. 5 rows found. Two column returned.
*** Total elapsed time was 1 second.
Region | rank_nr
---------- -----------
emea | 1
amr | 2
apac | 3
? | 4
| 5
? is represents NULL and space in region_cd column for rank=5 is not visible
If we query in this table with where clause as Region>='' then it will result below 4 rows:-
select * from databasename.tablename where Region_cd >='' ;
*** Query completed. 4 rows found. 2 columns returned.
*** Total elapsed time was 1 second.
col1 | rank_nr
---------- -----------
emea | 1
apac | 3
amr | 2
| 5

HiveQL, Hive SQL select date range

It seems simple in SQL but I'm having troubles using HiveQL with date range.
I have a dataset like this:
hive> describe logs;
id string,
ts string,
app_id int
hive> select * from logs limit 5;
1389 2014-10-05 13:57:01 12
1656 2014-10-06 03:57:59 15
1746 2014-10-06 10:58:25 19
1389 2014-10-09 08:57:01 12
1656 2014-10-10 01:57:59 15
My goal is to get the distinct id for the last 3 days. The best thing is to read the current system time and get the unique id of last 3 days, but not sure where I need to put "unix_timestamp()". Considered that the log is recorded realtime and there's today's date in ts, I tried to use this query (first approach)
hive > SELECT distinct id FROM logs HAVING to_date(ts) > date_sub(max(ts), 3) and to_date(ts) < max(ts);
FAILED: SemanticException [Error 10025]: Line 1:45 Expression not in GROUP BY key 'ts'
If I add group by 'ts' like below, it spits up this error:
hive> SELECT distinct ext FROM pas_api_logs group by ts HAVING to_date(ts) > date_sub(max(ts), 7) and to_date(ts) < max(ts);
FAILED: SemanticException 1:47 SELECT DISTINCT and GROUP BY can not be in the same query. Error encountered near token 'ts'
After the numerous try, the last approach made was this, studied after [similar topic][1].
Select distinct id from (SELECT * FROM logs JOIN logs ON (max(logs.ts) = to_date(logs.ts))
UNION ALL
SELECT * FROM logs JOIN logs ON (to_date(logs.ts) = date_sub(max(logs.ts), 1))
UNION ALL
SELECT * FROM logs JOIN logs ON (to_date(logs.ts) = date_sub(max(logs.ts), 2)));
Apparently this doesn't work either. Can someone shed some lights on this?
The required result can be obtained by using this statement:
select distinct id from logs where DATEDIFF(from_unixtime(unix_timestamp()),ts) <= 3;
Hope it helps !

How to filter teradata help table

I'd like to create a table out of the dataset generated by teradata's "help table" function so i can add some more information about the table, and be able to filter the rows by conditions. the table has 400+ columns, so this would be very convenient for management. I'd like to be able to do something similar to creating a table as select, but it doesn't work with the help table syntax. short of exporting the data to excel, then manually creating the table schema and importing the table back in, does anyone know how to convert the output of a help table query into a table in teradata?
The output from the HELP TABLE command comes from Data Dictionary.
If I understand correctly, you want to create a new table with the following output.
help table t1;
*** Help information returned. 4 rows.
*** Total elapsed time was 1 second.
Column Name Type Comment
------------------------------ ---- --------
a1 I ?
b1 CF ?
c1 D ?
d1 DA ?
You can get all of those three columns (or even more) from the table DBC.TVFields.
help table dbc.tvfields;
help table dbc.tvfields;
*** Help information returned. 37 rows.
*** Total elapsed time was 1 second.
Column Name Type Comment
------------------------------ ---- ----------------
TableId BF ?
FieldName CV ?
FieldId I2 ?
Nullable CF ?
FieldType CF ?
MaxLength I ?
DefaultValue CV ?
DefaultValueI BV ?
TotalDigits I2 ?
ImpliedPoint I2 ?
FieldFormat CV ?
FieldTitle CV ?
CommentString CV ?
CollationFlag CF ?
UpperCaseFlag CF ?
DatabaseId BF ?
Compressible CF ?
CompressValueList CV ?
FieldStatistics BV ?
ColumnCheck CV ?
CheckCount I2 ?
CreateUID BF ?
CreateTimeStamp TS ?
LastAlterUID BF ?
LastAlterTimeStamp TS ?
LastAccessTimeStamp TS ?
AccessCount I ?
SPParameterType CF ?
CharType I2 ?
LobSequenceNo I2 ?
IdColType CF ?
UDTypeId BF ?
UDTName CV ?
TimeDimension CF ?
VTCheckType CF ?
TTCheckType CF ?
ConstraintId BF ?
But first we need to find out DatabaseId and TableId.
select databaseid
from dbc.dbase
where databasename='db1';
*** Query completed. One row found. One column returned.
*** Total elapsed time was 1 second.
DatabaseId
----------
00000F04
select TVMId
from dbc.tables2
where databaseid='00000F04'xb
and TVMName='t1';
*** Query completed. One row found. One column returned.
*** Total elapsed time was 1 second.
TVMId
------------
0000D8070000
Now you can list all the columns you need and store them correspondingly.
select * from dbc.tvfields
where databaseid='00000F04'xb
and tableid='0000D8070000'xb;

Resources