How to normalise inconsistent category labels in teradata - sql

Table has text labels for category identification
The spelling has changed over time
I want to normalise the text labels when I count the records in each category
So for example I have The category labels:
'Ready to go' and 'Readytogo'
But I also have another text value abc that I want to replace with Abcd
The rest Inwant to keep in my Groupby and Count
How can I count these in the new group names in Teradata?
At the moment I'm using case statements for the ones I'm okay with, then using OREPLACE to switch one of the values but how do I nest it so that I can OREPLACE 2 or more values with 2 or more new ones? Is OREPLACE the best function to use here?
Thanks

Apologies I have managed to solve it using multiple WHEN, THEN clauses as part of 1 case statement
This changes the text value ax to bn along with Fx to BM for example, as required
The OREPLACE function was not recognised in the release of Teradata that I am using
I will specify question better next time
Thanks

Related

Forward fill in spark SQL based on column value condition

Please can someone help me how to forward fill values in a case statement based on another column value in SPARK SQL.
I am basically trying to detect outliers in the SQL dataset and so far how I have identified these outliers is identifying standard deviation of a value far from the mean of the dataset.
Now the problem statement is wherever these outliers fall, I have to fill the value in a new column the value which was last valid/authentic.
For example: after 1 in the first column, I want to append 556 in third column and for 3 in the first column, I want to append 561 in the third column
So far, I have identified the outliers and based on the value, I am guessing I can use lag function and go back 1 row. But I also know, this is not a good approach. For example, I get 10 outliers in a sequence, I will have to write 10 CASE statement for that.
Please if someone have any better/efficient approach, please help.

Qlikview conditionally hide expression in pivot table

Struggling with a way to hide an expression on certain rows in my pivot table below. Basically, I want to be able to hide my expression 'Cumulative' when the FINANCIAL_PLAN_TYPE is not equal to 'OB_VARIANCE_TO_T1'. I figured out a way to change the text format on the lines with other FINANCIAL_PLAN_TYPE so that it appears hidden (what is showing in the picture below), but I'd really like the whole row to go away in these cases.
Additionally, I do not want to see a Total on the Cumulative lines if possible, but I do want to keep them on the Sum(AMOUNT) lines.
My expression for Cumulative is:
sum(aggr(Rangesum(above(sum({<YEAR={"$(vYear_Current)"},FINANCIAL_PLAN_TYPE={"OB_VARIANCE_TO_T1"}>}AMOUNT),0,MONTH_NUM)), FINANCIAL_PLAN_TYPE, MONTH_NUM))
I tried to use the function Only, as well as, I tried conditionally enabling/disabling the expression but I can't seem to get it to work. Any ideas would be great. Thanks!
FINAL SOLUTION:
1) Load Inline Table
Load * Inline
[MyDim
Cumulative
Vals
];
2) Created calculated dimension to combine the two desired dimensions into one and show blank ('') in cases that you don't want to show (this leaves one blank line on the pivot but there is probably a way to hide that too):
=If(MYDim='Cumulative' and FINANCIAL_PLAN_TYPE='OB_VARIANCE_TO_T1','Orig Budg Cumulative Variance',if(MYDim='Cumulative' and FINANCIAL_PLAN_TYPE='LE_VARIANCE_TO_T1','LE Cumulative Variance',if(MYDim <> 'Cumulative',FINANCIAL_PLAN_TYPE,'')))
3) Create a new expression that does one calculation in the case of your made up dimension = Value A and something else in case your calculated dimension = Value B
If(MYDim='Cumulative' and (FINANCIAL_PLAN_TYPE='OB_VARIANCE_TO_T1' OR FINANCIAL_PLAN_TYPE='LE_VARIANCE_TO_T1'),If(ColumnNo()=0,'',sum(aggr(Rangesum(above(sum({<YEAR={"$(vYear_Current)"}, DEPARTMENT={"20820"}, ACCT_TYPE={"Capital"}>}AMOUNT),0,MONTH_NUM)),
FINANCIAL_PLAN_TYPE, MONTH_NUM))),IF(MYDim='Vals', SUM({<ACCT_TYPE={"Capital"},DEPARTMENT={"20820"}>}AMOUNT)))
You cannot hide expression on some of the rows - not possible in Qlikview.
The workaround is to create a dummy floating table that holds the second column as a dimension and then use it in your chart instead of the two expressions. Then you will have one expression that says something like that :
If ( dummyField = 'Cummulative' and = 'OB_VARIANCE_TO_T1',
{Use your second expression here} ,
If (dummyField = 'sum(Amount)', {use your original first expression here}))
Regarding the Total, check out this link in the "Tricking the Pivot Table" section

