Querying with SQL - mysql

I am trying to write a query in sql which gives me distinct Pan details and the count of that Pan in the table where the Name is appearing more than once
For example: Consider the table below
ID Name Pan
1 ABC 123
2 ABC 123
3 DEF 456
4 ABC 124
5 WW 234
6 WW 2345
The result expected is :
Pan Name Count1
123 ABC 2
124 ABC 1
234 WW 1
2345 WW 1
Could someone please help me out with this.

Try this. Hope below statement help you out.
SELECT PAN, NAME , COUNT(ID) FROM TAB_PAN GROUP BY PAN, NAME HAVING COUNT(ID)>1
As per conversation the requirement seems different.
The below code will work fine.
Schema for given data
SELECT * INTO #TAB FROM (
SELECT 1 ID , 'ABC' NAME , 123 PAN
UNION ALL
SELECT 2, 'ABC', 123
UNION ALL
SELECT 3 , 'DEF' ,456
UNION ALL
SELECT 4 , 'ABC', 124
UNION ALL
SELECT 5 , 'WW' , 234
UNION ALL
SELECT 6 , 'WW' ,2345
)AS A
Logic for the required case
SELECT PAN, NAME,COUNT(ID) ACTUAL_COUNT FROM #TAB t
WHERE (SELECT COUNt(NAME) FROM #TAB WHERE NAME= t.NAME) >1
GROUP BY PAN, NAME

SELECT DISTINCT x.*
FROM
( SELECT pan
, name
, COUNT(*) total
FROM my_table
GROUP
BY pan
, name
) x
JOIN my_table y
ON y.name = x.name
AND y.pan <> x.pan

Related

SQL query to find new column

I need your help. I have a table named Test_Result with 2 columns as shown below.
ID Source_ID
10 1
20 2
30 2
40 3
50 3
60 3
70 4
I am trying to get output as below,but unable to get logic.
ID Parent_ID Source_ID
10 Null 1
20 Null 2
30 20 2
40 Null 3
50 40 3
60 50 3
70 Null 4
Kindly help me with this scenario. I attached question in picture for as well.
Regards,
Abhi
These solutions (ROW_NUMBER/LAG) will work for MySQL 8.0+ or MariaDB 10.2
You could use ROW_NUMBER() and join to previous row:
CREATE TABLE tab(ID INT ,Source_ID INT);
INSERT INTO tab(id, Source_id)
SELECT 10, 1
UNION ALL SELECT 20 , 2
UNION ALL SELECT 30, 2
UNION ALL SELECT 40 , 3
UNION ALL SELECT 50 , 3
UNION ALL SELECT 60 , 3
UNION ALL SELECT 70 , 4;
WITH cte AS (
SELECT *, ROW_NUMBER() OVER(ORDER BY id) AS rn
FROM tab
)
SELECT c1.ID,
CASE WHEN c1.Source_ID = c2.Source_ID THEN c2.Id END AS Parent_Id,
c1.Source_ID
FROM cte c1
LEFT JOIN cte c2
ON c1.rn = c2.rn+1;
Rextester Demo
EDIT:
Using LAG() windowed function:
SELECT c1.ID,
CASE
WHEN c1.Source_ID = LAG(Source_ID) OVER w THEN LAG(ID) OVER w
END AS Parent_Id,
c1.Source_ID
FROM tab c1
WINDOW w AS (ORDER BY ID)
ORDER BY id;
DBFiddle
EDIT2:
Simulating LAG using variables:
SET #lag_Source_id='';
SET #lag_Id = '';
SELECT ID,
CASE WHEN Source_Id = lag_Source_ID THEN lag_ID END AS Parent_ID
,Source_ID
FROM (
SELECT ID
, Source_ID
, #lag_Source_id AS lag_Source_id
, #lag_Source_id:= Source_ID AS curr_Source_ID
, #lag_Id AS lag_ID
, #lag_Id := ID AS curr_ID
FROM tab
ORDER BY id
) AS sub
RextesterDemo2
if you are using mysql database simply do this,
SELECT ID, (ID + Source_ID) AS Parent_ID, Source_ID FROM tableName LIMIT 10;

Find duplicate records in MySQL without named column

I have a table like this:
**lead_id** **form_id** **field_number** **value**
1 2 1 Richard
1 2 2 Garriot
2 2 1 Hellen
2 2 2 Garriot
3 2 1 Richard
3 2 2 Douglas
4 2 1 Tomas
4 2 2 Anderson
Where field_number = 1 is the name and field_number = 2 is the surname.
I would like to find entries that are equal by name OR surname and group them by lead_id, so the output could be like this:
1
2
3
Any thoughts on how this can be done?
This should work and be reasonably efficient (depending upon indexes):
select distinct lead_id
from tablename as t1
where exists (
select 1
from tablename as t2
where t1.field_number = t2.field_number
and t1.value = t2.value
and t1.lead_id <> t2.lead_id
)
Select leadid from (
Select DISTINCT leadid,value from tablename
Where fieldnumber=1
Group by leadid,value
Having count(value) >1
Union all
Select DISTINCT leadid,value from tablename
Where fieldnumber=2
Group by leadid,value
Having count(value) >1
) as temp
Surely there is a faster option

Counting unique numbers in a column MySQL

I have a query that returns data in the following format:
id | name | number
1 John 12545
1 John 50496
2 Mary 23443
3 Mark 54
3 Mark 5600
3 Mark 50206
I would like to find out the number of distinct ids that appear in the result set. For example, for the result above. I would like to obtain the value 3.
Is there any way to add a column so the result looks like this instead?
count | id | name | number
3 1 John 12545
3 1 John 50496
3 2 Mary 23443
3 3 Mark 54
3 3 Mark 5600
3 3 Mark 50206
My query is:
SELECT * FROM (
SELECT id FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) totalCount,
id,name,number
FROM tableName
or by using CROSS JOIN
SELECT x.totalCount,
a.id, a.name, a.number
FROM tableName a, (SELECT COUNT(DISTINCT id) totalCount
FROM tableName) x
You should try :
SELECT id,name,number, (SELECT COUNT(DISTINCT name) FROM YourTableName) FROM YourTableName
Good luck
SELECT COUNT(DISTINCT id) would be faster than using column name.
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) as 'count',
id,name,number
FROM tableName
SELECT COUNT(id) AS count , id, name, number
FROM
(
SELECT id
FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
GROUP BY id, name, number

MySQL sly select

Can not understand how to make an select:
table structure:
id name value date
1 ivan 2 2010-01-01
2 ivan 3 2010-05-08
3 ivan 1 2009-04-14
4 sasha 2 2011-11-11
5 sasha 9 2012-04-04
How to get?:
name value(in last time)
ivan 3
sasha 9
This is untested, but should work:
SELECT
name,
MAX(value)
FROM
structure
GROUP BY
name
This query should work
select f.name, f.value, f.date
from (
select id,name,max(date) as dat from tableName group by name
) as x inner join tableName as f on f.name = x.name and f.date = x.dat;
SELECT DISTINCT name, value FROM table ORDER BY date DESC
thats what i understand from your current description so far.
Something like this:
SELECT
MAX(value),
name
FROM
Table
GROUP BY
name

self join plus another join

I have a table of country teams
id country group
1 Poland 1
2 Greece 1
3 England 2
4 France 2
5 Germany 3
6 Spain 3
I also a table of scores for each country
fromId score
1 100
3 50
2 90
4 60
What I would like to do is get back a table of scores for each country within a group, having supplied just a country name. For example if I supply "France" then I would want the following table back
country score
England 50
France 60
I have managed to self join the country table with
SELECT
`t1`.`fldCountry`,
`t1`.`fldID`
FROM tblteam t1, tblteam t2
WHERE
t2.fldTeam = t1.fldTeam
AND
t2.fldCountry = 'France'
but am now stuck how to joing this back to get the data!
Please could anyone help a little?
Here you go
Country Table:
CREATE TEMPORARY TABLE Country
(
id INT,
country VARCHAR(20),
grp INT
)
INSERT INTO Country
(
id,
country,
grp
)
SELECT 1,
'Poland',
1
UNION ALL
SELECT 2,
'Greece',
1
UNION ALL
SELECT 3,
'England',
2
UNION ALL
SELECT 4,
'France',
2
UNION ALL
SELECT 5,
'Germany',
3
UNION ALL
SELECT 6,
'Spain',
3
Score Table:
CREATE TEMPORARY TABLE Score ( fromid INT, score INT )
INSERT INTO Score
(
fromid,
score
)
SELECT 1,
100
UNION ALL
SELECT 3,
50
UNION ALL
SELECT 2,
90
UNION ALL
SELECT 4,
60
Query:
SELECT b.country,
IFNULL(s.score, 0) score
FROM Country a
INNER JOIN Country b
ON a.grp = b.grp
LEFT JOIN score s
ON s.fromid = b.id
WHERE a.country = 'France'
Result:
Country Score
England 50
France 60
SELECT
c.country,
s.score
FROM
countries c
INNER JOIN scores s ON c.id = s.fromId
WHERE
c.group = (SELECT group FROM countries WHERE country = 'France' LIMIT 1);
Something like this should work.
Select C2.Country,SUM(S.Score)
FROM Country AS C1
INNER JOIN Country AS C2
ON C1.Group=C2.Group
INNER JOIN Scores AS S
ON C2.id = S.FromID
WHERE C1.Country=#Country
GROUP BY C2.Country
Where #Country is the country you want to look up, in your case, France.