I have a table with two columns and following data
ID NAME
1 ALPHA
1 ALPHA
2 BETA
1 BETA
First three rows are correct data, but in the last row someone accidentally entered ID 1 instead of ID 2, can anyone help me with a query to fetch multiple rows of ID for distinct names. I have tried the query below but its not yielding the correct result
SELECT F1.ID FROM myTable F1 WHERE F1.Name in
(SELECT DISTINCT F2.Name FROM myTable F2)
Actually, you need names that have multiple IDs; right?
For sample data:
SQL> select * From test;
ID NAME
---------- -----
1 alpha
1 alpha
2 beta
1 beta
Query, using group by and having clauses:
SQL> select name
2 from test
3 group by name
4 having count(distinct id) > 1;
NAME
-----
beta
SQL>
Related
I have a table with 100 000 record, I want to select only the none repeated.
In another word, if the row are duplicated did not show it at all
ID Name Reslut
1 Adam 10
2 Mark 10
3 Mark 10
result
ID Name Reslut
1 Adam 10
any ideas ?
You could join a query on the table with a query that groups by the name only returns the unique names:
SELECT *
FROM mytable t
JOIN (SELECT name
FROM mytable
GROUP BY name
HAVING COUNT(*) = 1) s ON t.name = s.name
Using the same set :
ID Name Result
1 Adam 10
2 Mark 10
3 Mark 10
4 Mark 20
I'm guessing the final solution would be:
ID Name Result
1 Adam 10
4 Mark 20
Using the above query previously suggested I modified it to take the result into consideration:
SELECT t1.*
FROM myTable t1
JOIN
(
SELECT name, result
FROM myTable
GROUP BY name, result
HAVING COUNT(*) = 1
) t2
WHERE
t1.name=t2.name and
t1.result = t2.result;
TO Plot Each Input table I have Separate query, need to apply functionality on that queries and want to create Single query for Output Table
Select Distinct Names, SUM(count) from
(Select Query table 1
union
Select Query table 2
union
Select Query table 3) table group by Names;
this query Not adding count properly Niether Sorting Names properly Whats wrong with this ?
Input Table 1 :-
Names count
bob 3
pol 4
Input Table 2 :-
Names count
bob 5
0 - name may be missing here neglect this entry
Input Table 3 :-
Names count
james 4
pol 7
bob 1
Expected output table :-
Names count
bob 9
pol 11
james 4
You can use UNION and them sum of those.
select sum(a), sum(b) from
(select 2 as a, 1 as b
union select 3 as a, 6 as b
union select 4 as a, 1 as b) as b
Try this query
select `Name`,sum(`Count`) total from ( select `Name`,`Count` from `table1` union all select `Name`,`Count` from `table2` union all select `Name`,`Count` from `table3` ) tot group by `Name`
May this help you.
I have a mysql table named X and I wanna count number of occurrences of a tuple. For example we have table X as:
A B
1 2
1 3
1 2
And I wanna run a query and get results like this:
A B Count
1 2 2
1 3 1
Is there anyway to do so in mysql?!
You need to use Aggregate Function - COUNT in your query.
Use this (assuming your table's name is table1) :
select a, b, COUNT(*) as [Count] FROM table1 group by a, b
I have a record in table something like this
ID Name Value
1 abc 123
2 abc 152
3 cde 574
4 def 153
5 abc 777
How to delete row from the above column based on this algorithm,
We have same name for 3 rows (ID: 1,2,5). Delete all these rows except any one selected randomly. Same applied for other Names
Possible using sql or T-SQL?
;with d as (
select *, rowNum = row_number() over (partition by Name order by checksum(newid()))
from TableName
)
delete d
where rowNum > 1
I've the following scenario:
Table 1
group_id
location
Table 2
group_id
empname
I need the following output:
group_id, location, empname
1 ABC NULL
1 ABC XYZ
1 ABC PQR
so the first row is the master row and the rest of the rows are all detail rows for that master.
How can i get this output?
TIA
Bo
SELECT t1.group_id,t1.location,NULL AS empname FROM Table1 t1
UNION
SELECT t1.group_id,t1.location,t2.empname
FROM Table1 t1 INNER JOIN Table2 t2 ON t1.group_id=t2.group_id
ORDER BY 1,3
EDIT:
Just alias the 3rd column of the first SQL Statement as empname.