I would like to dedup rows with case insensitive values.
original table:
| ID | Name |
| ---| -------------- |
| 1 | Apple |
| 2 | Banana |
| 1 | apple |
desired output after deduping (keep the lowercase):
| ID | Name |
| ---| -------------- |
| 2 | Banana |
| 1 | apple |
Following statement only works for case sensitive match.
create table DELETE2 as select distinct * from DELETE1;
drop table DELETE1;
alter table DELETE2 rename to DELETE1;
I tried to use following statement, but it did not work.
ALTER SESSION SET QUOTED_IDENTIFIERS_IGNORE_CASE = TRUE;
Thank you!
knozawa
You could group by lower(x):
select id, max(name) name
from table
group by 1, lower(name)
Related
There is the following table structure in sqlite:
==========================
| Id | Name | Date |
==========================
| 1 | Foo | 2021-01-01 |
--------------------------
| 2 | Foo | 2021-01-02 |
--------------------------
| 3 | Foo | 2021-01-03 |
--------------------------
| 4 | Bar | 2021-01-01 |
--------------------------
| 5 | Bar | 2021-01-02 |
==========================
and I need to implement the following logic in one query (pseudocode):
$names = 'SELECT name FROM MyTable'
for ($name in $names) {
$fields = 'SELECT * FROM MyTable WHERE name = :name'
$id = getIdWithBiggestDate($fields)
}
As a result, I should get something like this (items with the unique names and maximum dates):
==========================
| Id | Name | Date |
==========================
| 3 | Foo | 2021-01-03 |
--------------------------
| 5 | Bar | 2021-01-02 |
==========================
Is it possible to do this in one query?
In SQLite you can do it with this non-standard SQL query:
SELECT Id, Name, MAX(Date) AS Date
FROM MyTable
GROUP BY Name
This works in SQLite because a column like Id which does not appear in the group by clause and also it is not aggregated (bare column) is returned from the row that contains the max Date.
See the demo.
select distinct Name,Date
from MyTable
where
(Name,Date) in (select Name,max(Date) from MyTable group by Name)
I have a table that looks like this:
+--------+-----------+------------+-----------+
| Group# | Person A | Person B | Person C |
+--------+-----------+------------+-----------+
| 1 | yes | no | no |
| 2 | no | yes | yes |
| 3 | yes | yes | yes |
I want to use a SQL query on this data that will return the Group# in one column and the column header in the second column when the value = yes. The result I want would look like this for the above table:
+-----------+----------+
| Group# | Person |
+-----------+----------+
| 1 | Person A |
| 2 | Person B |
| 2 | Person C |
| 3 | Person A |
| 3 | Person B |
| 3 | Person C |
+-----------+----------+
*Note that in contrast to my example, my actual data has many more columns than rows.
Thank you.
In my opinion, the best approach is a lateral join. But the most general method is simply union all:
select group#, 'personA' as person
from t
where personA = 'yes'
union all
select group#, 'personB' as person
from t
where personB = 'yes'
union all
select group#, 'personC' as person
from t
where personC = 'yes';
In answer to your next question . . . yes, you have to explicitly list the columns. However, you can use a SQL query on the metadata tables to generate the query you really want. And then execute that query.
I want to know if there is a way to remove duplicate values from a table. The key 'distinct' will fetch us the unique rows however if one value differs in a column, it wont. so just wanted to know if this can be achieved by any means. Hope the below example will help.
For example : In the below table there are two entries for Emp_ID 1234 with two different priorities. my output should consider the higher priority row alone. Is it possible?
My table
+---------+------+--------+-------+
| Employee_ID| priority | gender |
+------------+-----------+--------+
| 1234 | 1 | F |
| 1234 | 10 | F |
| 5678 | 2 | M |
| 5678 | 25 | M |
| 9101 | 45 | F |
+------------+-----------+--------+
Output
+---------+------+--------+-------+
| Employee_ID| priority | gender |
+------------+-----------+--------+
| 1234 | 1 | F |
| 5678 | 2 | M |
| 9101 | 45 | F |
+------------+-----------+--------+
DELETE
FROM Table t
WHERE EXISTS ( SELECT Employee_ID FROM Table WHERE Employee_ID = t.Employee_ID AND priority < t.Priority)
That is if you really want to remove them from the table. The Exists part can also be used in a select query to leave the values in the Original table.
SELECT *
FROM Table t
WHERE NOT EXISTS (SELECT Employee_ID FROM Table WHERE Employee_ID = t.Employee_ID AND priority > t.Priority)
select Employee_ID,max(priority) as priority,gender
from table
group by Employee_ID,gender
------------------
| ID | Other |
------------------
| 1 | 1 |
------------------
| 1 | 2 |
------------------
| 1 | 3 |
------------------
| 2 | 1 |
------------------
| 2 | 2 |
------------------
| 2 | 3 |
------------------
How can I write a query which groups by 'ID' and then UNIONS the 'Other' (so that I can determine which 'ID's have the same 'Other' field)?
I also want to be able to keep track of how many each 'Other's each of the 'ID's have. Is there a way to do this?
I think that with the term : UNIONS you really mean CONCATENATING others values, if so i depends on the database server you have, for example MySQL have GROUP_CONCAT :
SELECT ID, GROUP_CONCAT(Other) FROM table GROUP BY ID
Try running this query:
SELECT *, SUM(Other)
FROM tableName
GROUP BY ID
Short version: I need to return a query with 3 items from another table and adding it to the existing table.
Long version:
Table A contains the following information:
| ID | Name | Date | Comment |
--------------------------------
| 1 | AJ | 9/11 | Howdy |
| 2 | AW | 9/13 | Hi |
| 3 | AK | 9/15 | Aloha |
| 4 | AW | 9/15 | Hello |
| 5 | AJ | 9/18 | Greetings |
I need Table B to resemble:
| ID | Comment | Comment2 | Comment3 |
--------------------------------------------
| 1 | Howdy | Aloha | Greetings |
I am running
SELECT TOP 3 *
FROM a
WHERE Name IN ('AJ','AK')
but that makes Table B appear like:
| ID | Name | Date | Comment |
--------------------------------
| 1 | AJ | 9/11 | Howdy |
| 3 | AK | 9/15 | Aloha |
| 5 | AJ | 9/18 | Greetings |
Is it even possible to get what I want?
Not entirely sure what you are after as you have id's for each comment, then your output has a single row with an id (where does this id come from for your output?) but this may be able to be expanded upon:
SELECT
[1] AS COMMENT1,
[2] AS COMMENT2,
[3] AS COMMENT3
FROM
TABLE_A
PIVOT (MAX(COMMENT) FOR id IN ([1],[2],[3])) AS PVT
Please try this , it helpful to you
select b.id, b.comment as comment
, (select comment from ##temp1 where id = b.id+2 ) as comment1
, (select comment from ##temp1 where id = b.id+4 ) as comment2
from ##temp1 b where b.id=1
I don't know if this will work, but i'm just throwing the idea here. Let me know if it works!
Insert into B Values (
( SELECT TOP 3 comment FROM a WHERE Name IN ('AJ','AK') limit 1 ),
( SELECT TOP 3 comment FROM a WHERE Name IN ('AJ','AK') limit 1,1 ),
( SELECT TOP 3 comment FROM a WHERE Name IN ('AJ','AK') limit 2,1 )
)
This is for MySQL. please change accordingly for MSSQL Server