combine multiple rows into one row based on column value - sql

IS there a way in SQL to make multiple rows for a common column, show up in one?
For example I would like this output:
to show up as:

I believe this article contains solutions to your question -
SQL Query to concatenate column values from multiple rows in Oracle

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.

(Hibernate) searching for strings that match a pattern

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, ':'))

Equivalent of CONCAT_GROUP for multiple columns

Do you have any idea on how to display values obtained by concat_group in multiple columns instead of having a unique column containing all the values separated by commas.
Thanks in advance :-)
You cannot do this in SQL.
One of the fixed rules of SQL is that the columns in your select-list must be set at the time you prepare your query. The select-list does not expand dynamically to match the values it finds as it examines the data.
This comes from the origins of SQL in the relational model. A relation (not a relationship, lots of people get this wrong) is a data structure with a fixed set of columns, a header defining the names and data types of the columns, and then a set of rows, where every row has the same set of columns as the header.
The select-list of an SQL SELECT statement effectively defines the header of the relation returned as the result-set of that query. The number and names of the columns are defined by the query, not by the data in the result.
A commenter above asks if you want to do a pivot, but a pivot also requires that you name the columns in the select-list. There is no such thing as a SQL pivot query that grows its select-list according to the data in the result.

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-%'

String Grouping from a single column in Oracle database having million rows and removing duplicates

We have a huge table and one of the column contains queries like e.g. in row 1
1. (((firstname:Adam OR firstname:Neil ) AND lastname:Lee) ) AND category:"Legal" AND type:Individual
and in row 2 of same column
2. (((firstname:Adam* OR firstname:Neil ) AND lastname:Lee) ) AND category:"Legal" AND type:Organization
Similarly there are few other types of Query strings which are used eventually to query external services.
Issue is based on certain criteria I have to group and remove duplicates from this table.
There are few rules to determine grouping of Strings in different rows.One of them is that if first name and lastname are same then ignore category and type values, therefore above two rows will be grouped to one. There are around million rows. Comparing Strings and doing grouping is not looking elegant solution. What could be best possible solution using sql.