SQL query for counting rows with similar columns - sql

Now i have a table with 4 columns as below
seq col1 col2 col3
1 1 2 3
1 1 2 4
2 1 2 3
3 1 2 4
result should be as below
number of seq1 s which have ( col1 col2 col3) same
for the above example the output is expected as
count(seq) col1 col2 col3
2 1 2 3
2 1 2 4

Trust, this is what you require..
Select Count(seq) as countseq, col1, col2, col3 from <Table>
group by col1, col2, col3
Or if you have the columns to compare in Table2 then
Select A.col1, A.col2, A.col3, count(B.Seq) from
<Table2> as A inner join <Table> as B
on A.Col1 = B.Col1 and A.Col2=B.Col2 and A.Col3 = B.Col3
group by A.Col1, A.Col2, A.col3

Generally when you want to compare similar rows, you need to either select those columns as a result and build a checksum against them or select those rows in some form (concatenated or hash) and join the temp table on itself and select the result set on the remaining rows.
Your question does not have enough information to provide a more complete answer.

I think you want to compare col1,col2 and col3 and pick out the count of seq. I concatenated the values of col1,col2 and col3. I cast these columns to varchar and then replaced nulls by '-'. I guess this should help.
Select seq,
ISNULL(Cast(col1 as Varchar(5)),'-')+ISNULL(Cast(col2 as Varchar(5)),'-')+ISNULL(Cast(col3 as Varchar(5)),'-') as tempcol
into #temp from Table
Select Count(seq) from #temp group by tempcol

Related

Group by clause in Sql and filtering on an option,

I am working on a code, and the columns will something be like
Col1 Col2 Col3
10 1 A
10 2 B
11 3 C
11 4 c
So i am grouping by Col1 and whereever Col3 = A i need the whole information for that number of Col1. so in above example,i want the output as
Col1 col2 col3
10 1 A
10 2 B
Can someone please help.
Thanks in advance
You can use EXISTS and a correlating subquery to check if a row with the same col1 and a col3 of 'A' exists.
SELECT *
FROM elbat t1
WHERE EXISTS (SELECT *
FROM elbat t2
WHERE t2.col1 = t1.col1
AND t2.col3 = 'A');

Compare values in Different column and row

I have the following table:
ID COl1 COl2
1 13 15
2 13 16
3 13 17
4 17 13
What I need is to select all rows where Col1 value is available in Col2 and vice versa.
This case only ROW 4 or ROW 3 should be returned. They have same values (13 17).
Take it as col1 is Buyer and col2 is Seller
I want to know who are the users who bought / sell from EACH OTHER.
if user a bought from user b, user b should buy from user a in order to be returned.
SELECT
a.*
FROM
yourTable a
INNER JOIN
yourTable b
ON a.Col1 = b.Col2
AND a.Col2 = b.Col1
AND a.id != b.id
This can be done by using sub queries:
SELECT ID, COl1, COl2
FROM table1 WHERE COl1 IN (SELECT DISTINCT COl2 FROM table1)
UNION
SELECT ID, COl1, COl2
FROM table1 WHERE COl2 IN (SELECT DISTINCT COl1 FROM table1)
This sounds like exists:
select t.*
from t
where exists (select 1 from t t2 where t2.col1 = t.col2) and
exists (select 1 from t t2 where t2.col2 = t.col1) ;
If you want them in the same row, I would still use exists:
select t.*
from t
where exists (select 1 from t t2 where t2.col1 = t.col2 AND t2.col2 = t.col1) ;
I recommend this over a self-join because it will not generate multiple rows if there are multiple examples of the buyers and sellers on either side.
This also works
SELECT * FROM your_table WHERE
col1 IN (SELECT col2 FROM your_table)
AND
col2 IN (SELECT col1 FROM your_table);

How to get Distinct value for a column on the basis of other column in Oracle

