Vertica : Function return multiple column - sql

I have around 40 queries which run on various tables and at the end of each query is a sub query for calculation of some metrics based on the values from above sub-queries. The final sub-query in each query looks like this -
SELECT dimension,
CASE when(impressions!=0) THEN clicks/impressions*100
ELSE NULL
END ctr,
CASE when(impressions!=0) THEN gross_rev/impressions*1000
ELSE NULL
END cpm,
CASE when(clicks!=0) THEN gross_rev/clicks
ELSE NULL
END cpc,
CASE when(actions!=0) THEN gross_rev/actions
ELSE NULL
END AS cpa,
CASE when(post_click!=0) THEN gross_rev/post_click
ELSE NULL
END AS pc_cpa
I want to reuse these formulas so wanted to write a function. But a vertica function does not return more than one columns.
Any idea on how to re-use these sub queries in a function or any other reusability technique for vertica.

You can do that by creating your own Vertica UDX , the UDX should be of TRANSFORM type , using this type of UDX the output schema is unrelated to the input schema , so basically you can provide list of columns as an input and the output can be different list with different size .
You can see some examples in GitHub https://github.com/vertica/Vertica-Extension-Packages
I hope you will find it useful
Thanks

Related

How to implement nested CASE statements in a SQL Select of a stored procedure?

I have a SELECT statement from a temp table in a stored procedure that selects these two columns:
DECLARE #Mode INT
CASE
WHEN t.Descr = '-- Prior Balance --'
THEN ''
ELSE t.ChgAmount
END AS ChgAmount,
CASE
WHEN t.Descr = '-- Prior Balance --'
THEN ''
ELSE t.PayAmount
END AS PayAmount,
I want to conditionally return those two columns differently depending on the value of #Mode, specifically if it is equal to 7.
I'm getting confused about the levels of nesting that I need and the formatting of doing this.
So far I have tried something like this:
CASE
WHEN #Mode = 7
THEN
CASE
WHEN t.TranDate > #Due
THEN t.ChgAmount
END
END AS CurrentCharges,
CASE
WHEN t.Descr = '-- Prior Balance --'
THEN ''
ELSE t.ChgAmount
END AS ChgAmount,
CASE
WHEN t.Descr = '-- Prior Balance --'
THEN ''
ELSE t.PayAmount
END AS PayAmount,
The above SELECT might work, but it would stil return the extra column. How should I nest the other, original, CASE statement for the ChgAmount?
First, let's clarify in abstract terms that in the following pseudo-code
If X then
Case 1
If Y then
Case 2
Else
Case 3
End If
Else
Case 4
End If
Case 1 is equivalent to "X is true"
Case 2 is equivalent to "X is true and Y is true"
Case 3 is equivalent to "X is true and Y is false"
Case 4 is equivalent to "X is false"
Furthermore, let's clarify that case-when criterias are logically very similar to our pseudo-code presented above of if-then, hence, you can apply composite criteria instead of nesting case-when if you prefer that, but also, you can implement nested case-when criterias, it's a matter of style.
As a result, you will need to formulate the logic you want to apply, by asking yourself the following questions:
do I need the same number of fields in my different cases? (if not, then you will probably need to write different queries in different cases)
what cases do I have for my fields if #Mode is 7?
what cases do I have for my fields if #Mode is not 7?
how can I merge my criterias in the points above into coherent (composite) criterias that would not require nesting?
If you answer these questions as an edit to this question, then we will be able to more properly answer your questions than the general terms I'm using in this answer and we may provide code for you as well. However, if you think this through, then you might also be able to implement this in a not nested way. And, if you are able to understand this as far as to implement it into a not nested way, then you could transform that implementation into a nested implementation as well.

Return Condition/Expression from a Case Statement

I have searched all around and cannot seem to find a solution to this problem I'm having. I have a fairly large case statement (over 100 lines) that works and returns the result I am looking for. An example of the line is below:
case
When (Description like '%job%'
or description like '%job%fail%') then 'Job'
Else 'Not Classified'
End as ATC
I have a case statement that returns the result 'Job' as expected. I would also like to create a separate case statement that returns the criteria that returns the condition that the record met, allowing me to evaluate which criteria are returning the match ( a 'job' vs. 'job failed' comparison). I'm aware that I can duplicate my case statement to output the criteria met, but I would like to repeat this analysis and am looking for a more easily replicable solution (something along the lines of reading the conditions from the above case statement). Any thoughts?
If you're just trying to avoid repeating the logic you can wrap it up in a table expression.
with matches as (
select *,
case when Description like '%job%fail%' then 1 -- most specific first
when Description like '%job%' then 2 -- least specific last
else 0
end as MatchCode
from ...
)
select *,
case when MatchCode > 0 then 'Job' Else 'Not Classified' End as ATC
from matches

