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.
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 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.
I am looking for a way to display a table where a set of multiple attributes appear more than one time.
For example, suppose I had a table, Tbl1 with attributes A, B, C, D, E
How do I make a query such that it only shows rows where A, B, C appear more than once (as in the same A, B, C as a group), but D and E may or may not be different?
My attempt:
SELECT *
FROM Tbl1
WHERE COUNT(A, B, C) > 1
and I get an error: "group function is not allowed here"
The reason for this is, that you cannot use this grouping in the WHERE-part of an sql clause.
SELECT colums
FROM tables
WHERE condition
the condition refers to a single row of the table.
What you want is HAVING
SELECT colums
FROM tables
HAVING condition
The condition after HAVING is evaluated after the grouping and there you can use aggregation functions like COUNT or SUM
Use the GROUP BY clause (SQL Server: http://msdn.microsoft.com/en-us/library/ms177673.aspx, MySQL: http://www.tutorialspoint.com/mysql/mysql-group-by-clause.htm).
Within each group, you'll want to get get the count of rows in that group (using COUNT(*)) and then use a HAVING clause to filter on that count. HAVING is like a WHERE clause for GROUP BY. It filters on the results of the grouping, and can make reference to the grouped columns (in this case, A, B and C), or any aggregates (in this case, COUNT(*)).
Here's what your query could look like. Note that you can only include columns in the SELECT field list that are mentioned in the GROUP BY or that are contained in aggregate functions such as COUNT() and MAX(). MySQL will let you get away with putting other columns in, but SQL Server will give you an error. It's best to follow this rule even if the database allows it.
SELECT A,
B,
C,
COUNT(*) AS GroupCount
FROM Tbl1
GROUP BY A, B, C
HAVING COUNT(*) > 1
If you want the full rows where this is true, then you can used a derived table:
SELECT *
FROM Tbl1
JOIN (
SELECT A,
B,
C,
COUNT(*) AS GroupCount
FROM Tbl1
GROUP BY A, B, C
HAVING COUNT(*) > 1
) AS duplicates
ON duplicates.A = Tbl1.A AND
duplicates.B = Tbl1.B AND
duplicates.C = Tbl1.C
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