I want to get the distinct values from COL1 and it's COL3 value also but the condition is if COL1 = COl2 then it should pick the matching COL3 value otherwise pick the COL1 value if they are not same. I'm stuck in the logic, any help will be appreciated!
Please see the below image for more detail:
select DISTINCT COL1,
CASE WHEN COL1 = COL2 THEN COL3 END COL3 from TABLE1
WHERE COL1 IS NOT NULL;
Do a GROUP BY to get distinct COL1 values.
Use COALESCE() to return the COL3 value if there exists a COL1 = COL2 row, otherwise return the max COL3 value for the COL1. (Could use MIN() too, if that's better.)
select COL1,
COALESCE( MAX(CASE WHEN COL1 = COL2 THEN COL3 END), MAX(COL3) )
FROM table1
WHERE COL1 IS NOT NULL
GROUP BY COL1
use correlated subquery
select col1,col3
from TABLE1 a
where col2 in (select min(col2) from table1 b where a.col1=b.col1)
select distinct COL1, if(COL1 = COL2, COL3, COL1) as result
from table1
I think that you can join the table with itself and then use a join conditio to filter that out, then decide in select wether there was COL2 = COL1 and choose appropriate COL3:
SELECT DISTINCT a.COL1, CASE WHEN b.COL1 IS NULL THEN a.COL3 ELSE b.COL3 END as COL3
FROM TABLE1 a
LEFT JOIN TBALE2 b
on a.COL1 = b.COL2
and a.COL1 = b.COL1
This way you have on table a all the data, and on table b data if and only if COL1 matches with COL2. Then you select whichever COL3 is not null, prefarably the one from table b. There is Oracle function coalesce that does just that.
With a self join:
select distinct
t.col1,
case
when tt.col1 is null then t.col3
else tt.col3
end col3
from tablename t left join tablename tt
on tt.col1 = t.col1 and tt.col2 = t.col1
See the demo.
Results:
> COL1 | COL3
> ---: | :---
> 11 | ABC
> 12 | ABC
> 13 | BDG
> 14 | DEF
> 15 | CEG

Postgres - does DISTINCT ON take preference for non-null values in column?

If I use DISTINCT ON on 2 columns, and there is a third column that can have null values, does DISTINCT ON always try to return a row where that third column is not null, or is it just down to the ORDER BY?
So for example with this table:
col1 col2 col3
1 88 8
1 88 9
1 88 1
2 88 3
2 88
3 88
I want to be able to SELECT DISTINCT ON (col1, col2) and get rows where col3 is not null, unless the DISTINCT ON (col1, col2) does not have a row where col3 is not null.
It is entirely based on what you ORDER BY. If you want to prefer rows with a non-NULL col3, just include that in your ordering:
SELECT DISTINCT ON (col1, col2) ... ORDER BY col1, col2, col3 ASC NULLS LAST.
select distinct col1, col2, col3
from table t
where t.col3 is not null
or not exists(select 1 from table tt
where tt.col1 = t.col1 and tt.col2 = t.col2 and tt.col3 is not NULL)

Removing rows in SQL that have a duplicate column value

I have looked high and low on SO for an answer over the last couple of hours (subqueries, CTE's, left-joins with derived tables) to this question but none of the solutions are really meeting my criteria..
I have a table with data like this :
COL1 COL2 COL3
1 A 0
2 A 1
3 A 1
4 B 0
5 B 0
6 B 0
7 B 0
8 B 1
Where column1 1 is the primary key and is an int. Column 2 is nvarchar(max) and column 3 is an int. I have determined that by using this query:
select name, COUNT(name) as 'count'
FROM [dbo].[AppConfig]
group by Name
having COUNT(name) > 3
I can return the total counts of "A, B and C" only if they have an occurrence of column C more than 3 times. I am now trying to remove all the rows that occur after the initial value of column 3. The sample table I provided would look like this now:
COL1 COL2 COL3
1 A 0
2 A 1
4 B 0
8 B 1
Could anyone assist me with this?
If all you want is the first row with a ColB-ColC combination, the following will do it:
select min(id) as id, colB, colC
from tbl
group by colB, colC
order by id
SQL Fiddle
This should work:
;WITH numbered_rows as (
SELECT
Col1,
Col2,
Col3,
ROW_NUMBER() OVER(PARTITION BY Col2, Col3 ORDER BY Col3) as row
FROM AppConfig)
SELECT
Col1,
Col2,
Col3
FROM numbered_rows
WHERE row = 1
SELECT DISTINCT MIN(COL1) AS COL1,COL2,COL3
FROM TABLE
GROUP BY COL2,COL3
ORDER BY COL1