How can I compare two columns for similarity in SQL Server?

I have one column that called 'message' and includes several data such as fund_no, detail, keywords. This column is in table called 'trackemails'.
I have another table, called 'sendemails' that has a column called 'Fund_no'.
I want to retrieve all data from 'trackemail' table that the column 'message' contains characters same as 'Fund_no' in 'trackemails' Table.
I think If I want to check the equality, I would write this code:
select
case when t.message=ts.fund_no then 1 else 0 end
from trackemails t, sendemails s
But, I do want something like below code:
select
case when t.message LIKE ts.fund_no then 1 else 0 end
from trackemails t, sendemails s
I would be appreciate any advice to how to do this:
SELECT *
FROM trackemails tr
INNER JOIN sendemail se on tr.Message like '%' + se.Fund_No + '%'
Dear Check SQL CHARINDEX() Function. This function finds a string in another string and returns int for the position they match. Like
SELECT CHARINDEX('ha','Elham')
-- Returns: 3
And as you need:
SELECT *
,(SELECT *
FROM sendemail
WHERE CHARINDEX(trackemails.Message,sendemail.Fund_No)>0 )
FROM trackemails
For more information, If you want something much better for greater purposes, you can use Fuzzy Lookup Component in SSDT SSIS. This Component gives you a new column in the output which shows the Percentages of similarity of two values in two columns.

Trying to combine three individual SELECT statements into one main SELECT statement

Here's the problem. There are 3 fields in my table which may contain data with an extra quotation appended at the end. So, I'm trying to run a select statement that will remove this extra character from these fields, IF that extra character exists. I can write 3 individual queries just fine, and they work, but I'm trying to combine them all into one query. Here's what I have so far, and I know it's probably incorrect the way I have it:
Here's the result set that comes back. Notice that all three columns are NULL. They shouldn't be:
Here's an individual query that works for one field at a time:
Can you tell me what I'm doing wrong?
You can remove the WHEREs, as well as the non-correlated subqueries in the select list, and probably simplify it to this...
SELECT
AID
, EID
, STOREID
, [Language]
, 'BrandLabel' = CASE WHEN BrandLabel LIKE '%"'
THEN LEFT(BrandLabel, LEN(BrandLabel) -1)
ELSE BrandLabel
END
, 'Terms' = CASE WHEN Terms LIKE '%"'
THEN LEFT(Terms, LEN(Terms) -1)
ELSE Terms
END
, 'TrackOrderLbl' = CASE WHEN TrackOrderLbl LIKE '%"'
THEN LEFT(TrackOrderLbl, LEN(TrackOrderLbl) -1)
ELSE TrackOrderLbl
END
FROM parallel_Purchase_Email_Content_OMS WITH (NOLOCK)

Need to find value in one column and then return value from another column

Here is my problem. I have a Microsoft SQL server database table with a bunch of columns in it. (I cannot change the data structure of this table!)
21 of the columns in this table stand for 'Actual' sizes
21 of the columns in this table stand for 'Relative' sizes
The Actual size columns correspond through Relative size columns via column number. (ex : ActualColumn1 corresponds to RelativeColumn1, ActualColumn2 corresponds to RelativeColumn2, ActualColumnX to RelativeColumnX up to 21)
Based on the inputted 'Actual' value I need to return the related 'Relative' value.
I have managed to do part of what I need with the following case statement, but case statements have a maximum of 10 conditions and therefore won't be able to cover all 21 columns in my table. What would be the best way to approach this issue? My goal would be to put this into a select statement so that I could select the Relative Size and return it in my query.
Example of what I did with the case statement:
SELECT
T.AValue
CASE WHEN (T.ActualColumn1 = T.AValue) then T.RelativeColumn1 ELSE
CASE WHEN (T.ActualColumn2 = T.AValue) THEN T.RelativeColumn2 ELSE
CASE WHEN (T.ActualColumn3 = T.AValue) THEN T.RelativeColumn3 ELSE
CASE WHEN (T.ActualColumn4 = T.AValue) THEN T.RelativeColumn4 ELSE
NULL
END
END
END
END AS RValue
FROM T
Thank you for your help!
If you change your case statement to a searched case you can have more then 10 when clauses.
select T.AValue,
case T.AValue
when T.ActualColumn1 then T.RelativeColumn1
when T.ActualColumn2 then T.RelativeColumn2
when T.ActualColumn3 then T.RelativeColumn3
when T.ActualColumn4 then T.RelativeColumn4
end as RValue
from T