Using oracle decode - sql

I have read an article regarding rename data from oracle column. I follow one of the query posted, but when I try on my own. I just only get NULL values. Please help me, what's wrong in my DECODE QUERY.
The original data of status column is 'no answer' and 'answer'
Thanks.
Here's my query
select call_time, decode(status, 'no answer', 'hey', 'answer', 'yes'), channel
FROM APP_ACCOUNT.CC_CALL;
And the output of this is:
call_time decode(status, 'no answer', 'hey', 'answer', 'yes') CHANNEL
10/22/2013 NULL DAHDI/i1/
11/05/2013 NULL DAHDI/i2/
Instead of:
call_time decode(status, 'no answer', 'hey', 'answer', 'yes') CHANNEL
10/22/2013 yes DAHDI/i1/
11/05/2013 hey DAHDI/i2/

When use decode, you should always supply with a default value
decode(value from db, matching 1, alternative value1, matching2, alternative value,...matching n, alternative value n, DEFAULT VALUE)
so that you won't have null returned if all the matches are failed

Related

SQL: Consolidating results

I am a relative beginner in SQL (Learned and forgotten many times) and entirely self taught so please excuse my likely lack of proper terminology. I made a query that pulls items that have been returned, each items' row has a return code. Here is a result sample:
In the final report(Created with Visual Studio), I would like to be able to have a count of returns by return type but I would need to consolidate the 40 or so return codes into 4 or 5 return type groups. So RET_CODE values ) and . are both product quality issues and would count in the "Product Quality" row.
I could use some help with what the best way to accomplish this would be.
Thank You
Andrew
The bad way!
You could do this by creating the grouping within your SQL statement with something like
SELECT
*,
CASE
WHEN RET_CODE IN ('.', ')') THEN 'Quality Error'
WHEN RET_CODE IN ('X', 'Y', 'Z') THEN 'Some Other error'
ELSE [Desc1]
END AS GroupDescription
FROM myTable
The problem with this approach is that you have to keep repeating it every time you want to something similar.
The better option. (but not perfect!)
Assuming you do not already have such a table...
Create a table that contains the grouping. You can use this in the future whenever you need to do this kind of thing.
For example.
CREATE TABLE dbo.MyErrorGroupTable (RET_CODE varchar(10), GroupDescription varchar(50))
INSERT INTO dbo.MyErrorGroupTable VALUES
('.', 'Quality Error'),
(')', 'Quality Error'),
('X', 'Some Other Error'),
('Y', 'Some Other Error'),
('.', 'Some Other Error'),
('P', 'UPS Error'),
('A', 'PAck/Pick Error')
etc....
Then you can simply join to this table and use the GroupDescription in your report
e.g.
SELECT
a.*, b.GroupDescription
FROM myTable a
JOIN MyErrorGroupTable b ON a.RET_CODE = b.RET_CODE
Hope this helps...
You're looking for the GROUP BY clause.

How do I select a certain word with its result in a sentence with in a column SQL?

I am new to SQL and I am trying to learn on the job. I need to filter out the confidence level for responses in a table. The column doesn't not only include the confidence it includes the whole answer. I only need for the answer where it says type:2 I in fact just need the number.
I couldn't past a table here so one column is question the second is volunteer the third is answer in Answer there is whole jumble of words and markers I only need where it says type. In fact i only need the number that follows type /"type/":2
here is what the line in answer looks like what I need is in bold
{
"decision": "default_unused",
"rateorskip": "rate",
"decision_extra": "{ \"segments\": [{\"start\":0,\"end\":9,\"text\":\"the **\"type\":2**,\ series)\",\"commerceEntityExtraInfo\":[]}]}",
"skip": "valid"
}
This is what I wrote:
SELECT
question,
volunteer,
answer
(CASE WHEN answer "type/"= 2 THEN "HIGH",
WHEN answer "type/"= 1 THEN "MEDIUM",
WHEN answer "type/"= 0 THEN "LOW"
ELSE "SKIP/NO TARGET"
END) AS 'volunteer confidence'
FROM blanla
It gives me this error line 5:36: mismatched input '"type/"'. Expecting: '%', '*', '+', '-', '.', '/', 'AT', '[', '||',
I also tried:
SELECT
question,
volunteer
IF EXISTS(
SELECT
answer CASE
WHEN answer 'type' = 0 THEN "High"
WHEN answer 'type' = 2 THEN "Medium"
WHEN answer 'type' = 3 THEN "Low"
ELSE "Skipped/No Target"
END
) AS VolunteerConfidence
FROM blanla

