compare two fields of a single table in oracle sql - sql

I have a table like this, I need to write a sql query to compare and validate the records are in proper order. ex: dhoni is having no1 in column2 like that each players have their own no's , I need to write a code to validate that no player has the same no's assigned for more than one player. and need to check weather the player has his assigned no only.
+-------+-------+
|column1|column2|
+-------+-------+
|dhoni |no1 |
|sachin |no2 |
|dravid |no3 |
|dhoni |no1 |
+-------+-------+
Note:
write a query to validate the table data ex: to check dhoni should always has to get no1 in column2, irrespective of duplicate records and order, like wise need to check for other players also, irrespective of no of data's present in table.. just need to validate the things..

Find one user with more than one no.
select column1 from table group by column1 having count(*) > 1;
Find the same no with more then one user.
select column2 from table group by column2 having count(*) > 1;

Please try:
SELECT
column2, COUNT(*) TotalCount
FROM YourTable
GROUP BY column2
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
where TotalCount returns the count of number assigned multiple times.
or
select * From(
SELECT distinct Column1, Column2, COUNT(*) over (partition by Column2) TotalCount
FROM YourTable
)x
where TotalCount>1

Related

What is the difference between count (*) and count(attribute_name)?

Is there any difference between COUNT(*) and COUNT(attribute_name)?
I used count(attribute_name) as I thought that it would be specific hence the searching process would be easier. Is that true?
It would be great to see any example with sql code with my issue to help me understand better
Imagine this table:
select Count(TelephoneNumber) from Calls -- returns 3
select Count(*) from Calls -- returns 4
count(column_name) also counts duplicate values. Consider:
select Count(TelephoneNumber) from Calls -- returns 4
COUNT(*) counts all the records in the group.
COUNT(column_name) only counts non-null values.
There is also another typical expression, COUNT(DISTINCT column_name), that counts non-null distinct values.
Since you asked for it, here is a demo on DB Fiddlde:
with t as (
select 1 x from dual
union all select 1 from dual
union all select null from dual
)
select count(*), count(x), count(distinct x) from t
COUNT(*) | COUNT(X) | COUNT(DISTINCTX)
-------: | -------: | ---------------:
3 | 2 | 1
COUNT(*) will count all the rows.
COUNT(column) will count non-NULLs only.
Your can use of COUNT(*) or COUNT(column) which should be based on the desired output only.
Consider below Example of employee table
ID Name Description
1 Raji Smart
2 Rahi Positive
3
4 Falle Smart
select count(*) from employee;
Count(*)
4
select count(name) from employee;
Count(Name)
3
count() only counts non-null values. * references the complete row and as such never excludes any rows. count(attribute_name) only counts rows where that column is no null.
So this:
select count(attribute_name)
from the_table
is equivalent to:
select count(*)
from the_table
where attribute_name is not null
The difference is simple: COUNT(*) counts the number of rows produced by the query, whereas COUNT(1) counts the number of 1 values. Note that when you include a literal such as a number or a string in a query, this literal is "appended" or attached to every row that is produced by the FROM clause.
For more detail this link would help you understand.

What exactly does SELECT DISTINCT(COUNT(*)) do?

I used the following query and it returned what I wanted it to return, but I'm having a tough time wrapping my head around what the query is doing.
Query is nothing fancier than what's in the title: select distinct(count(*)) from table1
Distinct is not required in your SQL ,as you are going to get only result, count(*) without group by clause returns, count of all rows within that table.
Hence try this :
select count(*) from table1
Distinct is used for finding distinct values from a group of values:
say you have table1 , with column1 as :
Column1
----------
a
a
b
b
a
c
following sqls are run you will get output as :
1) select count(*) from table1
output :6
2) select distinct(count(*)) from table1
output :6
3) select count( distinct column1) from table1
output :3
Usually distinct is used inside count preferably with a particular column .
select count( distinct column_name_n ) from table1
The distinct is redundant... Select Count(*) with only one table can only generate one value, so distinct (which would eliminate duplicates) is irelelvant.
If you had multiple outputs, (if for example you were grouping on something) then it would cause the query to only display one output row for every distinct value of count(*) that would other wise be generated...
if, for example, you had
name
Bob
Bob
Bob
Bob
Mary
Mary
Mary
Mary
Dave
Dave
Al
George
then
select count(*)
From table
group By name
would result in
4
4
2
1
1
but
select distinct count(*)
From table
group By name
would result in
4
2
1

