SQL - After group by filtering down to rows where column value = certain string - sql

Not sure why this isn't working.
When I run the query without HAVING segment ilike 'Enterprise' I get results (see screenshot) but when I add it back in the query returns nothing despite clearly containing instances where segment = 'Enterprise'. I can't find any instances online where having is used to filter for strings so I'm thinking this just isn't possible. Could someone confirm if this is the case and if there is an alternative method if so? Thanks.

You can't use HAVING to filter for strings, you can only use it to filter for numeric values. To filter for strings, you'll need to use the WHERE clause.

Related

Filter for "contains text" with OR or CASE to allow multiple options in NetSuite. Custom field: Keyword Match option seems available

NetSuite formula(text) problem.
I have a specific search I need to power up but I could also really use this for general use across a few types of reporting.
We use a lot of custom body fields which do not seem to have access to the Keyword Match option in the dropdown system or I would be using commas left and right.
I need to search a single custom field for multiple possible answers.
I was thinking maybe a CASE WHEN formula but I don't have an else.
Lets call the field states. I want this report to only return rows of records where there are the following 5 states in the custom body field "Shipping State". it isn't allowing keyword match. I only want to see transactions with Michigan, New Jersey and Floria (this is hypothetical) so I want just to put a filter in up front to pair down the results.
CASE WHEN {custbody.shippingstate} IS 'Michigan' ...
Can I use an OR construct here? and do I need an else?
I tried using coalesce but I don't think I had the syntax around the coalese right.
Any help for a formula newbie appreciated.
Try the following as a formula text result column in a NetSuite saved search.
CASE WHEN LOWER({custbody.shippingstate}) LIKE '%michigan%'
OR LOWER({custbody.shippingstate}) LIKE '%jersey%'
OR LOWER({custbody.shippingstate}) LIKE '%florida%'
THEN 'Y' END
LIKE conditions are case sensitive, so that's why I incorporated the LOWER() method. Reminder in NetSuite "%" matches any string of any length (including zero length, and "_" matches a single character. If this works as a results column you can add the formula text as a criteria to filter results.

Datasource Establishment in Tableau and 170,000 records

