Solved

Group by with out aggregate function

Posted on 2011-09-11
8
199 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
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.

 

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

Suggested Solutions

Title # Comments Views Activity
SQL 2008 with .NET 4.5.2 4 35
date diff with Fiscal Calendar 4 56
SQL Server 2008 R2, need a pivot/cross tab query... 4 29
query optimization 6 15
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 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