I have a table in Snowflake with people's names and other attributes. To simplify, it looks like the table below.
How can I add a new column with assigned unique number to each person directly to the table using SQL?
The ideal result is like below
Use dense_rank():
select name, dense_rank() over (order by name) as uniquenum
from t;
You can use this logic in an update, but the exact syntax depends on the database.
Related
I can’t find how to rename the row counting column in a table in an SQL Server RDMS. When you create a table and you have user created columns, A and B for example, to the farthest right of those columns, you have the Row Number column.
It does not have a title. It just sequentially counts all the rows in your table. It's default. Is it possible to manipulate this column denoting the row numbers? Meaning, can I rename it, put its contents in descending order, etc. If so, how?
And if not, what are the alternatives to have a sequentially counting column counting all the rows in my table?
No. You can create your own column with sequential values using an identity column. This is usually a primary key.
Alternatively, when you query the table, you can assign a sequential number (with no gaps) using row_number(). In general, you want a column that specifies the ordering:
select t.*, row_number() over (order by <ordering column>) as my_sequential_column
from t;
I have a SQLite table sorted by column ID. But I need to sort it by another numerical field called RunTime.
CREATE TABLE Pass_2 AS
SELECT RunTime, PosLevel, PosX, PosY, Speed, ID
FROM Pass_1
The table Pass_2 looks good, but I need to renumber the ID column from 1 .. n without resorting the records.
It is a principle of SQL databases that the underlying tables have no natural or guaranteed order to their records. You must specify the order in which you want to see the records when SELECTing from a table using an ORDER BY clause.
You can obtain the records you want using SELECT * FROM your_table ORDER BY RunTime, and that is the correct and reliable way to do this in any SQL database.
If you want to attempt to get the records in Pass_2 to "be" in RunTime order, you can add the ORDER BY clause to the SELECT you use to create the table but remember: you are not guaranteed to get the records back in the order in which they were added to the table.
When might you get the records back in a different order? This is most likely to happen when your query can be answered using columns in a covering index -- in that case the records are more likely to be returned in index order than any "natural" order (but again, no guarantees with an ORDER BY clause).
If you want a new ID column starting at 1, then use the ROW_NUMBER() function. Instead of ID in your query use this ROW_NUMBER() OVER(ORDER BY Runtime) AS ID.... This will replace the old ID column with a freshly calculated column
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
I am trying to create a query that returns a single row for each unique ID in my oracle table.
The problem is that i have one column, Description, that isnt unique in each row (Description-column is the only coulmn that can differ for each ID row btw). This is what my table looks like:
ID Description Customer
==================================================
5119450733 Cost GOW_1
5119450733 Price GOW_1
1543512377 Cost GOW_2
Is there a way to query the table so that i append the results from Description so that i can have unique id rows? for example like this:
ID Description Customer
==================================================
5119450733 Cost,Price GOW_1
1543512377 Cost GOW_2
Use LISTAGG function if you are using Oracle 11g Release 2.
SELECT Id,
listagg(Description,',') WITHIN GROUP(ORDER BY description) AS Description,
Customer
FROM <table_name>
GROUP BY id, customer;
Refer the below link to know more about String Aggregation Techniques on different versions.
Here is the table:
table A (
id integer primary key,
name varchar(20),
age integer
)
The interview question is:
Write a query to return the list of unique names from table .
Here is my solution:
select id from A group by name;
My question: is this the efficient way to return the list of the unique names?
No, because you're returning the IDs, not the names. That query won't even work in many DBMS' because selected columns have to either be in the group by clause or have an aggregating function (like max() or sum() and so on) applied to them.
You would need:
select name from a group by name
It's usually even more efficient if the name column is indexed.
The other common way of doing this is:
select distinct name from a
I would go with
SELECT DISTINCT name FROM A
or
SELECT name FROM A GROUP BY name
both are well understood. As for which one is faster, it may depend on the RDBMS in question.
Select Distinct name FROM A is possibly the best bet you can get. There is no concept of getting list. You can create a list in the programming language which is linked to your database.