SQL case statement in Teradata - sql

I'm trying to write a simple IF/ELSE statement in Teradata. As far as I understand you have to use a CASE. What I want to do is write a statement that will check if a column IS NOT NULL and display something. ELSE if it IS NULL, display something else.
Every example I found simply replaces a single value with a hard coded string or int. I'm looking for something more along the lines of this which uses the THEN statement to make another SELECT:
SELECT CARS.VIN_NUM, CARS.DRIVER_NAMES
CASE
WHEN CARS.FUEL IS NOT NULL THEN SELECT CARS.DESTINATIONS
WHEN CARS.FUEL IS NULL THEN SELECT CARS.GAS_STATIONS
END
FROM AUTOMOBILES CARS
WHERE CARS.VIN_NUM IN
('345353',
'354632',
'535231')
ORDER BY CARS.VIN_NUM
The end result should be a table displaying the VIN_NUM, DRIVER_NAMES, DESTINATIONS OR GAS_STATIONS based on the CASE. Is something like this possible or am I going about it the wrong way?

If DESTINATIONS and GAS_STATIONS are columns in the table AUTOMOBILES (aliased CARS) then the following should work:
SELECT CARS.VIN_NUM,
CARS.DRIVER_NAMES,
CASE
WHEN CARS.FUEL IS NOT NULL THEN CARS.DESTINATIONS
ELSE CARS.GAS_STATIONS
END
FROM AUTOMOBILES CARS
WHERE CARS.VIN_NUM IN
('345353',
'354632',
'535231')
ORDER BY CARS.VIN_NUM;

Related

Combining multiple Case statements into single output column

I need to re-categorise a column marketing_channel with 10 unique values into 15 distinct groups by matching certain criteria.
I've done this via case statements but then the output is in 15 new columns.
Is there a more elegant way to re-class the marketing_channel by simply adding 1 extra column like "marketing_sub_channel" that contains all new 15 classes?
Is there better way to do the classification than by creating 15 case statements? Was thinking a with clause, but that would also be quite lengthy
Output looks like this but ultimately just a single added column would be great:
Yes you just have to change the format a bit. Remove the "case" statement at the beginning of each line and just put the "End" at the end of the statement, like so :
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE null
END as marketing_sub_channel
or in your case:
CASE
WHEN medium like ('%affiliate%') or marketing_cannel ='Affiliates' then 'Affiliate'
WHEN campaign like ('%_Display brand_global Progromatic Display%') then 'Dispay'
WHEN campaign like ('%display%') and campaign not like ('progrommatic') then 'Dispay'
....
else null
END as marketing_sub_channel
Also I would like to note that in your case statement since you have '%display%' and '%_Display brand_global Progromatic Display%' that you place the longer more specific one on top so it can trigger if it needs to. If '%display%' is on top then it will always trigger first since it contains a substring of the other one.

How to Sum two columns meeting certain conditions

What I want to do is basically merge the two highlighted code, so that the end result is it using this SUM formula for only the items matching the LIKE criteria (under WHERE) - so that I am still able to pull GameDescriptions that do not include the LIKE criteria. Hope that makes sense... enter image description here
I think you just need to replace that part of the WHERE statement with a case statement in the SUM, like this:
SELECT
SUM(CASE WHEN GameDescription LIKE '5R25L%' THEN NetRevenue
ELSE 0 END) / COUNT(DISTINCT AccountingDate)
AS 'ES Created TheoWPU'
FROM Prime.dbo.PivotData

How to write an expression for two different attributes in the same field in qlikview

Please help me write the script for the following statement in qlikview which I have it in SQL.
SELECT CASE
WHEN Total_A=0 THEN 0
ELSE cast(((Total_B+Total_C)/Total_A) AS decimal (5,2))
END AS ratio
I have Total_A , Total_B and Total_C in the same field called Total_val
The SQL CASE is usually replaceable by the QlikView if().
Try this
if(Total_A=0,0,(Total_B+Total_C)/Total_A) as Ratio
if the A,B,C switch is inside the Val column then it will get a lot more tricky as you will have to aggregate and use nested ifs. But I believe the statement I wrote is equivalent to the SQL you gave us. If my answer doesn't work please give us a few rows of data to look at

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.

Separate column based on condition in same table in SQL

I'm having a result set from a table in which I have to split a specific value and display in another column(user defined) based on a condition.
//Query
SELECT wtcCommentId, wtcTransactionId, wtcTaskId, wtcUserLogin,
wtcCommentDateTime, wtcCommentText
FROM dbo.tblWorkflowTransactionComments (NOLOCK)
WHERE wtcTransactionId = 141248
The output for the above query will be some 10 rows.
In the above query I need to separate the wtcCommentText column if the wtcTaskId for that result is 0. So if the wtcTaskId is 0 the wtcCommenttext value should be displayed in a separate column(transcommenttext) and for others it should be displaying in the same column(wtcCommentText). So that I can make a split between the comments based on the taskId.
How can I achieve this?
I'm not sure it really makes sense, but okay, I'm game for a laugh:
SELECT wtcCommentId, wtcTransactionId, wtcTaskId, wtcUserLogin,
wtcCommentDateTime,
CASE WHEN wtcTaskId!=0 THEN wtcCommentText END as wtcCommentText,
CASE WHEN wtcTaskId=0 THEN wtcCommentText END as transcommenttext
FROM dbo.tblWorkflowTransactionComments (NOLOCK)
WHERE wtcTransactionId = 141248
It seems like the kind of trivial reformatting that would be better done in a front end or report generator though.