multiple Case statement in a query - teradata

Can you please tell me why this statement is wrong, it is giving me an error that it was expecting something else instead of the case phrase
SELECT ACT.Employee_Id,
case when max( case when not ACT.ActivityTime_Date like '2019-09-%' and ACT.Activity_Time_Date not like '09/%/2019' and
not (ACT.Action like 'ACTIVITY_create_%' or ACT.Action like 'ACTIVITY_update_%') and
not ACT.Destination_ObjectType in ('announcement', 'attachment'
,'blog','blogpost','community', 'directmessage', 'document', 'event', 'idea', 'message', 'poll', 'project', 'question',
'socialgroup', 'task', 'thread', 'video', 'wallentry')) then 1 else 0
END)=0
then 'Contributor'
from Analytics.ConnectTodayActive ACT;
This is the error
SQL Editor 1: Encountered "case when max ( case when not ( ACT . ActivityTime_Date like \'2019-09-%\' or ACT . ActivityTime_Date like \'09/%/2019\' ) and not ( ACT . Action like not.
Was expecting one of:
"column" ...
"row" ...
"transactiontime" ...
"validtime" ...
"(" ...
"until_changed" ...
"until_closed" ...
<NAME> ...
"sampleid" ...
"interval" ...
"date" ...
"time" ...
"timestamp" ...
"begin" ...
"end" ...
"next" ...
<SINGLE_STRING_LITERAL> ...
<INTEGER_LITERAL> ...
<FLOATING_POINT_LITERAL> ...
":" ...
"?" ...
<NAMED_QUESTIONMARK> ...
"#" ...
"null" ...
"user" ...
"current_role" ...
"current_user" ...
"session" ...
"account" ...
"database" ...
"role" ...
"profile" ...
"zone" ...
"td_host" ...
"td_authid" ...
"new" ...
"current_date" ...

Your query doesn't seem to be formatted correctly:
your outer CASE statement is missing an END
an end parenthesis is out of place
if you want to return employee_id, you need to use a GROUP BY since you're using MAX
You should be able to combine your LIKE conditions too, so something like this:
SELECT
ACT.Employee_Id,
CASE
WHEN MAX (
CASE
WHEN
ACT.ActivityTime_Date NOT LIKE ALL('2019-09-%','09/%/2019') AND
ACT.Action NOT LIKE ALL ('ACTIVITY_create_%','ACTIVITY_update_%') AND
ACT.Destination_ObjectType NOT IN ('announcement','attachment','blog','blogpost','community', 'directmessage', 'document', 'event','idea', 'message', 'poll', 'project', 'question', 'socialgroup', 'task', 'thread', 'video', 'wallentry')
THEN 1
ELSE 0
END
) = 0 THEN 'Contributor'
END AS my_col
FROM Analytics.ConnectTodayActive ACT
GROUP BY ACT.Employee_Id;
SQL Fiddle (Postgres)

Related

how to select element or delete output string

I have a json contents
"objects" : [ {
"uid" : "2272dba0-9ffb-49d4-88b7-0a18f38a3cdc",
"name" : "All_Internet",
},
{
"uid" : "b83fd31c-3cb3-406e-b46b-816a138e8ea1",
"name" : "Public FTP",
} ]
I used :
jq '.objects[] | .name
and got the resule:
"All_Internet"
"Public FTP"
how to select the secondary "name" string, or removing the first "name" "All_Internet" of output
the "All_Internet" is fixed string.
Regards and thanks
It's unclear whether you want an answer like #KiranKandel's, namely:
.objects[].name | select(. != "All_Internet")
or something like:
.objects[1:][].name

Get array with key and value as my select attributes in DQL

