I am trying to select * from an oracle table, but only where user_id are unique.
i tried this:
select distinct user_id from users; -- which worked
i want to display EVERYTHING, so when i put:
select distinct user_id, * from users; -- i get a syntax error
how can i accomplish his?
select distinct user_id, users.* from users;
select * from users where users.primary_key IN
(select primary_key FROM users GROUP BY user_id HAVING count(*) = 1)
This will only select records that do not share user_ids with other rows.
Related
Running something like this:
SELECT user_id, username FROM `table` GROUP BY user_id HAVING COUNT(user_id) = 1
But BQ console complains that username is neither grouped nor aggregated. I'm looking at this post that explains how to remove rows that appears more than once. I'm assuming this error message is because there's no primary key or uniques in BQ? How can I get around this? I just want to eliminate repeated rows by user_id.
I just want to eliminate repeated rows by user_id.
below should do
SELECT user_id, ANY_VALUE(username) as username
FROM `table`
GROUP BY user_id
If you want one row per user_id, you can just use an aggregation function such as:
SELECT user_id, MAX(username) as username
FROM `table`
GROUP BY user_id
HAVING COUNT(user_id) = 1;
However, I might suggest using QUALIFY instead:
select t.*
from table t
where 1=1
qualify count(*) over (partition by user_id) = 1;
Good morning
I tried and tried to understand why this Query gives the usual error on Group By. I would like to find the duplicate lines and delete them. I found this query on Microsoft's MSDN but despite this it keeps giving me this error on Group By.
The main table has 3 fields "Id, Item, Description", the table name is "tlbDescription", this query should in theory create a table named "duplicate_table" insert the duplicate values inside the "duplicate_table", then delete the values from table "tlbDescription" and finally delete the table "duplicate_table".
If someone can kindly give me a hand
Thank you
Fabrizio
This is the query:
SELECT DISTINCT *
INTO duplicate_table
FROM [tlbDescrizione]
GROUP BY [Articolo]
HAVING COUNT([Articolo]) > 1
DELETE [tlbDescrizione]
WHERE [Articolo] IN (SELECT [Articolo] FROM duplicate_table)
INSERT [tlbDescrizione]
SELECT * FROM duplicate_table
DROP TABLE duplicate_table
This query doesn't make sense:
SELECT DISTINCT *
INTO duplicate_table
FROM [tlbDescrizione]
GROUP BY [Articolo]
HAVING COUNT([Articolo]) > 1;
It is selecting all columns but is an aggregation query because of the GROUP BY. Hence, the SELECT columns are inconsistent with the GROUP BY columns and you get an error.
If you want all the columns then you can use window functions:
SELECT DISTINCT *
INTO duplicate_table
FROM (SELECT d.*, COUNT(*) OVER (PARTITION BY d.Articolo) as cnt
FROM tlbDescrizione d
) d
WHERE cnt > 1;
Or, if you want only the ids:
SELECT Articolo
INTO duplicate_table
FROM tlbDescrizione
GROUP BY [Articolo]
HAVING COUNT(*) > 1;
I have two columns (user_from and user_to) and I need to know how many different users appears in my database. What is a good and fast way to do that?
I'm using PostgreSQL, btw.
select distinct tmp.UserName from
(
select distinct user_from as UserName from YourTable
union
select distinct user_To as UserName from YourTable
) as tmp;
This query is quite sufficient to get the list of users:
select user_from as UserName
from t
union -- intentional to remove duplicates
select user_To as UserName
from t;
If you want the count, then:
select count(*)
from (select user_from as UserName
from t
union
select user_To as UserName
from t
) t;
I need to remove duplicates from my table (user_info). I always want to remove the row with the id column that is lower of the two rows being returned from my select/having query below. Any ideas on how to write the delete statement to remove the duplicates (lower user_info.id column) from the results of my select/having query below? I'm using Oracle 11g.
user_info table structure:
id (unique primary key number 10 generated by sequence)
user_id (number 10)
first_name (varchar2)
last_name (varchar2)
data example:
id user_id
______ ___________
37265 1455
265798 1455
sql to show duplicates:
select user_id, count(*)
from user_info
group by user_id
HAVING count(*) > 1
You can use the following query:
DELETE
FROM user_info
WHERE id NOT IN
(SELECT MAX(id)
FROM user_info
GROUP BY user_id);
This query will delete all the duplicate rows except the user_id row with maximum id.
Here's a SQL Fiddle which demonstrates the delete.
Start with this to show you only the duplicates
Select user_id, count(*) NumRows, Min(Id) SmallestId, Max(Id) LargestId
From user_info
Group by user_id
HAVING count(*) > 1
This will show you the min and max for each user_id (with the same value for SmallestId and LargestId if there are no duplicates.
Select user_id, count(*) NumRows, Min(Id) SmallestId, Max(Id) LargestId
From user_info
Group by user_id
For a User, you want to keep the MaxId and Delete everything else. So you can write a DELETE statement to be
DELETE From user_info
Where Id Not IN
(
Select Max(Id)
From user_info
Group by user_id
)
This will get the
drop table test;
/
create table test
(
ids number,
user_id number
);
/
insert into test
values(37265,1455);
/
insert into test
values(265798,1455);
/
select * from test;
delete from test t
where t.ids < (select max(ids) from test t1 where T1.USER_ID= T.USER_ID)
This query employs the sub query to do the same !
I save user logins in a table that named loginstats, I want to retrieve last login of every users, I use these code but I meet some error, What is my mistake?
select *
from loginStats
where id in (
select distinct username, MAX(id) as id
from loginStats
group by username)
You are doing id IN, but are trying to compare it to multiple columns. Try this instead:
SELECT A.*
FROM LoginStats A
INNER JOIN (SELECT DISTINCT username, MAX(id) as id
FROM loginStats
GROUP BY username) B
ON A.username = B.username AND A.id = B.id
select *
from loginStats
where id in (
select distinct MAX(id) as id
from loginStats
group by username)
You can't have multiple field outputs in your IN subquery.
Or you could also do this like this:
;WITH CTE
AS
(
SELECT
RANK() OVER(
PARTITION BY loginStats.username
ORDER BY loginStats.id DESC
) AS iRank,
loginStats.*
FROM
loginStats
)
SELECT
*
FROM
CTE
WHERE
CTE.iRank=1
Here is some information how to use the rank function and how it is applied.
Here is some information on msdn about the rank function.
Here is some information about cte function and usage
Here is some information about With clause and how it is used
Hope it helps you understand
The mistake is that in the IN clause only one column can be specified and you are specifying two: username and id.
You're selecting two fields, where you should only return one. This correlated subquery should work for you:
select *
from loginStats AS a
where id = (
select MAX(id) as id
from loginStats AS b
where b.username = a.username)