?
Solved

Group by with out aggregate function

Posted on 2011-09-11
8
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

765 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