I'm doing a query using DQL
$query = $this->createQueryBuilder('i')
->select('(i.sortiesNoSortie)','COUNT(i.participantsNoParticipant)')
->groupBy('i.sortiesNoSortie');
$result = $query->getQuery()->getArrayResult();
but i'm getting this
array:3 [
0 => array:2 [
1 => "76"
2 => "1"
]
1 => array:2 [
1 => "82"
2 => "1"
]
2 => array:2 [
1 => "83"
2 => "1"
]
]
And I want to get that in a simple array with key and value from my select
76 => 1
82 => 1
83 => 1
Is that possible ?
For obvious reasons, the result is a 2D array as it is representing the rows and columns that you would receive when executing the query. From the top of my head, there isn't a doctrine provided function to transform it into your desired format.
You can however use PHP to reformat the array for you.
$result = $this->createQueryBuilder('i')
->select('(i.sortiesNoSortie)','COUNT(i.participantsNoParticipant)')
->groupBy('i.sortiesNoSortie')
->getQuery()
->getArrayResult();
$output = array();
foreach($result as $row) {
$output[$row[0]] = $row[1];
}
While it seems at first that this is not ideal, realistically if doctrine were to implement something to format it the way you want it, it would most likely do something similar and just wrap it in a nice function for you.
Having said that, there may be a nicer way to process the data using PHP other than a foreach loop. Either way, don't shy away completely from reformatting your data in PHP after getting it from doctrine. Obviously this would be a case by case thing but it seems that you are processing small amounts of data, so the extra processing won't be an issue.
Edit
Thanks #Matteo for reminding me. array_column is the function that you can use to replace the foreach loop.
$output = array_column($result, 1, 0);

Why is this push() overwriting instead of adding?

I have a line of code inside a http callable Firebase Function that is supposed to add a new record to a spot in the Firebase Database.
return admin.database().ref('games/' + gameid + '/' + uid + '/logs/').push().set({guess: guess, result: pigs.toString() + 'P ' + bulls.toString() + 'B', score: score});
Here is an example entry in my database.
"games" : {
"39dba69c-f5a8-4e94-9553-d19372d43a92" : {
"1P0mWbp1jYOayaMEyQcnKvX4YB63" : {
"logs" : {
"-LDshYx2wJK6eT47_OVQ" : {
"guess" : "5647",
"result" : "1P 1B",
"score" : 450
}
},
"maxbulls" : 3,
"maxpigs" : 3,
"moves" : 6,
"score" : 450
},
"answer" : "2739dba69c41",
"id" : "39dba69c-f5a8-4e94-9553-d19372d43a92",
"requestor" : "1P0mWbp1jYOayaMEyQcnKvX4YB63",
"status" : "pending",
"turn" : ""
}
}
For some reason the set() is overwriting the record under logs (id of -LDshYx2wJK6eT47_OVQ). I want it to add a new record under logs with the auto-generated key from push(). I have used a very similar method before (with basically no differences) that worked as intended.
I have also tried changing set() to update() but it still overwrote. Is there something wrong with my path or database structure that I am overlooking? Thanks.

ZF2 Doctrine Query With Multiple OR Conditions

I've wrote the following Doctrine query:
$query3 = $this->entityManager
->createQueryBuilder()
->select('t.textDomain , t.translationKey , t.languageIso , t.translationDate')
->from(
'AMDatabase\Entity\TheVerse\TranslationsMasters',
't'
)
->groupBy('t.languageIso')
->orderBy(
't.translationDate',
'DESC'
);
// the values of $key2 array are:
// en-US
// es-MX
// es-PR
foreach( $translation AS $key2=>$value2 ) {
if ( $key2 == 'en-US' ) {
$query3
->orWhere(
$query3->expr()
->like(
't.languageIso',
':languageIso'
)
)
->setParameter(
'languageIso',
$key2
);
}
}
$result3 = $query3->getQuery()
->getArrayResult();
How do I have the query search for all 3 language ISO's at the same time?
With "if ( $key2 == 'en-US' ) {" the query executes and gives me the expected result.
But if I remove "if ( $key2 == 'en-US' ) {" there are no search results.
I thought by using "orWhere" it would keep adding conditions to the query (where en-US will still produce a match).
In haven't ever been able to get orWhere to function the way I think it should. It might be that you need a Where in your opening definition before you can add an orWhere later. You might have to use the nested orX statement like this instead:
$query3->andWhere($query3->expr()->orX(
$query3->expr()->like('t.languageIso', $query3->expr()->literal('en-US')),
$query3->expr()->like('t.languageIso', $query3->expr()->literal('es-MX')),
$query3->expr()->like('t.languageIso', $query3->expr()->literal('es-PR'))
));
You can't develop the statement through a loop. But, since you've only got three criteria, it's easy enough to write out.

