I have this table
I need to pass the rows of the column brothers to others columns, like this.
how can I do it?
I think that I can't use the function PIVOT because I don't know how many rows there will be. and i can't set a limit for the number of rows.
Related
Is it possible to divide all columns in a table by one of them? There are 168 of them so I'd rather not write column2/column1, column3/column1, etc.
As Suggested by #Aishwary, you can write a script to get the list of columns using INFORMATION_SCHEMA and then iterate over the list of columns to get the desired result.
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
Im trying to make a table with dynamic columns.
I have this table. This is just simplified, in other instances these may have more values instead of 3.
name
----------------------
Fall
Medication
Wander
(3 rows)
I am trying to get this result. I need to separate the values into columns.
Fall | Medication | Wander
--------+------------+--------
(0 rows)
You need to PIVOT the table. Unfortunately MySQL (unlike Oracle http://www.oracle.com/technetwork/articles/sql/11g-pivot-097235.html) doesn't have a PIVOT operator, but there are workarounds it seems. MySQL pivot table
You might try one of the crosstab() functions in newer PostgreSQL. See https://www.postgresql.org/docs/current/static/tablefunc.html (F.35.1.2 and F.35.1.4). It allowes you to specify a query that has row-names (those create rows), categories (that create columns) and values (that populate the inside part of the table.
The variant with 2 queries can be especially useful since it allows you to specify the columns you want to use with a seperate query.
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.
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.