SQL QUERY - Omit ALL duplicate results - sql

I need to return values in a column where only the unique values are returned. I know that DISTINCT will return only unique values, however i need to completely omit any that are duplicated.
i.e.
Column 1 Column 2
----------------------
123456789 27/02/2014
123456789 25/02/2014
654789897 27/02/2014
To return only "654789897 27/02/2014" and omit the other results.

You want to use group by and having:
select column1, column2
from table t
group by column1, column2
having count(*) = 1;
EDIT: (based on comment by knkarthick24)
Depending on what the OP intends, this might also be correct:
select column1, max(column2)
from table t
group by column1
having count(*) = 1;

select column1,column2
from tbl
where column1 in(
select column1
from table
group by column1 having count(column1)=1)
Its good to have Having and GroupBy
Let me know if that works:)

Related

Removing duplicates of column2 then group them based on column1 , then sum the values of column3 in sql

The table looks like
column1 column2 column3
400196 2021-07-06 33
400196 2021-07-06 33
400196 2021-08-16 33
I want to get the sum of column3 values based on grouping of column 1 but the duplicate values of date should not be added
The desired output is:
column1 column3
400196 66
The query I wrote is
select sum(column3)
from table_name
group by column1
But this gives me result 99
You can remove duplicate values in a subquery:
select t.column1, sum(t.column3)
from (select distinct t.column1, t.column2, t.column3
from t
) t
group by t.column1;
Note: This sort of problem can arise when you are joining tables together. Removing duplicates may not always be the right solution. Often it is better to do the calculation before joining, so you don't have duplicate values to deal with.
You could use a two step process here, first remove duplicates, then aggregate and sum:
SELECT column1, SUM(column3) AS column3
FROM (SELECT DISTINCT column1, column2, column3 FROM yourTable) t
GROUP BY column1;
Demo

SQL Server : how to find ids where columns have different values

I have a table like this:
Column1 Column2
---------------
1 1
1 2
1 3
1 4
2 1
2 1
2 1
2 1
In column1 one there are 2 different ids, in column2 there are different values for each id from column1.
How can I get the id from column1 where not all ids from column2 are the same? So in this instance the output should be 1 - because they have all different values in column2, where id from column1 has all 1's in column2
Just use group by and having:
select column1
from table t
group by column1
having min(column2) <> max(column2);
Note: you could also use count(distinct), but that has more overhead than min() and max().
Similar logic can be used if the second column could be NULL. That doesn't appear in the sample data so it doesn't seem worth including it in the logic unless the OP specifically says this is a possibility.
Try like this:
select Column1
from yourTable
group by Column1
having count(DISTINCT column2) > 1;
I would think something like this should do the job:
SELECT t.column1 FROM table t
GROUP BY t.column1
HAVING COUNT(DISTINCT t.column2) > 1
This approach will handle the case where a null is an acceptable value in column2.
select column1
from
(
select distinct column1, column2
from yourTable
) t
group by column1
having count(*) > 1

SQL: Find duplicated values associated with one ID

Thanks to NoDisplayName (SQL: Query to set value based on group parameter) for getting my table to have Primary tags. But now I need help with a query to find errors in my table.
Sample Input:
Column1 | Column2 |
ID1 Primary
ID1 Primary
ID1
ID2 Primary
ID2
ID3 Primary
ID3
Specifically what would a query to find if there is more than 1 Primary in Column2 associated with the same value in Column1?
I just need the output to be something actionable so I can then remove the duplicated Primary tags.
Thank you!
select
column1,
column2,
count(column1)
from tablename
groupby column1, column2
having count(column1) > 1
Or you could do this:
;WITH CTE AS
(SELECT Column1, Column 2, ROW_NUMBER() OVER (PARTITION BY Column1, Column2 ORDER BY Column1) AS 'DupsWillHave2'
FROM Foo)
SELECT *
FROM CTE
WHERE DupsWillHave2 > 1
I think the following is the query you want:
select column1, count(column1)
from tablename
where column2 = 'Primary'
group by column1
having count(*) > 1;
This will pick up only duplicate values of 'Primary'. It will not pick up duplicate blank values.

SQL Server Sum of column1 of all distinct values of column2

I am trying to find the SUM of column1 of all Distinct values of column2. Is it possible?
You could try something like this:
Select SUM(ColA), ColB
from table
Group by ColB
You almost wrote the query yourself in that sentence:
SELECT Column2, SUM(Column1) FROM Table GROUP BY Column2
It's not entirely clear what you're asking...
...this adds all the values in column 1 for each distinct value in column 2 then gives a total of all values in column 1:
SELECT Column2,SUM(Column1) FROM Table GROUP BY Column2 with rollup
Note: If you want the rollup at the top of the output put distinct in it.
SELECT distinct Column2,SUM(Column1) FROM Table GROUP BY Column2 with rollup

SQL Server Count Query with no order

I have a series of select count queries tied together by UNION egg
Select Count(Column1) From Table1 where Table1 column1 = 1
union
Select Count(Column2) From Table1 where Table1 column2 = 1
It works fine but it just orders in asc or desc order but I want it to go in order by which I requested, I want the first query to always be first in the result no matter what the value is. Thanks for any help.
Run two queries?
You can add a column and sort on it
Select 1 as sequence, Count(Column1) From Table1 where Table1 column1 = 1
union
Select 2 as sequence, Count(Column2) From Table1 where Table1 column2 = 1
ORDER BY sequence
Try this:
SELECT COUNT(*) AS cnt, 1 AS SortOrder FROM Table1 WHERE column1 = 1
UNION ALL
SELECT COUNT(*) AS cnt, 2 AS SortOrder FROM Table1 WHERE column2 = 1
ORDER BY SortOrder
The main change I have made is to add a column which you can use to ORDER BY. Some of the other changes I have made:
You don't mean UNION, you mean UNION ALL. Otherwise with your query if the counts were the same you'd only get one row. In the new query this wouldn't happen, but you should still use UNION ALL because that's semantically what you mean.
Writing COUNT(column1) is unnecessary because your WHERE clause guarantees that column1 can never be NULL. Use COUNT(*). I imagine that even if you write COUNT(column1) most databases will see that column1 cannot be NULL and omit the unnecessary NULL check, but again there is nothing wrong with being explicit - you want to count all rows and COUNT(*) makes that clear.
You shouldn't have Table1 column1 with a space between. There should be a dot. Or simply omit the table name as it is not required here.