How to add logic to columns in Snowflake - snowflake-cloud-data-platform

Currently, I am using Snowflake, and I have to extract a string within an URL. This would tell me what kind of activity is going on in the data. I am using the following code:
SELECT [WAREHOUSE].[DATABASE].[SCHEMA]
AS
log:event_type AS event_type,
log:blah_blah_blah
Basically, I want to see if the event_type contains a particular phrase. For example, the event_type may contain the phrase "directory." If that is the case, I would like to add another column that only says "directory."
I'd imagine that the code would contain logic along the lines of:
CASE
WHEN CONTAINS(event_type, "directory") THEN "directory"
But how exactly could we make that happen? I hope this makes sense. Thanks in advance for your guys' help!

You can use a case expression with like
(CASE WHEN event_type LIKE '%directory%' THEN 'directory'
END)

Related

How to take apart information between hyphens in SQL Server

How would I take apart a column that contains string:
92873-987dsfkj80-2002-04-11
20392-208kj48384-2008-01-04
Data would look like this:
Filename Yes/No Key
Abidabo Yes 92873-987dsfkj80-2002-04-11
Bibiboo No 20392-208kj48384-2008-01-04
Want it to look like this:
Filename Yes/No Key
Abidabo Yes 92873-987dsfkj80-20020411
Bibiboo No 20392-208kj48384-20080104
whereby I would like to concat the dates in the end as 20020411 and 20080104. From the right side, the information is the same always. From the left it is not, otherwise I could have concatenated it. It is not an import issue.
As mentioned in the comments already, storing data like this is a bad idea. However, you can obtain the dates from those strings by using a RIGHT function like so:
SELECT RIGHT('20392-208kj48384-2008-01-04', 10)
Output:
2008-01-04
Depending on the SQLSERVER version you are using, you can use STRING_SPLIT which requieres COMPATIBILITY_LEVEL 130. You can also build your own User Defined Function to split the contents of a field and manipulate it as you need, you can find some useful examples of SPLIT functions in this thread:
Split function equivalent in T-SQL?
Assuming I'm correct and the date part is always on the right side of the string, you can simply use RIGHT and CAST to get the date (assuming, again, that the date is represented as yyyy-mm-dd):
SELECT CAST(RIGHT(YourColumn, 10) As Date)
FROM YourTable
However, Panagiotis is correct in his comment - You shouldn't store data like that. Each column in the database should hold only a single point of data, be it string, number or date.
Update following your comment and the updated question:
SELECT LEFT(YourColumn, LEN(YourColumn) - 10) + REPLACE(RIGHT(YourColumn, 10), '-', '')
FROM YourTable
will return the desired results.

Proper-Casing Street names in SSRS

With the application that I am working with and writing reports for, the user is entering the Location in all upper case. It has been requested by those who my reports are going to that the Location be in proper case. This was fine till I realized that proper case does not recognize abbreviations. Is there a way to write an expression in SSDT that will, while converting the street name into proper case, also make is so abbreviations like "SE" or "DR" are upper case?
John Saunders is right, it's not simple, and it'd be better if you can fix the data at the source. But you can wrap your Proper Case function in a series of outer REPLACE Functions. It's not simple because you'll have to analyze your data and figure out all the abbreviations you want to handle, and manually code each one. It will get huge, so you might consider creating this function in SSRS custom code, so it doesn't look so cluttered in the expression builder.
Psuedo code would look something like this:
REPLACE(
REPLACE(
ProperCase(MyFieldName)
,"Se","SE")
,"Dr","DR")
Add a REPLACE(InnerExpression,ProperCaseExpression,UpperCaseExpression) for each individual abbreviation you want to handle. It won't be fun, but it will work.

Grouping by the shortest common suffix in data

I have a table with a list of FQDN's, for example:
www.bbc.co.uk
bbc.co.uk
bbc.com
www.bbc.com
www.live.bbc.co.uk
www.live.bbc.com
I'd like to group these by the domain name; not the exact full domain name, but the shortest matching domain name that exists in the data. For instance, in the example above, I'd like to group
www.bbc.co.uk
bbc.co.uk
www.live.bbc.co.uk
together, as they have the common "suffix" of bbc.co.uk.
The fact that these are domain names is probably irrelevant, but might also play a part in the solution - can anyone suggest a way of GROUPing data together by the shortest common suffix?
EDIT: as requested, as an output I'd ideally like something like:
Domain Count
bbc.co.uk 3
bbc.com 3
If you do not know how many suffix to add in your grouping, it will be hard.
Maybe you can try to group by the last suffix (after the last dot).
Then if you got result, add the next suffix and group.
Then if you got result, add another one...
You can get the same amount of dots if you first convert the domain type to an IP address using nslookup. Link
Alternatively, there exists entire databases with list of known domain names. Link2
I've managed to bodge my way around the problem: I've introduced a temporary "MasterDomainName" field to the database, and I've updated it with:
UPDATE r1
SET r1.MasterDomainName= r2.domainname
FROM #results r1
LEFT JOIN #results r2
ON r2.domainname = right(r1.domainname,len(r2.domainname))
It's not perfect, but it gets me closed to where I need to be. Thanks for everyone's input.

Drupal 7 EntityFieldQuery OR Condition

I've got a query going using EntityFieldQuery. I have two fields called field_tags and field_categories the column name I'm looking at in each is tid. I want to check each field for a matching tid. Each one is checking a separate array of tids.
Normally I would use something like:
$query->fieldCondition('field_tags', 'tid', $my_tags, 'IN');
But now, since I'm checking if there are any matches in the tags array for field_tags OR the categories array for field_categories, I don't know how to do it. The idea is that each row returned must match at least one tag or one category.
I know there has to be an OR condition in there somewhere...
Thanks,
Howie
Found this: http://treehouseagency.com/blog/fredric-mitchell/2012/02/21/or-queries-entityfieldquery which explains how to use a tag to later the query and incorporate an OR clause.

MySQL Query Nightmare with RETs data

For those of you who have actually delt with RETS may be able to give me a hand here. The problem occurs when multiple properties are tied into the RETS data even though the property is sold. Basically what I need is to be able to check the database with the SELECT statement against three fields. The fields in question would be C_StreetName, C_StreetNumber, and C_PostalCode.
To make this clear what I want is some type of way to check for duplicates while gathering the dataset, this can't be done in php because of how the data is returned through the application. So if it finds another record with the same C_StreetName, C_StreetNumber, and C_PostalCode it will remove them from the dataset. Ideally it would be nice if it could also check the Status of the two to find out if one is Expired or Sold before removing them from the data.
I'm not familiar with complex SQL functions, I was looking at the IF statement until I found that can only be used while storing data not the other way around. And the CASE statement but it just doesn't seem like that would work.
If you guys have any suggestions on what I should use I'd appreciate it. Hopefully there is a way to do this and keep in mind this is only one table I am accessing I don't have any Joins.
Thanks in advance.
Here's something to get you going in the right direction. I haven't tested this, and am not sure you can nest a case expression inside max() in mysql.
What this accomplishes is to output one row per unique combination of street name, number and postcode, with a status of 'Expired' or 'Sold' taking precedence over other values. That is, if there's a row with 'Expired' it will be output in preference to non-expired and non-sold, and a row with 'Sold' will be output if it exists, regardless of what other rows exist for that property. The case statement just converts the status codes into something orderable.
select
C_StreetName,
C_StreetNumber,
C_PostalCode,
max(
case status
when 'Expired' then 1
when 'Sold' then 2
else 0
end) as status
group by
C_StreetName,
C_StreetNumber,
C_PostalCode;

Resources