Trying to do a Case statement in Powerbuilder

Just trying to do a simple case function in powerbuild.
I have a table
client_notes
A!, B!, C!
case(client_notes when '%A!%' then 'Cash1' else "Cash2")
It compiles OK but when I run it, it says Cash2.
Shouldn't it say Cash1?
What's the context here? Is this in PowerScript or in a datawindow expression, or in the SQL source of the datawindow?
And what version/build of PB are you using?
What it should "say" [sic] all depends on the value of "client_notes" at runtime. It will only return the string 'Cash1' when the value of client_notes is exactly equal to the string '%A!%'.
What set of data are you running this against? Show some sample data.
-Paul Horan-
I have never seen a "LIKE" expression used in the expression painter but it might be possible. I gave it a quick try and wasn't able to. I would agree with what Matt said, except your expression doesn't have the important "LIKE" keyword so it is matching exactly on your string and of course not matching so always getting Cash2.
I've wanted to use > or < in CASE statements before and had problems too, the WHEN seems to be pretty 'dumb' and not able to handle anything other than a value.
This works not as elegant as a case statement though.
if ( client_notes like '%A!%' , 'Cash1',
if ( client_notes like '%B!%' , 'Cash2',
if ( client_notes like '%C!%' , 'Cash3',
'Cash?' ) ) )
What programmer doesn't like a challenge? None of these work but you probably get the thought process I was going through.
case ( ('%' + client_notes + '%') when (like 'A1') then 'Cash1' else 'Cash2' )
case ( '%A1%' when (like 'A1') then 'Cash1' else 'Cash2' )
case ( ( '%bbb%' like 'A1' ) when true then 'Yah' else 'Nah' )
No cigar on CASE...
You could use a global-function, but then again you could write a database function too and it doesn't really solve what you wanted to do. I recall wanting to use like inside a CASE statement more than once and I don't recall getting it to work. I can also recall trying to do something like case variable_a when '4:20' then 'Celebrate' when > '4:20' then 'Too late' else 'Later'
This compiles but do not think it would work due to the comparison variable needing to be different.
case( if ( client_notes like '%' + client_notes + '%', client_notes , 'X')
when 'A1' then 'Cash1'
when 'A2' then 'Cash2'
else 'use nested if instead')
Well, if you wanted to try it as a datawindow computed field, you could use the "brute force" method.
CASE (client_notes
WHEN 'A!' THEN 'Cash'
WHEN 'A!, B!' THEN 'Cash, Check'
WHEN 'A!, B!, C!' THEN 'Cash, Check, Money Order'
WHEN 'A!, B!, C!, D!' THEN 'Cash, Check, Money Order, Card'
WHEN 'B!' Then 'Check'
...
Else '')
In the RetrieveRow event, it might look like this:
string ls_result = ''
string ls_client_notes
If row > 0 then
ls_client_notes = this.getItemString( row, 'client_notes')
If pos( ls_client_notes, 'A!' ) > 0 then ls_result = 'Cash'
If pos( ls_client_notes, 'B!' ) > 0 then
If len( ls_result ) > 0 then
ls_result += ', Check'
Else
ls_result = 'Check'
End if
End if
If pos( ls_client_notes, 'C!' ) > 0 then
If len( ls_result ) > 0 then
ls_result += ', Money Order'
Else
ls_result = 'Money Order'
End if
End if
If pos( ls_client_notes, 'D!' ) > 0 then
If len( ls_result ) > 0 then
ls_result += ', Card'
Else
ls_result = 'Card'
End if
End if
this.setItem( row, 'client_notes', ls_result ) // if you want it back in the same column
End if
-Paul-

Resources