I would like to do something like this , but getting an error please suggest some good methods?
select A,B,C, count(Distinct A,B,C)
from table_name
group by A,B,C
having count(Distinct A,B,C) > 1
Basically i have an index on the columns(A,B,C), and some rows doesnt have this unique combination set, So I'm trying a query similar to identify the rows which disobeys the unique constraint. PLease let me know if there is a best way
If you group by these columns then you already only get those unique records and then you can use count(*) to get how many duplicates you have
select A,B,C, count(*)
from table_name
group by A,B,C
HAVING count(*) > 1
What #jurgend said is right, and you can further find the exact rows (I'm assuming there are more fields to look at, including maybe a PK) by doing
SELECT *
FROM table_name
WHERE (A,B,C) IN (
SELECT A, B, C
FROM table_name
GROUP BY A, B, C
HAVING COUNT(*) > 1
)
A Tuple IN list query works in Oracle, although not all other DBMS.
Related
I have an SQL view, and I want to get unique rows based on only some columns.
If all other columns are the same (excluding A), duplicates must be deleted.
and
If all other columns are the same (excluding B), duplicates must be deleted.
I used the code below, but it still gives me the max of A then the Max of B, and what I want is to get distinct rows excluding these two columns, and I want to keep these columns in the select function.
SELECT Name, Account, MAX(A), MAX(B)
FROM MyTable
GROUP BY Name, Account
Thanks in advance!
Have not tested, but is this what you are searching for?
select sub.Name,sub.Account,MAX(sub.prod)
from
(
select Name,Account,MAX(A) as prod from MyTable group by Name,Account
union all
select Name,Account,MAX(B) as prod from MyTable group by Name,Account
) as sub
group by sub.Name, sub.Account
I have a permanent problem,
lets assume that I have a following columns:
T:A(PK), B, C, D, E
Now,
select A, MAX(B) from T group BY A
No, I cant do:
select A, C, MAX(B) from T group BY A
I don't understand why - when in comes to AVG or SUM I get it. However, MAX or MIN is getting from exactly one row.
How to deal with it?
You can use ROW_NUMBER() for that like this:
select A, C, B
from (
select *
, row_number() over (partition by A order by B desc) seq
-- group by ^ max(^)
from yourTable ) t
where seq = 1;
That's cause columns included in the select list should also be part of group by clause. You may have column which re part of group by but not present in select list but vice-versa not possible.
You generally, put only those columns in select clause on which you want the grouping to happen.
try this. it can help you find the MAX by just 1 column (f1), and also adding the column you wanted(f3) but not affecting your MAX operation
SELECT m.f1,s.f2,m.maxf3 FROM
(SELECT f1,max(f3) maxf3 FROM t1 GROUP BY f1) m
CROSS APPLY (SELECT TOP(1) f2,f1 FROM t1 WHERE m.f1 = f1) s
Your question isn't very clear in that we aren't sure what you are trying to do.
Assuming you don't actually want to do a group by in your main query but want to return the max of B based on column A you can do it like so.
select A, C,(Select Max(B) from T as T2 WHERE T.A = T2.A) as MaxB from T
I have an oracle database table with a lot of columns. I'd like to count the number of fully unique rows. The only thing I could find is:
SELECT COUNT(DISTINCT col_name) FROM table;
This however would require me listing all the columns and I haven't been able to come up with syntax that will do that for me. I'm guessing the reason for that is that this query would be very low performance? Is there a recommended way of doing this?
How about
SELECT COUNT(*)
FROM (SELECT DISTINCT * FROM Table)
It depends on what you are trying to accomplish.
To get a count of the distinct rows by specific column, so that you know what data exists, and how many of that distinct data there are:
SELECT DISTINCT
A_CODE, COUNT(*)
FROM MY_ARCHV
GROUP BY A_CODE
--This informs me there are 93 unique codes, and how many of each of those codes there are.
Another method
--How to count how many of a type value exists in an oracle table:
select A_CDE, --the value you need to count
count(*) as numInstances --how many of each value
from A_ARCH -- the table where it resides
group by A_CDE -- sorting method
Either way, you get something that looks like this:
A_CODE Count(*)
1603 32
1600 2
1605 14
I think you want a count of all distinct rows from a table like this
select count(1) as c
from (
select distinct *
from tbl
) distinct_tbl;
SELECT DISTINCT **col_name**, count(*) FROM **table_name** group by **col_name**
How can I get only non-unique values from 2 columns table:
I need something like
Select a,b from tableA group by a,b having count(a,b)>1
Now of cource it doesn't because of count.
I've forgot how can I do it and maybe thank to you I get known new simply solution :)
Best regards
this should work.
Select a,b from tableA group by a,b having count(*)>1
count cannot have multiple fields in it. either star which indicates everything or a single field which basically does the same thing as star unless that field's value is NULL in which case it is not counted.
SELECT a, b, COUNT(*) FROM tableA GROUP BY a, b HAVING COUNT(*) > 1
I've included the COUNT(*) so you can see how many there are for each case.
Hi I have a table of deals, I need to return the entire table but I need the title and the price to be distinct, as there is quite a few double ups, I've put in an example scenario below
Col ID || Col Title || Col Price || Col Source
a b c d
a b c b
b a a c
b a a 1
Expected result:
a b c d
b a a c
I'm not sure whether or not to use distinct or group by here, any suggestions would be appreciated
Cheers
Scott
=======================
Looking at some of your suggestions I'm going to have to rethink this, Thanks guys
This will arbitrarily pick one of the rows for each distinct (price,title) pair
;WITH myCTE AS
(
SELECT
*,
ROWNUMBER() OVER (PARTITION BY Price, Title ORDER BY Source) AS rn
FROM
MyTable
)
SELECT
*
FROM
myCTE
WHERE
rn = 1
You can use group by, but to return only title and price, ID and source would have to be ignored
You are asking for entire table but in your sample output you have lost two Records and thus losing the value of 'Col Source'.
a b c b
b a a 1
Group By will help you write very simple query
select id, title, price, source from table group by title, price
A DISTINCT and GROUP BY usually generate the same query plan, so performance should be the same across both query constructs. GROUP BY should be used to apply aggregate operators to each group. If all you need is to remove duplicates then use DISTINCT. If you are using sub-queries execution plan for that query varies so in that case you need to check the execution plan before making decision of which is faster.
You should go for the GROUP BY as the entire columns required in your resultset. However, the DISTINCT will return only unique list of specific column.
SELECT ID, Title, Price, Source
FROM table as t
GROUP BY Title, Price