How to loop through selected data in SQL Server? [duplicate] - sql

This question already has answers here:
Insert into ... values ( SELECT ... FROM ... )
(27 answers)
Closed 4 years ago.
I want to select multiple rows of data from a table and add it onto another one.
For example:
Select * from Table1
which would return
id | name
1 | Chad
2 | Mary
3 | Denise
I want to add these rows of data to Table 2
Insert(id, name)
values(#id, #name)
Thank you!

INSERT INTO Table2(id,name)
SELECT t.id,t.name
FROM Table1 t
;

Related

string_agg show info in columns [duplicate]

This question already has answers here:
Split comma separated column data into additional columns
(3 answers)
Closed 4 years ago.
I'm using the function string_agg to show together some values of the same ID, and it works well because it shows the info concatenated:
SELECT string_agg(table1.value , '#BR#') as "values"
FROM table1 WHERE id=1
id | values
--------------
1 | v1#BR#v2#BR#v3#BR#v4#BR#v5#BR#v6
Is there a way I can show the info on separated columns? Something like this:
id | values | values | values | values | values | values |
----------------------------------------------------------
1 | v1 | v2 | v3 | v4 | v5 | v6
If you can define a sensible upper limit for the number of columns you could use something like this:
select id,
v[1] as value_1,
v[2] as value_2,
v[3] as value_3,
v[4] as value_4,
v[5] as value_5
from (
select id, array_agg(value) as v
from table1
where id = 1
) t;
If you expect more columns, just add more expressions to the outer SELECT list
Note that accessing array elements that don't exists will not result in an error, it will only yield NULL. So you can create any number of columns you like even if you know that there won't be values for all of them all the time.

How to remove duplicate rows in postgresql? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I would like to remove duplicate entries in Postgresql.
There is no unique constraint, but I would like to consider all columns together to consider a row as a duplicate.
So we have a table containing following rows :
id | name | age | started_date |Score |
-----|----------|---------------|---------------|------|
1 | tom | 15 | 01/06/2022 |5 |
2 | tom | 15 | 01/06/2022 |5 |
3 | henry | 10 | 01/06/2022 |4 |
4 | john | 11 | 01/06/2022 |6 |
...
I would like to consider all columns together to identify the duplicate rows.
How to achieve this in Postgresql ?
PostgreSQL assigns a ctid pseudo-column to identify the physical location of each row. You could use that to identify different rows with the same values:
-- Create the table
CREATE TABLE my_table (num1 NUMERIC, num2 NUMERIC);
-- Create duplicate data
INSERT INTO my_table VALUES (1, 2);
INSERT INTO my_table VALUES (1, 2);
-- Remove duplicates
DELETE FROM my_table
WHERE ctid IN (SELECT ctid
FROM (SELECT ctid,
ROW_NUMBER() OVER (
PARTITION BY num1, num2) AS rn
FROM my_table) t
WHERE rn > 1);
DB Fiddle
Let say your table has 2 columns, you can identify duplicates using.
Post this :-
1) Insert this result into a temp table
2) Drop data from Main table
3) Insert data from temp table into main table
4) Drop temp table.
select col1, col2, count(*) as cnt
from table1
group by col1, col2
having cnt > 1

Get row values as new columns [duplicate]

This question already has answers here:
Create a pivot table with PostgreSQL
(3 answers)
Closed 8 years ago.
I was wondering if it would be possible to get all values of rows with the same ID and present them as new columns, via a query.
For example, if I have the following table:
ID | VALUE
1 | a
1 | b
1 | c
2 | a
2 | b
[...]
I want to present it as:
ID | VALUE1 | VALUE2 | VALUE3 [...]
1 | a | b | c
2 | a | b | -
Thank you for any help
A query wouldn't do it. Unless you do 3 seperate querys.
SELECT ID,VALUE1 FROM Table
SELECT ID,VALUE2 FROM Table
ect...
If you have a problem with your database values not being recursive, then i would set up your table differently.
ID | VALUE
1 | a
1 | b
1 | c
2 | a
2 | b
[...]
You should set up the Table atributes like that rather than your first table.
if you are going to set up your tables differently I would do insert Statements.
INSERT INTO newTable (ID, VALUE)
SELECT ID,VALUE1 FROM oldTable
INSERT INTO newTable (ID, VALUE)
SELECT ID,VALUE2 FROM oldTable
ect..
Another possible way to do it is to display it in your application. Take php for instance.
foreach($sqlArray as $var){
echo $var['id'] ' | ' $var['value1']
echo $var['id'] ' | ' $var['value2']
echo $var['id'] ' | ' $var['value3']
}

Add counter field in select result in Access Query [duplicate]

This question already has an answer here:
Access query producing results like ROW_NUMBER() in T-SQL
(1 answer)
Closed 8 years ago.
I have a table with some field.
name, stud_id
ali | 100
has | 230
mah | 300
I want to get some of record and show a row field beside of record.
1 | ali | 100
2 | has | 230
3 | mah | 300
How I can do it.
Thanks.
Select (select count(*) from Table1 A where A.stud_id>=B.stud_id) as RowNo, B.*
from Table1 as B
order by A.stud_id
MS-ACCESS does not have rownum function, so this might help you.
But your ID need to be sortable and unique.

Partially distinct select [duplicate]

This question already has answers here:
Select first row in each GROUP BY group?
(20 answers)
Closed 8 years ago.
I have a table with fields utm and tracking_id. There may be many tracking_ids for each utm.
So I need to select distinct utm and any one tracking id for each utm (let`s say thefirst one).
For example, we got the table:
utm | tracking_id
-------------------
ddff | 1
ddff | 2
llzz | 3
ddff | 4
ohoh | 5
ohoh | 6
And as an output i want to get:
utm | tracking_id
-------------------
ddff | 1
llzz | 3
ohoh | 5
I use PostgreSQL 9.1.
Is there a way to do it with SQL?
select utm, min(tracking_id)
from t
group by utm
if you have only one column, than simple aggregate is what you want, go with Clodoaldo Neto's advice.
If you have more than one columns, you can use dictinst on syntax:
select distinct on(utm)
utm, tracking_id, column1, column2, ...
from t
order by utm, tracking_id
sql fiddle demo