Sort Order Column Dynamically Created From Another Column (Postgres) - sql

I am trying to output a sort order column on a Postgres select query to give each row an order integer based the values of it's columns - with this sort order derived from another column. Something like this:
select column_1_name, column_2_name, column_3_name, [a formula] AS sort_order
from table_name
The data in the column sort_order_column would be a comma separated list of column names in the order I would like them to show in the sort_order (starting from 1 again for each unique item in the item column).
In the example below, I want to sort the 'car' items first by column_2_name, then by column_3_name. - this means the green car comes before the red car.
For boats, these are sorted first by column_1_name, then column_3_name, then finally by column_2_name. This means the 10m boat comes before the 20m boat.
Data:
Desired output
Thanks in advance.

Related

How can I remove Null value from first column but keep the value of the 2nd and thirds columns

I am Omar, a new learner of SQL.
I have a large excel sheet that I want to analyze by SQL.
It has the following columns (Manufacturers, Products, sales)
the problem is, in the first column 'Manufacturers,' the manufacturer name has only been entered once per one manufacturer. while for the rest of the below rows, the cells are empty until the next manufacturer.
Please refer to the attached image for more understanding.
How can I remove these null values in my query results while keeping the values of the product column value?
thank you
The main problem you have is that SQL tables represent unordered sets. So, if you have only your specified columns, you cannot reconstruct the Excel format.
To solve this, you want to load the data into a table that has an identity or auto-incremented column, in order to preserve the insertion order. The exact details depend on the database. Let me call this column id.
Then you can "spread" the value where it is missing. One method is:
select t.*,
max(manufacturer) over (partition by manufacturer_grp) as imputed_manufacturer
from (select t.*,
count(manufacturer) over (order by id) as manufacturer_grp
from t
) t

HIVE table - Get records which are closest to a given numeric value

From a hive table, I want records which are closest to a given value of each of the columns.
E.g.
The table has columns - total_score, avg_score, etc. I want to get records which have total_score and avg_score close or equal to "a given value".
Note - Table has approx. 183 million rows and I want 1,50,000 records which are closest/equal to the given value of each of the columns.
Please help me with the process of doing it.
The general concept needs to be top x, ordered by the absolute value of difference between parameter value and values in list.

Create a Patern Rank, Based on THE VALUE(s) IN A COLUMN

I have data like this -
I want to create one more column name Pattern and it looks like this -
How to create this column -
It is based on the column 'NAME'. It should Rank Starting from the Row having Parent_ID=(ContentID of the row having Parent ID 1)
It should increase by one and should make the same Group if it get's 'Only For'/'Not For' in the column NAME.
It will work on the group of Jill_Equipment_ID,Req_Group,Operand..
As my complete data in broken into these groups and it will have multiple Jill_Equipment_ID and for each ID multiple Req_Group and for each group multiple Operands.
Please help me in getting it solved. Thanks in advance.

How would you total the values in one column and keep a distinct value in another?

I have a database table that has multiple codes in one column that correspond to certain values in another column. For example, a particular code in column A corresponds to a value in column B. There are thousands of duplicate entries in column A that correspond to different values in column B. I want to add up all of the values in column B that have the particular code in column A, while only keeping one copy of the code from column A. You may think of the columns as key-value pairs, where column A contains the key and column B contains the value.
Basically, I want to add all the values in column B where column A is a specific value, and I want to do for this all of the unique "keys" in column A. I'm sure that this is a simple task; however, I am pretty new to SQL. Any help would be greatly appreciated.
Here is the result I'm looking for.
This should work:
SELECT
A,
SUM(B) AS sum_b
FROM [yourTable]
GROUP BY A
SELECT COLUMNA, SUM(ISNULL(COLUMNB,0)) AS TOTAL
FROM
dbo.TableName
GROUP BY COLUMNA

Assign an ID Value for Every Set of Duplicates

How can i generate an ID value for every set of duplicate records as seen in the second table with ID column? In other words, how can I let the first table to look like the second table using SQL query?
Assume that first name and last name in the first table can appear in duplicates.
Each first name and last name can have one or many purchase yr and cost.
The given image is just a sample. Total records in table 1 can reach thousands.
I'm using Oracle SQL.
Note: I'm working with one table only that is the first one. The second table is what I want.
You can use the DENSE_RANK analytic function to assign ID's as below:
EDIT:
Simplified query to generate ID's.
SELECT
DENSE_RANK() OVER (ORDER BY First_Name, Last_Name) ID,
t.*
FROM Table1 t;
Reference:
DENSE_RANK on Oracle Database SQL Reference