How can I create multiple rows based on the value of one column in SQL? - 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.

Related

combine multiple rows into one row based on column value

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

Splitting up same values in a query output

For starters I should say that I'm using openoffice.
I've got a query with the following columns.
Now when I get the results there are multiple delegations with the same value.
Let's just say for demonstrating purposes I got 6 delegations which are and 3 of them have the same value. In the results they are shown directly below one another. But for this I need a result where I can't have that. I need to separate each value in a way that they don't appear after each other.
So that the results shouldn't look like this
but that the delegations are separated with a least another delegation in between
I hope you got what I want to do.
you use a following query
query=SELECT DISTINCT column1, column2, ...
FROM table_name;
The SELECT DISTINCT statement is used to return only distinct (different) values.
nside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
The SELECT DISTINCT statement is used to return only distinct (different) values.

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

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

Get row values as column names in t-sql

I have a requirement to display row values as column names in a data grid view. I want to get the store names into columns using sql select statement. (Please refer the attached image). I want user to enter some values under each column. So STORE 1, STORE 2, STORE 3 should displays as columns in datagrid view. Does anyone can help me to get this work?
while googling i found this can be done using PIVOT in SQL. But in this table i don't have any aggregate columns. Any help pls?
the result should be somthing like
You may know that your data only contains a single row for each pivoting column, but SQL Server has to construct a plan that could accommodate multiple rows.
So, use PIVOT and just use an aggregate that, if passed a single value, will return that same value. MIN() and MAX() fit that description (as does SUM if you're working with numeric data)
You may use specific function of dynamic pivot and pass your query with item count column.
You can use below link which provided you function and can easily show you expected output.
http://forums.asp.net/t/1772644.aspx/1
Procedure name:
[dbo].[dynamic_pivot]