Counting the no. of commas across multiple fields using SQL

I've 3 fields which contain only text. However, i want to add a calculated field which counts the number of commas in each of these 3 fields and displays it separately in the adjacent column. The snippet of SQL i use is shown below. How can i build the calculated field?
SELECT week, client_I, client_II, client_III
FROM quality_control_test;
Please advise!
well, you can "count" the number of a given character in a string, by using this:
length(c) - length(replace(c,',',''))
I'm assume you can figure out how to leverage that for your own query ;)

Checking Range in Comma Separated Values [SQL Server 2008]

I have a table with following structure
ID FirstName LastName CollectedNumbers
1 A B 10,11,15,55
2 C D 101,132,111
I want a boolean value based on CollectedNumber Range. e.g. If CollectedNumbers are between 1 and 100 then True if Over 100 then False. Can anyone Suggest what would be best way to accomplish this. Collected Numbers won't be sorted always.
It so happens that you have a pretty simple way to see if values are 100 or over in the list. If such a value exists, then there are at least three characters between the commas. If the numbers are never more than 999, you could do:
select (case when ','+CollectedNumbers+',' not like '%,[0-9][0-9][0-9]%' then 1
else 0
end) as booleanflag
This happens to work for the break point of 100. It is obviously not a general solution. The best solution would be to use a junction table with one row per id and CollectedNumber.
Just make a function, which will return true/False, in the database which will convert the string values(10,11,15,55) into a table and call that function in the Selection of the Query like this
Select
ID, FirstName, LastName,
dbo.fncCollectedNumbersResult(stringvalue) as Result
from yourTableName
I think the easiest you can do is build a C# function and use the builtin sqlclr to load it as a custom function you can then call.
Inside the C# function, you can then sort your numbers and make simple logic to return your true/false.

Oracle TO_CHAR Format Mask for displaying both integral numbers and floating point numbers

I'm trying to find the correct Oracle format mask to display numbers on an Apex page in a report in a certain way.
Most of the times these numbers are integers but sometimes these numbers can be floating point numbers.
Let's say I have the following three queries:
Query 1
SELECT TO_CHAR(1, '<Format Mask>', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL;
Query 2
SELECT TO_CHAR(0.1, '<Format Mask>', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL;
Query 3
SELECT TO_CHAR(0.01, '<Format Mask>', 'NLS_NUMERIC_CHARACTERS = '',.''') FROM DUAL;
Now I want to use one single format mask which will give me the following results:
Result 1
1
Result 2
0,1
Result 3
0,01
Can anyone provide me with the correct format mask to achieve this?
I've tried a format mask like FM990D999 but it leaves me with a comma trailing the 1 in Query 1.
There are ways to alter your column value in the query while still retaining (some) of the functionality in the report(s). However, having multiple such columns and multiple report you might find there is a lot of overhead for little gain.
Look at this post on the OTN forums: order by date in IR
The issue is much the same: the data in the column represents a date but is actually not a date. This post contains a solution to use in apex < 4.2.
From 4.2 onwards you have a better option called the HTML expression.
Again, linked from OTN: Re: Report formatting/sorting issue
Quoted from linked post, user fac586
Include both variance and abs(variance) in the query:
SELECT
region,
estimate,
actual,
(estimate - actual) AS variance,
ABS(estimate - actual) AS abs_variance,
(CASE
WHEN (estimate - actual)>=0 THEN 'green'
WHEN (estimate - actual)<0 THEN 'red'
ELSE NULL
END) AS variance_color
from
expenses
And the HTML Expression for the "variance" column is:
<span style="color: #VARIANCE_COLOR#; font-weight: bold;">#ABS_VARIANCE#</span>
Hide the #VARIANCE_COLOR# and #ABS_VARIANCE# columns.
#ABS_VARIANCE# is the value shown in the column, but the sort is
performed in the underlying SQL using the original variance value.
This is much like Alex suggested but is a bit more work: formatting in the source, adding an html expression, hiding the other column.
I suppose it depends on how far you want to drive it. Why not just apply the format to the column through its attributes?
Also be aware it is possible to use string substitution syntax in those fields. You could have a couple application items containing format masks, and then reference the correct mask in the format mask field.
Eg:
Application item AI_FORMAT_MASK1 has a value FM9990D00.
In the format mask field you can then use &AI_FORMAT_MASK1.