I have two EXCEL datasources. 175,000 rows. I'm trying to set up a join (Add New Join Clause) using the INNER option between the two datasources. The left datasource includes certain member id #s. Unfortunately, the right datasource's member id #s are within a large field called member Desc. Something like below,
Datasource Left
Member ID #
ALL89098
Datasource Right
Member Desc
YTRNNN TO=ALL89098_KIA TO BE OR NOT OR
POALL89098 JOE
So, I need to deal with two scenarios as you notice from above. The member id is within the Member Desc after a TO= and it could be anywhere like scenario 2 POALL89098
If I can't get this done in Tableau to establish the Join between these two columns from different datasources, since I have both of these datasources loaded into SQL Server DB, I can run SQL statements in SQL since they are in two different Tables within SQL Server DB as well.
I'm trying the use of CONTAINS clause in Tableau such as below but it is running very very slow. it is only Tableau Desktop with 16 GB Ram.
if contains([Member Desc],([Member id #])then
[Member id #]
ELSE
"NOT FOUND"
END
Thanks so much for your time.
SO, IS THERE A WAY TO HAVE THE REGEXP WITHIN IF AND ELSE OR CASE STATEMENTS?
You can create a join calculation. The highlighted dropdown shows where this can be found:
As long as the format of the Member ID in [Member Desc] has some pattern, it can be extracted with Regex. As you mention in your question, one way the ID may present itself is after a "TO=" and it looks like it ends before a "_". The following regex calculated field will pull the string between the two:
REGEXP_EXTRACT([Member Desc],"([^TO=]*)(?=_)")
The result should properly join the two datasources:
The above is an outline which I hope sets you on the right path. I realize that there may be a few different methods in which the [Member ID] presents itself so I wont be able to nail down the exact Regex, but if there is any pattern at all then the format above should work. (ie: even if the only pattern is that [Member ID] is three letters followed by four numbers - or it always starts with an A and ends with something else - etc.)
Regex should also perform better than a contains() function, but do be aware that the function does need to search through every string in every row to make the join.
Edit in response to comment:
To add multiple conditions, try the following method:
IF LEN(REGEXP_EXTRACT([Member Desc],"([^FROM=]*)(?=,)")) > 0
THEN REGEXP_EXTRACT([Member Desc],"([^FROM=]*)(?=,)")
ELSEIF LEN(REGEXP_EXTRACT([Member Desc],"([^TO=]*)(?=,)")) > 0
THEN REGEXP_EXTRACT([Member Desc],"([^TO=]*)(?=,)")
ELSEIF [...Put as many of these as might match your pattern]
THEN [...Put as many of these as might match your pattern]
END
Essentially the calculation is going down the list and trying each possibility. I changed yours a little to look at the length (LEN()) of the returned value which should compare fairly quickly, as it is an integer. As this calculation iterates through each ELSEIF and finds a match, it will stop iterating through the list -- so its important to put the most likely match at the top. The result of the calculated field should be a member ID. If there is no match, there really isn't a need for an ELSE statement because the Inner Join will exclude it automatically.
Edit in response to comment:
Thank you. I see your recommendations.
I think you are going to have to find a way to strip out the member ID from the member desc in SQL. There should be some pattern to Member ID.
For instance is it always 3 letters followed by 5 numbers or something similar.
If you can come up with a pattern, then you can use SQL and some combination of Substring, Charindex, and/or Like %Text% or a regex
pattern to strip out the actual member ID in the SQL Server table as its own field before bringing it into Tableau.

Create Search Function including AND OR vb .net

Hope you can help me out and inspire me here
I am looking to create a Search function that searches through a datagridview column looking for key words and then filtering based on the result. I have done this already and it works fine. However this is based on the user entering strings into the textbox separating their strings with a comma. So an example would be
A DataGridViewColumn with 3 values
ERROR_QUEUE_MAY05
ERROR_QUEUE_MAY06
ORDER_QUEUE_JAN01
The User then enters the search criteria
ORDER, 06
And the result would be
ERROR_QUEUE_MAY06
ORDER_QUEUE_JAN01
As you can see the filter has used an OR to filter the column
I want the user to be able to use brackets () and also AND statements like you would in a SQL statement so they could use for example
("ORDER" AND "MAY") OR "03"
This would filter the results to show anything with ORDER and MAY in the title or 03.
Has anyone done anything like this in the past or have any ideas of going about it?
Thanks All
One approach is to scan the string from left to right, then recursively call the evaluator every time parentheses are encountered. After a recursive call, replace the parenthesized expression with "true" or "false". If you intend to allow and a higher priority than or, you can treat that recursively as well.

Hide Empty columns

I got a table with 75 columns,. what is the sql statement to display only the columns with values in in ?
thanks
It's true that a similar statement doesn't exist (in a SELECT you can use condition filters only for the rows, not for the columns). But you could try to write a (bit tricky) procedure. It must check which are the columns that contains at least one not NULL/empty value, using queries. When you get this list of columns just join them in a string with a comma between each one and compose a query that you can run, returning what you wanted.
EDIT: I thought about it and I think you can do it with a procedure but under one of these conditions:
find a way to retrieve column names dynamically in the procedure, that is the metadata (I never heard about it, but I'm new with procedures)
or hardcode all column names (loosing generality)
You could collect column names inside an array, if stored procedures of your DBMS support arrays (or write the procedure in a programming language like C), and loop on them, making a SELECT each time, checking if it's an empty* column or not. If it contains at least one value concatenate it in a string where column names are comma-separated. Finally you can make your query with only not-empty columns!
Alternatively to stored procedure you could write a short program (eg in Java) where you can deal with a better flexibility.
*if you check for NULL values it will be simple, but if you check for empty values you will need to manage with each column data type... another array with data types?
I would suggest that you write a SELECT statement and define which COLUMNS you wish to display and then save that QUERY as a VIEW.
This will save you the trouble of typing in the column names every time you wish to run that query.
As marc_s pointed out in the comments, there is no select statement to hide columns of data.
You could do a pre-parse and dynamically create a statement to do this, but this would be a very inefficient thing to do from a SQL performance perspective. Would strongly advice against what you are trying to do.
A simplified version of this is to just select the relevant columns, which was what I needed personally. A quick search of what we're dealing with in a table
SELECT * FROM table1 LIMIT 10;
-> shows 20 columns where im interested in 3 of them. Limit is just to not overflow the console.
SELECT column1,column3,colum19 FROM table1 WHERE column3='valueX';
It is a bit of a manual filter but it works for what I need.

Django: how to filter for rows whose fields are contained in passed value?

MyModel.objects.filter(field__icontains=value) returns all the rows whose field contains value. How to do the opposite? Namely, construct a queryset that returns all the rows whose field is contained in value?
Preferably without using custom SQL (ie only using the ORM) or without using backend-dependent SQL.
field__icontains and similar are coded right into the ORM. The other version simple doesn't exist.
You could use the where param described under the reference for QuerySet.
In this case, you would use something like:
MyModel.objects.extra(where=["%s LIKE CONCAT('%%',field,'%%')"], params=[value])
Of course, do keep in mind that there is no standard method of concatenation across DMBS. So as far as I know, there is no way to satisfy your requirement of avoiding backend-dependent SQL.
If you're okay with working with a list of dictionaries rather than a queryset, you could always do this instead:
qs = MyModel.objects.all().values()
matches = [r for r in qs if value in r[field]]
although this is of course not ideal for huge data sets.