Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 17th, 2003, 08:23 PM
Authorized User
 
Join Date: Oct 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help SQL querry

I have a table called GroupMembers
This has 4 col's named GroupName, GroupLevel, PhoneNr and GroupType
Info like this:
**************TABLE GroupMembers Content********
Whole Country -- Level1 -- 12332112 -- 1
StreetCar ------ Level2 -- 90000000 -- 1
BMW ---------- Level3 -- 90001800 -- 1
Mercedes ----- Level3 -- 90001800 -- 1
Oldsmobile ----- Level3 -- 90000000 -- 4
Raceing -- Level4 -- 90000000 -- 1
Raceing -- Level4 -- 90080000 -- 1
************************************************

I need to make a querry that gives me the top 10 groups with most members in it. And I can't figure out this puzzle.. If I use subquerries then I can only get one hit, with count and such.

So if anybody with some clue would spare a few moments to hel I will apreciate it!

Thanks in advance folks!

------------------------
All help is Good help!
Regards
Michael
__________________
------------------------
All help is Good help!
Regards
Michael
 
Old November 17th, 2003, 10:07 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I'm confused by your table structure. Is this the table of members of a group or the table of the groups?

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 17th, 2003, 10:23 PM
Authorized User
 
Join Date: Oct 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is the table with group members. I have one table containing members info (MEMBERS) and on that contains groups info (GROUPS). This third is to keep track of what members are in what group.

When a member joins a group, a row is inserted into GroupMembers containig data from both GROUPS and MEMBERS.
Does that help?

------------------------
All help is Good help!
Regards
Michael
 
Old November 17th, 2003, 10:36 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

So this table is a join table. That throws me cause it's got a large amount of descriptive data for a join table. Usually joins table (at least all the ones I've worked with) contain just two or three columns, and they are usually just key columns like integer fields that are keyed back to the main tables. I'm curious to see your other table layouts.

My wrambling about table layout aside, here's an example query that should get you in the right direction...

SELECT GroupName, COUNT(GroupName) AS MemberCount
FROM GroupMembers
GROUP BY GroupName
ORDER BY MemberCount DESC

You can order by the alias of a column so in this case we'll order by the aggregation result "MemberCount". In MS-SQL server you can also select a limited set of result rows:

SELECT TOP 10 GroupName, COUNT(GroupName) AS MemberCount
...

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 17th, 2003, 11:28 PM
Authorized User
 
Join Date: Oct 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Worked like a charm.... as always!

If you were around in Norway I'd buy you a beer..

btw. If you are interrested in some table layout give me a word and I'll post it for you. (An easy way to get the layout in text form would be nice to have then, if you got one he he he)


Thanx again for your help Peter

------------------------
All help is Good help!
Regards
Michael
 
Old November 18th, 2003, 01:23 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

FYI: In SQL query analyzer you can type "sp_help <tablename>" and with your results showing as text, you can get the table details.

Glad you got it working.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 19th, 2003, 11:37 PM
Authorized User
 
Join Date: Oct 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx for another helpfull tip Peter... I'm new (getting better) at ASP.NET. But still quite hopeless in SQL. **Thinking** Wonder how I actually make things work he hehe.
My tables as such a mess, I need to clean up before I post'em....
Cant show people how lost I really am!

Thanx agian, and don't worry. I'll ask again when stuck on something new..

------------------------
All help is Good help!
Regards
Michael





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with insert querry Ashwini Classic ASP Databases 0 January 16th, 2006 07:17 AM
Problem with querry Ashwini Classic ASP Databases 0 January 16th, 2006 07:07 AM
MS Access SQL: >1 querry results in one cell pav Access 0 November 23rd, 2004 06:14 AM
How to generate SQL querry ? mistry_bhavin ADO.NET 1 August 12th, 2004 01:15 PM
querry an access database silver_cuts Classic ASP Basics 8 June 25th, 2004 04:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.