C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » SQL Server »

pl help me simple qry


Posted Date: 04 Aug 2005      Posted By: JAIGANESH      Member Level: Silver     Points: 2   Responses: 5



assume my table name is table1
city name sex
--- --- --
Del A M
Del B M
UP C M
UP D F
UP E M
HR F F
HR G F

i need output like this

city male female
--------------------
del 2 0
up 2 1
hr 0 2

that is for each city i need count of male and female.

pl write qry





Responses

Author: Priya Rohan Marc    04 Aug 2005Member Level: SilverRating: 2 out of 52 out of 5     Points: 2


select distinct a.city ,
(Select count(*) from citydetails where sex = 'M' and city in (select a.city)) as Male,
(Select count(*) from citydetails where sex = 'F' and city in (select a.city)) as Female
from citydetails a



Author: santhi priya peddi    04 Aug 2005Member Level: SilverRating: 2 out of 52 out of 5     Points: 2

hi..
Select x.City , Sum(X.Male) as Male , Sum(X.Female) as Female from
(select City,count(Sex) as Male,0 as Female from E1 Where Sex='M' group by City
Union
select City,0 as Male ,count(Sex) as Female from E1 Where Sex='F' group by City)
x Group by X.City

Try this n hope u get answer for ur ?

keep smiling,
santhi.



Author: Ashish Singh Khichi    06 Aug 2005Member Level: BronzeRating: 2 out of 52 out of 5     Points: 2

SELECT a.city, a.Male, b.feMale
FROM (SELECT city, COUNT(*) AS Male
FROM ex
WHERE sex = 'M'
GROUP BY city) a LEFT OUTER JOIN
(SELECT city, COUNT(*) AS feMale
FROM ex
WHERE sex = 'F'
GROUP BY city) b ON a.city = b.city



Author: Ashish Singh Khichi    06 Aug 2005Member Level: BronzeRating: 2 out of 52 out of 5     Points: 2

 
SELECT a.city, a.Male, b.feMale
FROM (SELECT city, COUNT(*) AS Male
FROM ex
WHERE sex = 'M'
GROUP BY city) a LEFT OUTER JOIN
(SELECT city, COUNT(*) AS feMale
FROM ex
WHERE sex = 'F'
GROUP BY city) b ON a.city = b.city




Author: AbiBaby (Abishek Bellamkonda)    10 Aug 2005Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

The same query can be written as below in MS Access.
SELECT CityName, SUM(IsMale) As NoOfMales, SUM(IsFemale) As NoOfFemales
FROM (
SELECT CityName, IIF(Sex='M', 1,0)As IsMale, IIF(Sex='F', 1,0) as IsFemale
FROM Citytable
) GROUP BY CityName;



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Exporting SQL Server Table to Excel Using DTS
Previous : Function is not working
Return to Discussion Forum
Post New Message
Category: SQL Server

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use