Solved

Group by with out aggregate function

Posted on 2011-09-11
8
193 Views
Last Modified: 2012-05-12

I have table  as followingly.

Table Name : Authors
Author_ID , Author_FirstName,Author_LastName,State .

Now I want to group by state and I dont want use any aggregate function ? How can I do this.?
 
0
Comment
Question by:N_Sri
  • 5
  • 3
8 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36518331
N_Sri, what is the goal?

If you just want a distinct listing of States, then you can do this w/o an aggregate function.

SELECT [State]
FROM Authors
GROUP BY [State]

Open in new window


If you are wanting something else, please clarify my initial question.
0
 

Author Comment

by:N_Sri
ID: 36518381
No . Goal is to see like all the  records   of particular state ..   and for all states .. like this..

State 1
    Author_ID , Author_FirstName,Author_LastName, State
State 2
    Author_ID , Author_FirstName,Author_LastName, State

State 3
    Author_ID , Author_FirstName,Author_LastName, State
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
ID: 36518540
What is wrong with viewing it like this:
SELECT [State], Author_ID , Author_FirstName, Author_LastName
FROM Authors
ORDER BY [State]

Open in new window


If you are outputting this to an application, what I would do is use the above query ordering by [State] and then in the loop to display authors I would have a variable that holds the state value of last iteration. If the current state equals the last state, I just show the new author. If state is different, I show the new state then new author. With the rows in order by state, I now a chance in state means a change in groupings.
0
 

Author Comment

by:N_Sri
ID: 36518572
I am not showing in any application, I am querying database.

Just want to get data as

State  California
     Author_ID , Author_FirstName,Author_LastName     record 1
     Author_ID , Author_FirstName,Author_LastName       record 2
     Author_ID , Author_FirstName,Author_LastName     record 3
State  ABC
 
   Author_ID , Author_FirstName,Author_LastName     record 1
   Author_ID , Author_FirstName,Author_LastName       record 2
   
State SomeState
 
   Author_ID , Author_FirstName,Author_LastName     record 1
   Author_ID , Author_FirstName,Author_LastName       record 2

Want to group  all records of particular state into one set...

 
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
ID: 36518605
Okay, if you are querying the database, again I will ask what is wrong with the order by. Given this is not a client app, but actual result set from SQL, you should expect this to be tabular and not in a tree.
0
 

Author Closing Comment

by:N_Sri
ID: 36518616
thankyou
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36518621
If you really want to look at it this way, you could use XML:
SELECT [State] AS Name
     , (SELECT Author_ID, Author_FirstName, Author_LastName
	    FROM Authors a2
		WHERE a2.[State] = a1.[State]
		FOR XML PATH('Author'), TYPE, ROOT('Authors'))
FROM Authors a1
GROUP BY [State]
FOR XML PATH('State'), TYPE, ROOT('States')
;

Open in new window


Click on the result and you will get an XML display like:
<States>
  <State>
    <Name>ABC</Name>
    <Authors>
      <Author>
        <Author_ID>25</Author_ID>
        <Author_FirstName>Joe</Author_FirstName>
        <Author_LastName>Smith</Author_LastName>
      </Author>
      <Author>
        <Author_ID>125</Author_ID>
        <Author_FirstName>Joe</Author_FirstName>
        <Author_LastName>Myer</Author_LastName>
      </Author>
      <Author>
        <Author_ID>250</Author_ID>
        <Author_FirstName>Quincy</Author_FirstName>
        <Author_LastName>Tuplets</Author_LastName>
      </Author>
    </Authors>
  </State>
  <State>
    <Name>California</Name>
    <Authors>
      <Author>
        <Author_ID>1</Author_ID>
        <Author_FirstName>John</Author_FirstName>
        <Author_LastName>Smith</Author_LastName>
      </Author>
      <Author>
        <Author_ID>11</Author_ID>
        <Author_FirstName>John</Author_FirstName>
        <Author_LastName>Johnson</Author_LastName>
      </Author>
      <Author>
        <Author_ID>19</Author_ID>
        <Author_FirstName>Richard</Author_FirstName>
        <Author_LastName>Tracy</Author_LastName>
      </Author>
    </Authors>
  </State>
  <State>
    <Name>SomeState</Name>
    <Authors>
      <Author>
        <Author_ID>10</Author_ID>
        <Author_FirstName>Jerry</Author_FirstName>
        <Author_LastName>Smith</Author_LastName>
      </Author>
      <Author>
        <Author_ID>110</Author_ID>
        <Author_FirstName>Jerry</Author_FirstName>
        <Author_LastName>Riggs</Author_LastName>
      </Author>
      <Author>
        <Author_ID>140</Author_ID>
        <Author_FirstName>Charles</Author_FirstName>
        <Author_LastName>Smith</Author_LastName>
      </Author>
    </Authors>
  </State>
</States>

Open in new window


It serves purpose of viewing the data in a tree. If you just really want text.
SELECT CASE Author_ID WHEN 0 THEN [State] ELSE '' END AS [StateName]
     , Author_ID , Author_FirstName, Author_LastName
FROM (
SELECT [State], 0 AS Author_ID, '' AS Author_FirstName, '' AS Author_Lastname
FROM Authors a1
GROUP BY [State]
UNION ALL
SELECT [State], Author_ID , Author_FirstName, Author_LastName
FROM Authors
) t([State], Author_ID , Author_FirstName, Author_LastName)
ORDER BY [State], Author_ID

Open in new window


Then you can turn on Results to Text in SSMS:
StateName  Author_ID   Author_FirstName Author_LastName
---------- ----------- ---------------- ---------------
ABC        0                            
           25          Joe              Smith
           125         Joe              Myer
           250         Quincy           Tuplets
California 0                            
           1           John             Smith
           11          John             Johnson
           19          Richard          Tracy
SomeState  0                            
           10          Jerry            Smith
           110         Jerry            Riggs
           140         Charles          Smith

Open in new window


Kevin
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36518623
I was typing and didn't see you closed this. Glad I could help.
Best regards and happy coding,

Kevin
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now