How to reorder column values in a table? - sql

When I delete some rows from a table it looks like the following table after removing:
+-----+--------+----------+
| ID | Name | Address |
+-----+--------+----------+
| 1 | name1 | address1 |
| 2 | name2 | address2 |
| 8 | name8 | address8 |
| 9 | name9 | address9 |
+-----+--------+----------+
The problem is the ID isn't ordered properly.
I need it to be reordered like this:
+----+---------+----------+
| ID | Name | Address |
+----+---------+----------+
| 1 | name1 | address1 |
| 2 | name2 | address2 |
| 3 | name8 | address8 |
| 4 | name9 | address9 |
+-----+--------+----------+
Is there a command to reorder the column like shown?
I'm using sqlite

I think this type of query should do that:
UPDATE yourTable
SET ID = (SELECT COUNT(*) FROM yourTable ti WHERE ti.ID <= yourTable.ID)

Related

Join postgres table that has two common columns of another table

I have two tables:
Transactions:
+---------+--------------+------------+-----------+
| id | address_from | address_to | value |
+---------+--------------+------------+-----------+
| 1 | 1 | 2 | 1000 |
| 2 | 1 | 2 | 500 |
+---------+--------------+------------+-----------+
Addresses:
+---------+--------------+
| id | address |
+---------+--------------+
| 1 | address1 |
| 2 | address2 |
+---------+--------------+
I need to get all transactions with adresses instead id:
SELECT * FROM transactions tr
JOIN addresses ad ON tr.address_from = ad.id OR tr.address_to = ad.id
WHERE tr.address_from = 1 OR tr.address_to = 1
And then i get following result:
+---------+--------------+-----------+
| id | address | value |
+---------+--------------+-----------+
| 1 | address1 | 1000 |
| 1 | address1 | 1000 |
| 2 | address2 | 500 |
| 2 | address2 | 500 |
+---------+--------------+-----------+
But i need something like this:
+---------+--------------+-------------+-----------+
| id | address_from | address_to | value |
+---------+--------------+-------------+-----------+
| 1 | address1 | address2 | 1000 |
| 2 | address1 | address2 | 500 |
+---------+--------------+-------------+-----------+
How can I get that result?
And isn't it so expensive to do join on two columns?
You could try with 2 join on same table to get the expected result
SELECT tr.id,adf.address as address_from,adt.address as address_to,tr.value
FROM transactions tr
JOIN addresses adf ON tr.address_from = adf.id
JOIN addresses adt ON tr.address_to = adt.id

How to insert or update a column using SQL based on sorted number of items for each item group

I have two tables 'Product' and 'product_Desc'
+-----------+-------------+
| ProductID | ProductName |
+-----------+-------------+
| 1 | A |
| 2 | B |
+-----------+-------------+
+----+-----------+-------------+-----------+
| Id | ProductID | ProductDec | SortOrder |
+----+-----------+-------------+-----------+
| 1 | 1 | Aero-pink | |
| 2 | 1 | Aero-white | |
| 3 | 1 | Aero-green | |
| 4 | 1 | Aero-Orange | |
| 5 | 2 | Baloon-1 | |
| 6 | 2 | Baloon-2 | |
| 7 | 2 | Baloon-3 | |
+----+-----------+-------------+-----------+
Now, what is the Sql code that can update 'sortOrder' column sequentially for each group of ProductID as shown below:
+----+-----------+-------------+-----------+
| Id | ProductID | ProductDec | SortOrder |
+----+-----------+-------------+-----------+
| 1 | 1 | Aero-pink | 1 |
| 2 | 1 | Aero-white | 2 |
| 3 | 1 | Aero-green | 3 |
| 4 | 1 | Aero-Orange | 4 |
| 5 | 2 | Baloon-1 | 1 |
| 6 | 2 | Baloon-2 | 2 |
| 7 | 2 | Baloon-3 | 3 |
+----+-----------+-------------+-----------+
Please note that these are sample tables, actual tables have thousands of records.
Would appreciate your help on this. Thank you
with cte
as
(
select SortOrder, row_number() over(partition by ProductID order by Id) as newPerProductOrder
from product_Desc
)
update cte
set SortOrder = newPerProductOrder
where (SortOrder <> newPerProductOrder or SortOrder is null)

SQL all rows into one column - no concatenation

