Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Group by with out aggregate function

Posted on 2011-09-11
8
Medium Priority
?
207 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 60

Accepted Solution

by:
Kevin Cross earned 2000 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 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 2000 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 2000 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 60

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 60

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

810 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