How to extract multiple substring keywords in SQL Server and show results in multiple columns? - sql

I have a table called Usage and there is a column called TEXT.
This TEXT columns holds a string value that looks something like this below.
"TIME EXPENSE ACCRUALS COST DC WITH RATES XX INTEGRATION TIME OD TRAVEL..."
I would like to write a SQL query that would search this column by the selected keywords like TIME or TIME OD or COST, etc., and if the search is true return a check or X that represent that there is that keyword in there or nothing if it doesn't.
For example, if I ran a substring looking for my keywords, my results would like this:
I hope this helps identify what I'm looking for. Any help would be appreciated.
Image of current data fields

How about:
select
section,
name,
case when charindex('TIME', text) > 0 then 'X' else '' end as Time,
case when charindex('EXPENSE', text) > 0 then 'X' else '' end as Expense,
... all other columns here
from usage;

Related

Need to identify same characters between two columns' value in spark sql

I have two columns one is code and other one is category
Code: category
321 3210001
432 4320001
5314 5314001
6310 7480001
Based on the above code value is exactly the prefix of category value.
Now I have to write a spark sql query which should provide how many rows are categorized and non categorized
eg: if code and prefix of category values are matching, then it should be categorized else
it is non categorized.
from the above table, 4th row is non-categorized and rest of them are categorized
I am trying to use like, substring, len, case but I am not able to achieve this result.
Could someone please help me out on this?
thanks,
Raja
assumed those columns are string , if not cast them to str by cast(column as string) in the same query below :
select case when instr(category , code) = 1
then 'Categorized'
else 'Non Categorized' end
, count(*) counts
from df
group by case when instr(category , code) = 1
then 'Categorized'
else 'Non Categorized' end

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.

Convert Xpath to SQL

Previously we were fetching a number of countries from a table. Now the table changed to support multiple languages inside the text cell. The previous SQL statement was:
select b.text, a.iso_num, a.iso2, a.iso3, a.kfz, a.ind_member
from schema.zl_countrycode a, schema.zl_countrycode_lsd b
where a.ind_land = 1 and a.kfz is not NULL and dat_end = to_date('99991231','YYYYMMDD') and a.ZL_COUNTRYCODE_id = b.ZL_COUNTRYCODE_LSD_ID order by text
The list previously outputted each country in a row.
With the addition of the language selection, the list suddenly doubled in size - because it lists every county in two different languages.
This is how the table looks with the GUI
I'm trying to figure out, how to expend the statement to only select each "DE:" value in the text column. After having a quick indirect discussion with the developers, they provided me this XPATH statement:
Parentofparent/Parent/Valuestable[name="ZL_COUNTRYCODE"]/Valueclassification[name="TEXT"]/Value/ValueLng[lng="DE"]
I don't know the first thing about XPATH and no matter what I have tried so far, it failed.
Please help me out if you're knowledgeable in this field. It's probably an Oracle database, as I have experienced a plethora of Oracle error codes in the last few days.
I think this should do the trick:
SELECT b.TEXT
,a.iso_num
,a.iso2
,a.iso3
,a.kfz
,a.ind_member
,a.de
FROM SCHEMA.zl_countrycode a
JOIN SCHEMA.zl_countrycode_lsd b
ON a.ZL_COUNTRYCODE_id = b.ZL_COUNTRYCODE_LSD_ID AND b.spec_lng = 'DE'
WHERE a.ind_land = 1
AND a.kfz IS NOT NULL
AND dat_end = to_date('99991231', 'YYYYMMDD')
ORDER BY TEXT

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

Showing specific data without filtering out query data

I need to build a form where one field (Unplanned Amount) will only populate with data if another field (status) equals a certain value ("not in workflow"). If the status equals anything else, Unplanned amount field would be blank.
The data is coming from three different tables:
Table 1) AccountNum
Table 2) DocNum, DocAmount, DocStatus
Table 3) CommitAmount
The value in CommitAmount will always equal DocAmount, but the value of DocAmount doesn't have to equal the value of the CommitAmount if it's "unplanned."
I tried putting the data into a query and using the following code on my form to no avail:
If Me.DocStatus = "Not in workflow" Then
Me.DocAmount = Null
Else
Me.DocAmount = [forms]![form2]![DocAmount]
End If
Does anyone know how to go about making a query-based form or report that allows what I've described above to take place? Or maybe this should not be done via a query?
Thanks!!
Put the IF statement into to data source for me.docamount
OR use a case statement in the query itself
select case docstatus when 'Not in workflow' then null else docamount end