sql calculated column when find words - sql

I am trying to accomplish the following.
There is a calculated field I need to create that sums the rows when it finds a specific word. Something like this:
SELECT sum(
case
when product.description like'%trans%'
or product.description like '%transitions%'
then sales
else null
end
) as transitions
How can I search for multiple words in a column using the conditional case to sum only the rows that have these words?

First, your particular example is poor because '%trans%' matches '%transitions%'. So, this is sufficient for your example:
SELECT sum(case when product.description like '%trans%'
then sales
end) as transitions
You can search for other things using or:
SELECT sum(case when product.description like '%trans%' or
product.description like '%cis%'
then sales
end) as transitions
Note that if you are looking for words in product.description then perhaps a text field is not the right structure (that is, perhaps you should be storing each word separately). Alternatively, you might want to look into full-text search.

Related

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 extract multiple substring keywords in SQL Server and show results in multiple columns?

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;

How to order by relevancy including word order in SQL

I'm creating a 'smart' search engine that will look into database by relevancy. My system calculate how many words in your sentence correspond to the database field 'tag_clean' that contains text, and try to get the proper result (one per research).
For example you get 'search youpla boom' in a tag_clean field, and another entry like 'search youpla bim' if you tape 'search bim' it will show the second entry.
My system set one point per word and get the most relevant as result. Everything works but my big problem is, it ignores completely the words order !
If you have 'google image test' and 'google test' and you search 'google test image' with my system, the most relevant will be the first one, but it's the second one that's right.
I'd like a system that understand the importance of word orders, but i've no idea how to do it in SQL.
A sample of my SQL request (important part is CASE WHEN at the end):
SELECT *
FROM keywords
WHERE tag_clean LIKE 'google%'
AND (static = 0)
AND
(
tag_clean LIKE '%google%'
OR tag_clean LIKE '%test%'
OR tag_clean LIKE '%image%'
)
OR
(
tag_clean = 'google test image'
AND static = 1
)
ORDER BY
((CASE WHEN tag_clean LIKE '%google%' THEN 1 ELSE 0 END)
+ (CASE WHEN tag_clean LIKE '%test%' THEN 1 ELSE 0 END)
+ (CASE WHEN tag_clean LIKE '%image%' THEN 1 ELSE 0 END))
DESC LIMIT 0, 1;
Thank you people :)
First, I'm not sure that raw SQL is the best tool for this. You should look into the full text capabilities of whatever engine you are using. Searching for text is a rather solved problem, and databases support this functionality (through extensions to the base language).
Assuming you want to continue, the problem is your structure. You could start to add additional clauses for tag clear like '%google test%' and every other two-way combination. That might be a quick and dirty solution.
Your real problem is that you are storing relational data in a single field. There should be a keywords table with a separate row for each keyword on each document. This would have columns such as: documentID, KeyWord, and KeyWordPosition. Using the KeyWordPosition, you could start to do the proximity searches that you want.
However, you are probably better off investigating full text functionality in existing software.

Shortening the sentence to find a search value

Searching for values that uses the same value
Example
SELECT Name, UnitPrice, Quantity, Color
FROM Product
WHERE Name Like '2' OR UnitPrice Like '2' OR Quantity LIKE '2' OR Color LIKE '2'
is there a way to shorten the sentence so that i do not need to repeat Variable LIKE 'value'?
LIKE without wildcards is =
SELECT Name, UnitPrice, Quantity, Color
FROM Product
WHERE '2' IN (Name, UnitPrice, Quantity, Color)
This expands out to a series of OR conditions
Note that the string '2' will be CAST as needed after OR expansion
If you do require wild cards and LIKE, you'd have to use a series of OR statements.
Finally, this won't scale performance-wise. With good indexing, (eg one per column and other 2 as INCLUDE columns) then this would probably be better written as separate queries with UNION between them
Edit, for clarity
OP wants to use wildcards. Therefore my IN suggestion will not work. So you have to do this
WHERE
Name Like '%2%' OR
UnitPrice Like '%2%' OR
Quantity LIKE '%2%' OR
Color LIKE '%2%'
Because of the leading wildcard, there is no point adding indexes. So my text above about indexes+includes is irrelevant

Can someone explain me which queries I need for a nice search in SQL?

I asked for a single query in my previous question but now I want to ask about multiple queries.
I'm making a search engine that searches through some specific tables.
When the input is "testone testtwo testthree" I will need to do a lot of queries.
a search with the complete string
a search with the 3 words in 3 strings ( if they can be seperately found in one item)
a search with 2 words in a combined string
a search with 2 words in 2 strings
a search with all words apart
As you know this decides what is a more important result.
I am building the queries seperately for all the 5 searches above as well as seperate for each table because a result from one table can be more important than from the other.
I made the queries like this:
SELECT *
FROM company_address
WHERE address_street LIKE '%test%'
OR address_zipcode LIKE '%test%'
OR address_telephone LIKE '%test%'"
OR address_fax LIKE '%test%'
OR address_website LIKE '%test%'
I know this is not the best way to do it, and because you guys are much better than me with this i would love some advice!
Even if you could do all of this in one query, it would be hard, if doable, to differentiate which result is more important(relevant) in that case.. so you either do requests separately, or go for some completely different solution.
Looks like you could use soemthing like Sphinx search server (it is used in conjuction with your RDBMS, MySQL most probably), it's opensource:
http://sphinxsearch.com/
Sphinx can search for phrases and sort results by relevance.
In the past I've done some trickery using CASE.
SELECT
*,
CASE WHEN address_street LIKE '%test%' THEN 1 ELSE 0 END
+
CASE WHEN address_zipcode LIKE '%test%' THEN 1 ELSE 0 END
+
CASE WHEN address_telephone LIKE '%test%' THEN 1 ELSE 0 END
+
CASE WHEN address_fax LIKE '%test%' THEN 1 ELSE 0 END
+
CASE WHEN address_website LIKE '%test%' THEN 1 ELSE 0 END
AS Score
FROM company_address
That gives you results with a score where the more matches there are, the higher the score, and you can give different scores to different matches. If you repeat the CASE statement in the ORDER BY or WHERE, you can narrow down your results.
This can get unwieldy pretty quick though, so be careful :)