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;
Related
I am trying to identify what records exist in table 1 that are not in table 2 (so essentially using NOT IN)
let outliers =
Table 2
| project UniqueEventGuid;
Table 1
|where UniqueEventGuid !in (outliers)
|project UniqueEventGuid
but getting 0 records back even though I know there are orphans in table 1.
Is the !in not the right syntax?
Thanks in advance!
!in operator
"In tabular expressions, the first column of the result set is
selected."
In the following example I intentionally ordered the column such that the query will result in error due to mismatched data types.
In your case, the data types might match, so the query is valid, but the results are wrong.
let t1 = datatable(i:int, x:string)[1,"A", 2,"B", 3,"C" ,4,"D" ,5,"E"];
let t2 = datatable(y:string, i:int)["d",4 ,"e",5 ,"f",6 ,"g",7];
t1
| where i !in (t2)
Relop semantic error: SEM0025: One of the values provided to the
'!in' operator does not match the left side expression type 'int',
consider using explicit cast
Fiddle
If that is indeed the case, you can reorder the columns or project only the relevant one.
Note the use of double brackets.
let t1 = datatable(i:int, x:string)[1,"A", 2,"B", 3,"C" ,4,"D" ,5,"E"];
let t2 = datatable(y:string, i:int)["d",4 ,"e",5 ,"f",6 ,"g",7];
t1
| where i !in ((t2 | project i))
i
x
1
A
2
B
3
C
Fiddle
Another option is to use leftanti join
let t1 = datatable(i:int, x:string)[1,"A", 2,"B", 3,"C" ,4,"D" ,5,"E"];
let t2 = datatable(y:string, i:int)["d",4 ,"e",5 ,"f",6 ,"g",7];
t1
| join kind=leftanti t2 on i
i
x
2
B
3
C
1
A
Fiddle
I have a given SQLIt database (so no chance to do it in a better way):
CREATE TABLE `history` (
`TIMESTAMP` TIMESTAMP,
`DEVICE` `enter code here`varchar(32),
`TYPE` varchar(32),
`EVENT` varchar(512),
`READING` varchar(32),
`VALUE` varchar(32),
`UNIT` varchar(32)
);
In this table I have for example the following data:
DEVICE VALUE
d1 1
d5 500
d2 10
d1 2 <--
d5 501
d1 100 <---
I want to figure out for the device "d1" all timestamps where the difference between the last value and the current value is > 10
I have absolutly no idea how to do this with SQL
thank you
To get the last value for the current timestamp T, you would use a query like this:
SELECT Value
FROM History
WHERE Device = '...'
AND Timestamp < T
ORDER BY Timestamp DESC
LIMIT 1;
You can then use this as a correlated subquery in your query:
SELECT Timestamp
FROM History
WHERE Device = 'd1'
AND ABS((SELECT Value
FROM History AS last
WHERE Device = 'd1' -- or Device = History.Device
AND last.Timestamp < History.Timestamp
ORDER BY Timestamp DESC
) - Timestamp) > 10;
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
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 !
In my Symfony app I have an entity 'Project' which contains two fields: 'createdOn' (type = date) and 'individual'. An individual can occur multiple times in 'Project'.
_created_On_|_individual_id
2012.12.01 | 3
2012.12.24 | 5
2013.01.10 | 9
I'm trying to build a query to count all distinct individuals grouped by 'createdOn' in such a way, that I get results sorted by month. And it must be possible to set a date range for the query.
My query so far:
'SELECT p.createdOn, COUNT (DISTINCT p.individual)
FROM ...\DossierBundle\Entity\Project p
WHERE p.createdOn
BETWEEN :name1
AND :name2'
)->setParameters(array(
'name1' => $startDate,
'name2' => $endDate,
))
This doesn't quite get me the desired result below
_DATE____|_Number_of_Individuals
Dec 2012 | 2
Jan 2013 | 1
But instead I get
__DATE_____|_Number_of_Individuals
2012.12.01 | 1
2012.12.24 | 1
2013.01.10 | 1
Google didn't help me either so any support will be much appreciated.
Flo
You need to extend doctrine with custom dates functions , and be carefull because you cant use group by with functions so you'll have to trick doctrine.
have a look at that :
http://www.doctrine-project.org/blog/doctrine2-custom-dql-udfs.html
here is an exemple of a day native mysql function :
https://github.com/beberlei/DoctrineExtensions/blob/master/lib/DoctrineExtensions/Query/Mysql/Day.php
and read that for the group by issue and work around ( using as ) :
http://www.doctrine-project.org/jira/browse/DDC-1236
i needed to group visits by date ( without the time ) , so i wrote that dql query with a date extension :
select count(v) as visit_count , DATE(v.created_at) as day_created_at from Shorten\Entity\Visit v group by day_created_at
hope it helps.