DB2 Generate GUID/UUID - db2

Is it possible to generate to GUID/UUID string in DB2 whose contents need to be absolutely unique in the universe? similar to uniqueidentifier data type in SQL server. In DB2 we got GENERATE_UNIQUE function, that ensures uniqueness only across multiple execution of the same function. I think i can try Java Routines in DB2. In worst possible case I can generate GUID/UUID from my Java app and send it across to DB2.. are there any other alternatives to achieve the same within DB2 without using external components/routines. Please suggest.
Thank you..

Try This...
SELECT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),8) CONCAT '-' CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),4) CONCAT '-' CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),4) CONCAT '-' CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000 )), 'abcdef123456789', '1234567890' ),4) CONCAT '-' CONCAT LEFT(TRANSLATE ( CHAR(BIGINT(RAND() * 10000000000000 )), 'abcdef123456789', '1234567890' ),12) FROM YOURTABLE

You can't do it without external function in Db2.
A Java UDF is the simplest way for this.

Related

Why Does string_agg Not Make Changes In This Context

I was starting down the road of testing within group(), and I came across this unexpected behavior of string_agg (or maybe substring). In the following code, mType is a field containing things like 'HMA - etc.etc.etc.' or 'ACP - etc.etc.etc.' In the following context, the 2nd selected field fails to change the delimiter from a ',' to a '-'
select string_agg( substring( mType,1,3 ), ',' ) mType
,string_agg( substring( mType,1,3), '-' ) mType2
from ourDB..mTypeTable
where ref = '3944900'
Returns:
If I change the substring from 1,3 to 1,4, then things work.
select string_agg( substring( mType,1,3 ), ',' ) mType
,string_agg( substring( mType,1,4), '-' ) mType2
from ourDB..mTypeTable
where ref = '3944900'
Returns:
Why does TSQL fail to change the delimiter in the first context? Is this some kind of optimization moment or something where TSQL is reusing the last substring because it's pulling from the same field in the same table in the same select?

Group by with distinct count in DAX PowerBI

how can I count the number of different companies for each user / activity_desc / activity combination?
I tried with that dax but it doesn't work; THNAKS
Try this
Column =
CALCULATE (
DISTINCTCOUNT ( 'Table'[company] ),
ALLEXCEPT ( 'Table', 'Table'[user], 'Table'[activity], 'Table'[activity_desc] )
)

Data factory Postgres query date time error

I have a postgressql query. I tried it in db query and it seems to work fine. But in data bricks its not working as expected.
I have to use where clause. Where I am extracting date from timestamp and comparing it with passed value (which is in date time format). I am extracting date and checking if its less than the one from timestamp.
Pls check in the WHERE clause.
select DATE(to_timestamp(time,'YYYY-MM-DD %H24:%m:%s' )) as modifiedtime, *
FROM testg
WHERE DATE(to_timestamp(time,'YYYY-MM-DD %H24:%m:%s' )) >= DATE(to_timestamp('2020-10-25 00:00:00','YYYY-MM-DD %H24:%m:%s' )) - INTERVAL '7 DAY'
order by time asc
In data factory the query looks like:
#concat('select * FROM streamingData WHERE property_name = ''', item(), '''' ,' AND DATE(to_timestamp(''', activity('Lookup1').output.firstRow.max_timestamp, '''',',''','YYYY-MM-DD %H24:%m:%s', ''' ))' ,' >= ''', 'DATE(to_timestamp(''', pipeline().parameters.enddate, '''',',''','YYYY-MM-DD %H24:%m:%s', ''' ))' )
Data:
time:
"2020-10-25 13:00:22.000000+01"
"2020-10-24 14:00:22.000000+01"
"2020-10-25 12:00:22.000000+01"
"2020-10-26 01:00:22.000000+01"
and I am using trigger().outputs.windowEndTime() -
output is: 2020-10-20 01:00:22
I am comparing both the date values i.e.
If 2020-10-26 >= 2020-10-20
Even after filtering, i get all the data i.e. also the dates from 2019 etc.
What am I missing.
Strangely I tried the comparison using Between (which was not working) and I was still getting all the years even after filtering.
Methods used :
Used between clause - was not working
Used <= and >= - was also not working.
I used later "SYMMETRIC" function. with between - with which the filter worked. I suppose in BETWEEN SYMMETRIC the arguments are automatically swapped and that a nonempty range is always implied.
at the end the query is like:
#concat('select * FROM streamingData WHERE property_name = ''', item(), '''' ,' AND time BETWEEN SYMMETRIC to_timestamp(''', activity('LKP_RMS_ValueStream_Time').output.firstRow.max_timestamp, '''',',''','YYYY-MM-DD', ''' )' ,' AND ', 'to_timestamp(''', pipeline().parameters.enddate, '''',',''','YYYY-MM-DD', ''' )' , ' - ', 'INTERVAL ''', '7 DAY', '''' )
Thanks for the time and help.

WHERE IN with composite keys and wildcards (postgres)

As an extension to this question, is it possible to incorporate wildcards with postgres?
E.g. Something like this (note the wildcard to select all entries where key_part_1 has a value of 'C'):
SELECT *
FROM table_name
WHERE (key_part_1, key_part_2) IN ( ('B',1), ('C',?) );
Is this possible, and if so, what's the syntax?
If key_part_2 can be anything, there is no need to test it.
SELECT *
FROM table_name
WHERE (key_part_1, key_part_2) IN ( ('B',1) )
OR key_part_1 IN('C' )
;
You can use the same column in IN clause(if both columns are not nullable):
SELECT *
FROM table_name
WHERE (key_part_1, key_part_2) IN ( ('B',1), ('C',key_part_2) );
So for second value pair only key_part_1='C' will be used because key_part_2=key_part_2 is always true.
WHERE (key_part_1, key_part_2) IN ( ('B',1), ('C',key_part_2) );
<=>
WHERE (key_part_1='B' AND key_part_2 = 1)
OR (key_part_1='C' AND key_part_2 = key_part_2);
If columns can be nullable you can wrap it with COALESCE(column_name, value) like:
SELECT *
FROM tab
WHERE (key_part_1, COALESCE(key_part_2,-1)) IN ( ('B',1),
('C',COALESCE(key_part_2,-1)) );
SqlFiddleDemo
Note that value used in COALESCE should be distinct for actual data to avoid colisions.

Sum like operation for strings in t-sql

I already have query to concatenate
DECLARE #ids VARCHAR(8000)
SELECT #ids = COALESCE(#ids + ', ', '') + concatenatedid
FROM #HH
but if I have to do it inline how can I do that? Any help please.
SELECT sum(quantity), COALESCE(#ids + ', ', '') + concatenatedid from #HH
Thanks.
Use the XML PATH trick. You may need a CAST
SELECT
SUBSTRING(
(
SELECT
',' + concatenatedid
FROM
#HH
FOR XML PATH ('')
)
, 2, 7999)
Also:
Join characters using SET BASED APPROACH (Sql Server 2005)
Subquery returned more than 1 value