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

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

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

Subtract 2 rows using case statement in SQL Server 2008

My data is like below, it's in a single table
Column1 Column2
abc 100
abc 200
Now I need like below
abc 100 //here 200-100
I am banging my head on how to achieve this.
I have tried to use the row_number and then subtract using case statement like
Select
column1,
sum(
case when rownum=1
then column2
end
-
case when rownum=2
then column2
end
)
from table
group by column1
But this is giving me null.
Assuming there is no attribute which can define row ordering -
;with cte as(
select
row_number() over (order by (select null)) as IndexId,
Column1,
Column2
from #xyz
)
select sum(case when IndexID=1 then (-1 * Column2) else Column2 end), Column1
from cte
group by Column1
Input data-
declare #xyz table(Column1 varchar(10),Column2 int)
insert into #xyz
select 'abc' ,100 union all
select 'abc' ,200
Assuming you have an attribute rownum in table which is always 1 or 2 (it can be generated by some row_number() as you suggest in question, according to any order that is suitable for you)
Column1 Column2 Rownum
------------------------
abc 100 1
abc 200 2
then you can simply use
Select
column1,
sum(
case when rownum=1
then column2
else -column2
end
)
from table
group by column1
It performs a sum of the Column2 per Column1, however, in the row having rownum = 2 the Column2 value is negated. Therefore in our example you end up with 100 + (-200) = -100
You could do:
select column1, max(column2) - min(column2)
from t
group by column1;
Here is a short form of the answer above if you care:
SELECT
column1,
SUM(IIF(rownum=1,column2,-column2))
FROM table
GROUP BY column1

Get multiple records only using SELECT DISTINCT or similar

I have records like this:
Column1 Column2
A Blue
A Blue
B Red
B Green
C Blue
C Red
Using SELECT DISTINCT I get this:
Column1 Column2
A Blue
B Red
B Green
C Blue
C Red
What I'd like to get:
Column1 Column2
B Red
B Green
C Blue
C Red
So I need to get only multiple records of column1 that have different values on column2.
(I'm joining two tables)
With SELECT DISTINCT, I got closer to what I need, but I can't find a way to exclude records like "A" on column1 that have always the same value on column2...
Try this:
SELECT * FROM yourtable
WHERE Column1 IN
(SELECT Column1
FROM yourtable
GROUP BY Column1
HAVING COUNT(DISTINCT Column2) > 1
)
The DISTINCT in COUNT ensures that you only get those records where Column2 has multiple distinct values.
I think this code will work in most systems:
SELECT Col1,Col2
FROM tbl
GROUP BY Col1,Col2
HAVING COUNT(*)<=1
See the results: http://sqlfiddle.com/#!6/47285/8/0
Assuming you're using MS SQL Server....that's not how DISTINCT works, DISTINCT is applied across the whole list of Columns in the SELECT statement, that's why you're getting the output you mentioned. Try using a combination of GROUP BY and HAVING like below...
SELECT Column1, Column2 FROM [table_name]
GROUP BY Column1, Column2
HAVING COUNT(*) < 2
ORDER BY Column1
Try the following Query :
SELECT Col1,Col2
FROM tbl
GROUP BY Col1,Col2
HAVING COUNT(Col1)<=1
You can use like this
select a.Column1 ,count(a.Column1 )
from
(select Distinct Column1 ,Column2
from Items) a
group by a.Column1
having Count(a.Column1 ) > 1

SQL QUERY - Omit ALL duplicate results

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:)

how to write procedure for the following in sql

i have a tabel with columns as so..
column1 column2 column3 column4 column5
i want the datas to fill like below in column two..
where colum1,3,4,5...user will give the values...
in column2 its values should be auto sequencing like as follows...
column1 column2 column3 column4 column5
1 1 one one one
1 2 two two two
1 3 three three three
2 1 one one one
2 2 two two two
2 3 three three three
3 1 one one one
3 2 two two two
3 3 three three three
Folks kindly Give ur Suggestions...Thankxxx!!! cya!
It seems you want to give each row a distinct value for equal values in column1. This can be done with a windowing function:
SELECT column1,
row_number() over (partition by column1) as column
FROM some_table
I do not understand what you want to have in columns3, 4 and 5
Depending on your platform, the following May work...
UPDATE
<your-table>
SET
column2 = ROW_NUMBER() OVER (PARTITION BY column1)
EDIT
As Martin says in his comment...
WITH
windowed_table
AS
(
SELECT
column2,
ROW_NUMBER() OVER (PARTITION BY column1 ORDER BY column1) AS row_number
FROM
my_table
)
UPDATE
windowed_table
SET
column2 = row_number