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 October 14th, 2004, 11:43 AM
Authorized User
 
Join Date: Jul 2003
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default Group By Problem

Hello Everyone and thanks for your help in advance. I am in need of creating a query that retrieves several fields and a total count for each distinct Name in the database. I tried:

SELECT MLS_Agent_Name AS AgentName, MLS_AGENT_ID AS AgentID, COUNT(*) AS ListingCount
FROM tblListings
GROUP BY MLS_Agent_Name, MLS_AGENT_ID With RollUp
GO

In this case, I am trying to retrieve both the agent name, agent ID, and a total count of occurrences for each agent. However, the way the group by clause works in this case, it retrieves two set of names for each distinct name, obviously due to the second grouping. However, when I remove the second column in the Group By parameter, the query throws an error. I am really not sure if I am going ab out this the wrong way or even if what I want can be done in one query. Any help on this topic would be greatly appreciated. Thanks.


 
Old November 25th, 2004, 05:46 AM
Authorized User
 
Join Date: Oct 2003
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SubodhKumar Send a message via Yahoo to SubodhKumar
Default

Try this one
SELECT MLS_Agent_Name AS AgentName, COUNT(*) AS ListingCount
FROM tblListings
GROUP BY MLS_Agent_Name

if you want some other result then pl explain.



Quote:
quote:Originally posted by [email protected]
 Hello Everyone and thanks for your help in advance. I am in need of creating a query that retrieves several fields and a total count for each distinct Name in the database. I tried:

SELECT MLS_Agent_Name AS AgentName, MLS_AGENT_ID AS AgentID, COUNT(*) AS ListingCount
FROM tblListings
GROUP BY MLS_Agent_Name, MLS_AGENT_ID With RollUp
GO

In this case, I am trying to retrieve both the agent name, agent ID, and a total count of occurrences for each agent. However, the way the group by clause works in this case, it retrieves two set of names for each distinct name, obviously due to the second grouping. However, when I remove the second column in the Group By parameter, the query throws an error. I am really not sure if I am going ab out this the wrong way or even if what I want can be done in one query. Any help on this topic would be greatly appreciated. Thanks.


Enjoy!
Subodh Kumar
Phoneytunes.com
 
Old December 9th, 2004, 12:42 PM
Authorized User
 
Join Date: Nov 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just wanted to add, when you do aggregation using Group By you have to include all columns (excluding columns in aggregate functions like Count,Sum etc) in Select Statement as Group By parameters.

RollUp is used when you want to summarize you selection.





Similar Threads
Thread Thread Starter Forum Replies Last Post
"Group by" problem dani1 XSLT 2 October 30th, 2008 03:21 PM
Restart new group number in Group Footer sukarso Crystal Reports 2 October 13th, 2006 12:11 PM
Report problem in group by mateenmohd Access 3 May 24th, 2004 12:16 AM
Query Problem group by mateenmohd SQL Server 2000 4 February 4th, 2004 02:44 AM
group by problem rajanikrishna Access ASP 1 January 27th, 2004 08:17 AM





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