How to grouping postgresql when id is same into new add value - sql

I have table like this:
And I would like to bring value item in one row when user_input_id and question_id is duplicate.
The result that I wish is this:
Can anyone tell me how to querying it?
Thank You

You can easily do taht with string_agg(value,',') in postgresql.
select user_input_id,question_id,
string_agg(value,',') as other_names
from table_name
group by user_input_id,question_id
order by user_input_id,question_id
Output:
You can also have array_aggr() in postgres:
select user_input_id,question_id,
array_agg(value::text ) as other_names
from table_name
group by user_input_id,question_id
order by user_input_id,question_id
Output:

Just fill a temp table with data of this table and truncate it. Then use aggregate functions like this:
CREATE TEMP TABLE temp_tbl(
columns ...
);
insert into temp_tbl
select * from main_tbl
truncate table main_tbl
insert into main_tbl
select user_input_id,question_id, string_agg(value, ',')
from temp_tbl
group by user_input_id,question_id

Related

How to rewite pandas to sql with drop_duplicates

I have the following code in pandas
pand = tb.sort_values(['created_at','id','name'], ascending = False).drop_duplicates(['id','name','day'])
So I rewrote it like this in SQL. However, the results of these two codes are different, can't get why.
(SELECT *,
ROW_NUMBER() OVER (
Partition by id,name,day
ORDER BY
created_at DESC,id DESC,name DESC
) as row_num
into new
FROM tb);
DELETE FROM new
WHERE row_num > 1;
```
In case you can afford DROP TABLE use an intermediate table:
CREATE TABLE tb_temp (LIKE tb);
INSERT INTO tb_temp
SELECT DISTINCT ON (id,name,day) * FROM tb;
DROP TABLE tb;
ALTER TABLE tb_temp RENAME TO tb;
Demo: db<>fiddle

Remove duplicates from table in bigquery

I found duplicates in my table by doing below query.
SELECT name, id, count(1) as count
FROM [myproject:dev.sample]
group by name, id
having count(1) > 1
Now i would like to remove these duplicates based on id and name by using DML statement but its showing '0 rows affected' message.
Am i missing something?
DELETE FROM PRD.GPBP WHERE
id not in(select id from [myproject:dev.sample] GROUP BY id) and
name not in (select name from [myproject:dev.sample] GROUP BY name)
I suggest, you create a new table without the duplicates. Drop your original table and rename the new table to original table.
You can find duplicates like below:
Create table new_table as
Select name, id, ...... , put our remaining 10 cols here
FROM(
SELECT *,
ROW_NUMBER() OVER(Partition by name , id Order by id) as rnk
FROM [myproject:dev.sample]
)a
WHERE rnk = 1;
Then drop the older table and rename new_table with old table name.
Below query (BigQuery Standard SQL) should be more optimal for de-duping like in your case
#standardSQL
SELECT AS VALUE ANY_VALUE(t)
FROM `myproject.dev.sample` AS t
GROUP BY name, id
If you run it from within UI - you can just set Write Preference to Overwrite Table and you are done
Or if you want you can use DML's INSERT to new table and then copy over original one
Meantime, the easiest way is as below (using DDL)
#standardSQL
CREATE OR REPLACE TABLE `myproject.dev.sample` AS
SELECT * FROM (
SELECT AS VALUE ANY_VALUE(t)
FROM `myproject.dev.sample` AS t
GROUP BY name, id
)

List all distinct values of column and their count

I have a column with different text values. How can I get a list of all the unique values and the count of the appearance of them in the column?
Simplest way is to use GROUP BY
select text_column, count(*) from text_table group by text_column
more info - http://www.w3schools.com/sql/sql_groupby.asp
SELECT column_name
, COUNT(*)
FROM table_name
GROUP BY column_name
;

How to sort values in columns and update table?

I'm completely new to sql and can't do that myself. So I need your help.
I want to sort values in column and then save changes. But I don't know how to do that.
Table looks like that:
Id | Name | SomeDescription
---------------------------
1 |Best | Description1
2 |Worth | Description2
3 |Good | Description3
I want to get something like that:
Id | Name | SomeDescription
---------------------------
1 |Best | Description1
2 |Good | Description3
3 |Worth | Description2
So I need to sort "id" and "name" columns.
I use following statement to sort values of "name" column:
SELECT * FROM games ORDER BY name ASC
But how can I sort the values of id column and save changes in table?
Please, help.
You would have to use a second table
create a new table games2 with the same structure as your games table, making sure the ID is auto-incrementing
CREATE TABLE `games2` LIKE `games`;
copy the data, sorted, into games2
INSERT INTO `games2` (`Name`, `SomeDescription`) SELECT `Name`, `SomeDescription` FROM `games` ORDER BY `Name`
drop or move the old table
-- DROP TABLE `games`;
-- or
RENAME TABLE `games` TO `games1`;
rename new table to old name
RENAME TABLE `games2` TO `games`;
These steps will result in what you want.
You can use the ROW_NUMBER ranking function to renumber the rows.
SELECT UnsortedId = id
, SortedId = ROW_NUMBER() OVER (ORDER BY g.name, g.id)
FROM games
You can use alter table command which is simpler.
mysql> ALTER TABLE games ORDER BY name asc;
That's all !
it is simple. just use a few codes. I have tried this and it is working. first create a temporary table while sorting values at the same time.
create table new as select * from games order by name;
and then drop the games table using,
drop table games;
now create games table again with same properties and data as in new (where sorting here is an optional) using,
create table games as select * from new order by name;
now drop the temp table 'new'
drop table new;
now check your table. it must be sorted out.
you can sort on two fields at the same time.
SELECT *
FROM games
ORDER BY id DESC, name ASC
I don't understand what you mean by save changes in the table. The ORDER BY, is just changing the display that you are looking at it doesn't change the data in the table unless you perform an UPDATE.
If you are unfamiliar with SQL, there are a lot of tutorials online that can help:
SQL Course
Beginner SQL Tutorial
SQL Tutorial - Learn SQL
Edit based on the comments, this can be done in one step:
create table #temp
(
id int,
name varchar(50),
somedesc varchar(50)
)
insert into #temp values (1, 'best', 'desc1')
insert into #temp values (2, 'worth', 'desc2')
insert into #temp values (3, 'good', 'desc3')
update t1
SET t1.id = t2.rn
, t1.somedesc = t1.somedesc
FROM #temp t1
JOIN
(
select id, name, somedesc, row_number() over(order by name, id) as rn
from #temp
) t2
on t1.id = t2.id
select *
from #temp
order by id
drop table #temp
This is my fix... stupid simple:
Extract to temp table
SELECT SortKeyValue (add any other fixed values, just don't include the existing sequence) INTO #Temp FROM SourceTable;
Empty the table:
TRUNCATE TABLE SourceTable
Reinsert:
INSERT INTO SourceTable (SortKeyValue, DisplayOrder (plus other fields))
SELECT SortKeyValue, ROW_NUMBER() OVER (ORDER BY SortKeyValue) AS DispOrd
(plus other fields) FROM #Temp;
Done!
SQL> SELECT * FROM games
ORDER BY ID,NAME;

How to keep only one row of a table, removing duplicate rows?

I have a table that has a lot of duplicates in the Name column. I'd
like to only keep one row for each.
The following lists the duplicates, but I don't know how to delete the
duplicates and just keep one:
SELECT name FROM members GROUP BY name HAVING COUNT(*) > 1;
Thank you.
See the following question: Deleting duplicate rows from a table.
The adapted accepted answer from there (which is my answer, so no "theft" here...):
You can do it in a simple way assuming you have a unique ID field: you can delete all records that are the same except for the ID, but don't have "the minimum ID" for their name.
Example query:
DELETE FROM members
WHERE ID NOT IN
(
SELECT MIN(ID)
FROM members
GROUP BY name
)
In case you don't have a unique index, my recommendation is to simply add an auto-incremental unique index. Mainly because it's good design, but also because it will allow you to run the query above.
It would probably be easier to select the unique ones into a new table, drop the old table, then rename the temp table to replace it.
#create a table with same schema as members
CREATE TABLE tmp (...);
#insert the unique records
INSERT INTO tmp SELECT * FROM members GROUP BY name;
#swap it in
RENAME TABLE members TO members_old, tmp TO members;
#drop the old one
DROP TABLE members_old;
We have a huge database where deleting duplicates is part of the regular maintenance process. We use DISTINCT to select the unique records then write them into a TEMPORARY TABLE. After TRUNCATE we write back the TEMPORARY data into the TABLE.
That is one way of doing it and works as a STORED PROCEDURE.
If we want to see first which rows you are about to delete. Then delete them.
with MYCTE as (
SELECT DuplicateKey1
,DuplicateKey2 --optional
,count(*) X
FROM MyTable
group by DuplicateKey1, DuplicateKey2
having count(*) > 1
)
SELECT E.*
FROM MyTable E
JOIN MYCTE cte
ON E.DuplicateKey1=cte.DuplicateKey1
AND E.DuplicateKey2=cte.DuplicateKey2
ORDER BY E.DuplicateKey1, E.DuplicateKey2, CreatedAt
Full example at http://developer.azurewebsites.net/2014/09/better-sql-group-by-find-duplicate-data/
You can join table with yourself by matched field and delete unmatching rows
DELETE t1 FROM table_name t1
LEFT JOIN tablename t2 ON t1.match_field = t2.match_field
WHERE t1.id <> t2.id;
delete dup row keep one
table has duplicate rows and may be some rows have no duplicate rows then it keep one rows if have duplicate or single in a table.
table has two column id and name if we have to remove duplicate name from table
and keep one. Its Work Fine at My end You have to Use this query.
DELETE FROM tablename
WHERE id NOT IN(
SELECT id FROM
(
SELECT MIN(id)AS id
FROM tablename
GROUP BY name HAVING
COUNT(*) > 1
)AS a )
AND id NOT IN(
(SELECT ids FROM
(
SELECT MIN(id)AS ids
FROM tablename
GROUP BY name HAVING
COUNT(*) =1
)AS a1
)
)
before delete table is below see the screenshot:
enter image description here
after delete table is below see the screenshot this query delete amit and akhil duplicate rows and keep one record (amit and akhil):
enter image description here
if you want to remove duplicate record from table.
CREATE TABLE tmp SELECT lastname, firstname, sex
FROM user_tbl;
GROUP BY (lastname, firstname);
DROP TABLE user_tbl;
ALTER TABLE tmp RENAME TO user_tbl;
show record
SELECT `page_url`,count(*) FROM wl_meta_tags GROUP BY page_url HAVING count(*) > 1
delete record
DELETE FROM wl_meta_tags
WHERE meta_id NOT IN( SELECT meta_id
FROM ( SELECT MIN(meta_id)AS meta_id FROM wl_meta_tags GROUP BY page_url HAVING COUNT(*) > 1 )AS a )
AND meta_id NOT IN( (SELECT ids FROM (
SELECT MIN(meta_id)AS ids FROM wl_meta_tags GROUP BY page_url HAVING COUNT(*) =1 )AS a1 ) )
Source url
WITH CTE AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY [emp_id] ORDER BY [emp_id]) AS Row, * FROM employee_salary
)
DELETE FROM CTE
WHERE ROW <> 1