I have a table that I need has multiple rows and I to put them all into a new table.
All my rows need to be converted into one row.
+-------+-----------+-------+--------+
| ID | Name | Last | Gender |
+-------+-----------+-------+--------+
| 1 | Person1 | Last1 | M |
| 2 | Person2 | Last2 | F |
| 3 | Person3 | Last3 | M |
| 4 | Person4 | Last4 | F |
+-------+-----------+-------+--------+
I need to convert the above table to the below:
+-------+------------+------------+
| NewID | ColumnName | Value |
+-------+------------+------------+
| 1 | ID | 1 |
| 1 | Name | Person1 |
| 1 | Last | Last1 |
| 1 | Gender | M |
| 2 | ID | 2 |
| 2 | Name | Person2 |
| 2 | Last | Last2 |
| 2 | Gender | F |
| 3 | ID | 3 |
| 3 | Name | Person3 |
| 3 | Last | Last3 |
| 3 | Gender | M |
| 4 | ID | 4 |
| 4 | Name | Person4 |
| 4 | Last | Last4 |
| 4 | Gender | F |
| | | |
+-------+------------+------------+
The most general method is to use union all:
select 'id' as columnname, cast(id as varchar(255)) as value from t union all
select 'name', name as value from t union all
select 'last', last as value from t union all
select 'gender', gender as value from t;
This should work in basically any database, although the cast to a string might vary. Some databases offer other solutions that are more efficient.
Union happy solution.
select 'id' as columnname, id as value from table
union all
select 'name' as columnname, name as value from table
union all
.e
.t
.c

Need to shift the data to next column, unfortunately added data in wrong column

I have a table test
+----+--+------+--+--+--------------+--+--------------+
| ID | | Name1 | | | Name2 |
+----+--+------+--+--+--------------+--+--------------+
| 1 | | Andy | | | NULL |
| 2 | | Kevin | | | NULL |
| 3 | | Phil | | | NULL |
| 4 | | Maria | | | NULL |
| 5 | | Jackson | | | NULL |
+----+--+------+--+--+----------+--+--
I am expecting output like
+----+--+------+--+--+----------+--
| ID | | Name1 | | | Name2 |
+----+--+------+--+--+----------+--
| 1 | | NULL | | | Andy |
| 2 | | NULL | | | Kevin |
| 3 | | NULL | | | Phil |
| 4 | | NULL | | | Maria |
| 5 | | NULL | | | Jackson |
+----+--+------+--+--+----------+--
I unfortunately inserted data in wrong column and now I want to shift the data to the next column.
You can use an UPDATE statement with no WHERE condition, to cover the entire table.
UPDATE test
SET Name2 = Name1,
Name1 = NULL

query after inner joining two tables

I have two tables TableA and TableB....
TableA
+----+-----------------+
| Id | ColName |
+----+-----------------+
| 1 | Name1 |
| 1 | Name2 |
| 1 | Name3 |
| 1 | Name4 |
| 1 | Name5 |
| 1 | Name6 |
| 1 | Name7 |
| 1 | Name8 |
+----+-----------------+
TableB
+----+-----------------+-----------------+-----------------+
| Id | ColName | CriticalityName | RefNo |
+----+-----------------+
| 1 | Name1 | High | RE1 |
| 1 | Name1 | Low | RE1,RE2 |
| 1 | Name1 | Low | RE1 |
| 1 | Name1 | Low | RE1 |
| 1 | Name1 | Low | RE1,RE2 |
| 1 | Name6 | High | RE3 |
| 1 | Name7 | High | RE3 |
| 1 | Name8 | High | RE1,RE#,RE3 |
| 1 | Name1 | High | RE1 |
| 1 | Name1 | High | RE4,RE5 |
| 1 | Name1 | High | RE1 |
| 1 | Name1 | High | RE1 |
| 1 | Name5 | High | RE1 |
| 1 | Name6 | High | RE1 |
| 1 | Name1 | High | RE1 |
| 1 | Name1 | High | RE3 |
| 1 | Name1 | High | RE2 |
| 1 | Name1 | High | RE4 |
| 1 | Name3 | High | RE5 |
| 1 | Name1 | Low | RE1 |
| 1 | Name1 | Low | RE1,RE#,RE3 |
| 1 | Name1 | High | RE5 |
| 1 | Name1 | Low | RE1,RE#,RE3 |
| 1 | Name8 | Low | RE4 |
+----+-----------------+-----------------+-----------------+
I need to filter using RefNo column, while inner joining two table TableA,TableB
I need a output as below:
+----+--------------+--------------+
| Id | ColName | RefNo |
+----+--------------+--------------+
| 1 | Name1 | RE1 |
| 1 | Name5 | RE1 |
| 1 | Name6 | RE1 |
| 1 | Name8 | RE1 |
+----+--------------+--------------+
here i'm inner joining TableA and TableB and filter with RefNo (passing RE1 as parameter).
In which ever row contains RE1, is displayed as output..
I dnt know to filter it after inner joining..
Please help me out..
Is this what you want?
select a.id, a.colname, 'RE1' as b.refno
from tableA a join
tableB b
on a.colname = b.colname and
',' + RefNo + ',' like '%,' + 'RE1' + ',%' ;
As a note: it is a really, really bad idea to store lists of things in a column. SQL offers a great data structure for storing lists of things, it is called a table and not a string column. In this case, you want a junction table, that has a separate row for each individual id (and/or ColName) and RefNo.