SQL - combination of MIN and COUNT

This is in Oracle database. Say I have the following table A:
column1 column2
id1 a
id2 a
id3 a
id4 b
id5 b
id6 c
So what I want the sql does is:
First count there's three As and two bs and one c, then based on the counts return me the smallest number of these counts, in this case is 1 (because we only have one c)
Can this be achieved somehow by using the combination of MIN and COUNT?
In Oracle you can do this directly; count per group and use MIN on the results to get back one row with the desired value.
select min(count(*))
from tablea
group by column1;
Try this:
SELECT MIN(Count) as MinVal
FROM
(SELECT column2,COUNT(column2) as Count
FROM TableA
GROUP BY column2) T
Explanation:
Inner query select the counts of column2 for each value of column2 in the table. Then with the outer query, the minimum count is selected.
If you are using Oracle 12, you can do this without a subquery:
select count(*) as cnt
from table t
group by cnt
order by cnt asc
fetch first 1 row only;
For those familiar with MySQL or Postgres, fetch first 1 row only is equivalent to limit, and allows you to limit the number of output rows without using a subquery.
This should work for you
SELECT *
FROM(
SELECT Column2, COUNT(Column1)
FROM TableA
GROUP BY Column2
ORDER BY COUNT(Column1))
WHERE Rownum = 1
SELECT MIN(cnt)
FROM (SELECT COUNT(*) AS cnt
FROM my_table
GROUP BY column2)
EDIT:
As ElmoVanKielmo noted, it's somewhat pointless to offer a solution without explaining it.
The inner query groups the data by column2 values, and return the number of rows for each one. The outer query treats these as just a bunch of numbers, and returns the minimal value among them.

SQL Query to get all rows with duplicate values but are not part of the same group

The database schema is organized as follows:
ID | GroupID | VALUE
--------------------
1 | 1 | A
2 | 1 | A
3 | 2 | B
4 | 3 | B
In this example, I want to GET all Rows with duplicate VALUE, but are not part of the same group. So the desired result set should be IDs (3, 4), because they are not in the same group (2, 3) but still have the same VALUE (B).
I'm having trouble writing a SQL Query and would appreciate any guidance. Thanks.
So far, I'm using SQL Count, but can't figure out what to do with the GroupId.
SELECT *
FROM TABLE T
HAVING COUNT(T.VALUE) > 1
GROUP BY ID, GroupId, VALUE
The simplest method for this is using EXISTS:
SELECT
ID
FROM
MyTable T1
WHERE
EXISTS (SELECT 1
FROM MyTable
WHERE Value = t1.Value
AND GroupID <> t1.GroupID)
Here is one method. First you have to identify the values that appear in more than one group and then use that information to find the right rows in the original table:
select *
from t
where value in (SELECT value
FROM TABLE T
GROUP BY VALUE
HAVING COUNT(distinct groupid) > 1
)
order by value
Actually, I prefer a slight variant in this case, by changing the HAVING clause:
HAVING min(groupid) <> max(groupid)
This works when you are looking for more than one group and should be faster than the COUNT DISTINCT version.
SELECT ALL_.*
FROM (SELECT *
FROM TABLE_
GROUP BY ID, GROUPID, VALUE
ORDER BY ID) GROUPED,
TABLE_ ALL_
WHERE GROUPED.VALUE = ALL_.VALUE
AND GROUPED.GROUPID <> ALL_.GROUPID

Select distinct name with random id

I have a table with an id and a name (an a bunch of other stuff not relevant for this query). Now I need an SQL statement that returns one row per distinct name and in that row I need the name and one id (can be any id).
The table is looking something like this:
id | name
---+-----
1 | a2
2 | a2
3 | a4
4 | a4
5 | a2
6 | a3
btw. using Postgres 8.4
Tried various combinations of grouping or joining with self. Is this even possible without creating extra tables?
Arbitrarily choosing to return the minimum id per name.
SELECT name, MIN(id)
FROM YourTable
GROUP BY name
You may look at PostgreSQL wiki. It shows how to select random rows.
You may use random() function to select random rows using ORDER BY clause of SELECT. Example:
SELECT id FROM mytable ORDER BY random()
You can then use GROUP BY to select distinct names. You may need to limit results using LIMIT clause. So the query looks something like this:
SELECT id, name FROM table_name GROUP BY name ORDER BY random() LIMIT 1
select ID, name from table group by name;