(Hibernate) searching for strings that match a pattern - sql

I can't find the hql to solve my problem.
I have 2 nice tables.
The first table has a column with strings in the form 'xxx-xxx-xxx'.
The second table has a column with strings in the form 'some_prefix:xxx-xxx-xxx'.
What I want to do is given a subset of rows in the second table, find all the entries in the first table that match in the 'xxx-xxx-xxx' part. And I know for sure there cannot be more that one entry in the fist table for each row in the second.
I'm looking for a hql query that fetches those objects but I could use a sql too.
Cheers.

you can use a combination of locate and substring function on column of the second table to get the string after the : sign.
I haven't tested it but it should be something like:
where table1.column = substring(table2.column, locate(table2.column, ':'))

Related

How can I create multiple rows based on the value of one column in SQL?

I have a column of type string in my table, where multiple values are separated by pipe operator. For example, like this,
Value1|Value2|Value3
Now, what I want is to have a query, which will show three rows for this row. Basically something similar to the concept of explode in Dataframes.
Note that I am using Spark SQL. And I want to achieve this using SQL, not dataframes.
I got it working by using the following query.
select t.*, explode(split(values, "\\|")) as value
from table t
\\| here can also be replaced by [|]. Just specifying | doesn't work.

Removing rows with duplicated column values based on another column's value

Hey guys, maybe this is a basic SQL qn. Say I have this very simple table, I need to run a simple sql statement to return a result like this:
Basically, the its to dedup Name based on it's row's Value column, whichever is larger should stay.
Thanks!
Framing the problem correctly would help you figure it out.
"Deduplication" suggests altering the table - starting with a state with duplicates, ending with a state without them. Usually done in three steps (getting the rows without duplicates into temp table, removing original table, renaming temp table).
"Removing rows with duplicated column values" also suggests alteration of data and derails train of thought.
What you do want is to get the entire table, and in cases where the columns you care about have multiple values attached get the highest one. One could say... group by columns you care about? And attach them to the highest value, a maximum value?
select id,name,max(value) from table group by id,name

Selecting all Columns which have not already been mentioned

First off: I am reverse engineering SQL queries and as a result I have a bunch of candidate queries, which all contain a designated 'entity' column in their SELECT clause.
The problem is: I want the entity column to be the first column of my result and let all the other columns follow, so basically I want something like this
SELECT A.entity, *
FROM A
...
...but without the duplicate entity column resulting from the *-operator.
EDIT: I also do not know all of the column names, generally only the entity column name is known.
Thanks for helping me

How to build SQL query for associated values?

I am using PyODBC to fetch some data, since I am not fetching all data in my table, I need to write a query which grabs only rows which have associated columns. For example my initial query is:
SELECT SRNumber FROM SO_SC_1 WHERE SRNumber LIKE '%1-%'
This returns the SRNumber values that I want.
Next I want to return the associated last edited user with this SRNumber. This column is named last_edited_user. What is the proper syntax to incorporate multiple queries into one for this scenario? Basically I would like to use the initial query and grab all associated data for each SRNumber.
You query all needed columns using their comma separated names
SELECT SRNumber, last_edited_user
FROM SO_SC_1
WHERE SRNumber LIKE '%1-%'

SQL function CONTAINS() does not return expected results?

I have a table 'Asset' with a column 'AssetDescription'. Every row of it has some group of words/sentences, seprated by comma.
row1: - flowers, full color, female, Trend
row2:- baby smelling flowers, heart
Now if I put a search query like:-
select * from Asset where contains(AssetDescription,'flower')
It returns nothing.
I have one more table 'SearchData' with column 'SearchCol', having similar rows as mentioned above in table 'Asset'.
Now if a put a search query like:-
select * from SearchData where contains(SearchCol,'flower')
It returns both the rows.
QUESTION:-
Why first query doesn't return any result, but second one does correctly.
If 'Full Text Search' has something to do with 1st ques, than what to do regarding that. As I'm using SQL server 2000.
Clearing a comment doubt on my question:-
Table 'SearchData' has more than 100,000 rows and so as the table 'Asset'.
Those two tables are NOT identical. But their respective columns has rows that contains some group of words seperated by commas. (So flowers, flower etc etc are in plenty in both of those columns.)
Screenshot of the Indexes of both the tables (Asset and SearchData):-
it probably has something to do with your full-text index configuration.
Can you post some info on your index and catalog?
If you read the article on CONTAINS you will see that when searching for
contains(AssetDescription,'flower')
'flower' is treated as a simple term which
matches an exact word or phrase
However for
contains(AssetDescription,'flower*')
'flower' is treated as a prefix term which
specifies a match of words or phrases beginning with the specified
text
and will match 'flowers' in your data.
So, are you sure that data in your two tables is the same, or does 'Asset' contain 'flowers' and 'SearchData' contain 'flower'?
GOT THE SOLUTION.
THANKS TO ALL and especially DIEGO for support.
To allow the FULL TEXT SEARCH (FTS) to work properly:-
Enable FTS for the required table.
Enable FTS for the required column under that table.
Open the properties for the same table and check if attributes 'Full-text change tracking' and 'Full-text update-index' are enabled. If not, enable them.
DONE.
: )