Solved

Group by with out aggregate function

Posted on 2011-09-11
8
195 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

20 Experts available now in Live!

Get 1:1 Help Now