How to write a Kusto query to find two consecutive rows that have the same value in a field

I need to write a Kusto query for Azure Log Analysis that finds consecutive events that have the same value in a field (same error code). We basically need to find if the requests fail twice in a row.
The case where a request fails, one succeeds and one fails is not to be returned.
Assuming you have a table with Id, Datetime, and a ErrorCode, you can utilize prev() function to achieve this:
https://learn.microsoft.com/en-us/azure/kusto/query/prevfunction
datatable(Id:string, Datetime:datetime, ErrorCode:string)
[
'1', datetime(2018-10-16 00:00), 'Error 1',
'1', datetime(2018-10-16 00:01), 'Error 1',
'2', datetime(2018-10-16 00:02), 'Error 1',
'2', datetime(2018-10-16 00:03), 'Error 2',
]
| order by Id, Datetime asc
| extend prevErrorCode = prev(ErrorCode), prevId=prev(Id)
| where prevErrorCode==ErrorCode and prevId == Id

Decode function in oracle for null values

I am working on a java code in which i am comparing the user input values with database values.
I am writing the code to display the alert message to the users on selection of particular division from drop down menu. But there are some alert message which I want to display to all users of every division.
I have the table columns as sr_n0, alert_desc, div_code, alert_flag.
Initially I am checking the div_code = 'division code' and alert_flag = 'y' to display division specific alert message.
But now I want to know how the decode function will work if div_code = null and alert_flag = 'y'.
I have tried this SQL query :
SELECT DECODE(DIV_CODE,'61','Division A',
'62','Division B',
'ALL')
FROM ALERTS WHERE ALERT_FLAG='Y';
If there is no match, then the result will be 'ALL'. I would strongly encourage you to use the ANSI-standard CASE statement, instead of the Oracle-specific DECODE() function:
SELECT (CASE DIV_CODE
WHEN '61' THEN 'Division A'
WHEN '62' THEN 'Division B'
ELSE 'ALL'
END)
FROM ALERTS
WHERE ALERT_FLAG = 'Y';
I solved this issue by writing simple SQL command and it worked for me.
SELECT ALERT_DESC FROM ALERTS A
WHERE
A.DIV_CODE IS NULL
AND A.ALERT_FLAG='Y' ;

SQL server query on json string for stats

I have this SQL Server database that holds contest participations. In the Participation table, I have various fields and a special one called ParticipationDetails. It's a varchar(MAX). This field is used to throw in all contest specific data in json format. Example rows:
Id,ParticipationDetails
1,"{'Phone evening': '6546546541', 'Store': 'StoreABC', 'Math': '2', 'Age': '01/01/1951'}"
2,"{'Phone evening': '6546546542', 'Store': 'StoreABC', 'Math': '2', 'Age': '01/01/1952'}"
3,"{'Phone evening': '6546546543', 'Store': 'StoreXYZ', 'Math': '2', 'Age': '01/01/1953'}"
4,"{'Phone evening': '6546546544', 'Store': 'StoreABC', 'Math': '3', 'Age': '01/01/1954'}"
I'm trying to get a a query runing, that will yield this result:
Store, Count
StoreABC, 3
StoreXYZ, 1
I used to run this query:
SELECT TOP (20) ParticipationDetails, COUNT(*) Count FROM Participation GROUP BY ParticipationDetails ORDER BY Count DESC
This works as long as I want unique ParticipationDetails. How can I change this to "sub-query" into my json strings. I've gotten to this query, but I'm kind of stuck here:
SELECT 'StoreABC' Store, Count(*) Count FROM Participation WHERE ParticipationDetails LIKE '%StoreABC%'
This query gets me the results I want for a specific store, but I want the store value to be "anything that was put in there".
Thanks for the help!
first of all, I suggest to avoid any json management with t-sql, since is not natively supported. If you have an application layer, let it to manage those kind of formatted data (i.e. .net framework and non MS frameworks have json serializers available).
However, you can convert your json strings using the function described in this link.
You can also write your own query which works with strings. Something like the following one:
SELECT
T.Store,
COUNT(*) AS [Count]
FROM
(
SELECT
STUFF(
STUFF(ParticipationDetails, 1, CHARINDEX('"Store"', ParticipationDetails) + 9, ''),
CHARINDEX('"Math"',
STUFF(ParticipationDetails, 1, CHARINDEX('"Store"', ParticipationDetails) + 9, '')) - 3, LEN(STUFF(ParticipationDetails, 1, CHARINDEX('"Store"', ParticipationDetails) + 9, '')), '')
AS Store
FROM
Participation
) AS T
GROUP BY
T.Store