Click here to Skip to main content
15,908,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

VB
ID | Name |Dept |Gender
=======================
1  |ABC   |DPT1 |M
2  |DEF   |DPT2 |M
3  |GHI   |DPT1 |F
4  |JKL   |DPT2 |F
5  |MNO   |DPT3 |M
6  |PQR   |DPT1 |M


Above is the table from which I want the below result.

VB
Dept |Gender|Count
==================
DPT1 |M     |2
DPT2 |M     |1
DPT3 |M     |1
DPT1 |F     |1
DPT2 |F     |1
DPT3 |F     |0


What is the query in SQL Server.?

Thanks,
Janes T
Posted

1 solution

Select dept, gender, count(name) from myTable group by dept, gender

You should read my articles on SQL, because this is quite a basic request, and I cover it in my article on 'select'.


Here[^]
 
Share this answer
 
v2
Comments
maajanes 13-Jan-14 1:40am    
Can I have a query to find only the duplicate records in a table??
Christian Graus 13-Jan-14 16:40pm    
You can do that with row_number. Assuming your table has an id column, do this:

with dups
as
(
select id, row_number() over (partition by id order by id) as row from myTable
)

select * from dups where row > 1

replace select * with delete, to delete duplicates.

http://www.codeproject.com/Articles/709759/SQL-Wizardry-Part-Six-Windowing-Functions

has more info.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900