Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

how to group my results Expand / Collapse
Author
Message
Posted Tuesday, January 26, 2010 10:00 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, March 19, 2013 2:21 PM
Points: 116, Visits: 160
here is the structure of my table
CREATE TABLE [dbo].[duplicate2](
[id] [int] IDENTITY(1,1) NOT NULL,
[firstName] [varchar](25) NULL,
[Lastname] [varchar](25) NULL,
[grades] [smallint] NULL,
[date] [datetime] NULL
) ON [PRIMARY]

the question is i want to find the average grades based on the name and i want the result to include all other columns, the problem i have is that when i execute this statements;

select firstname, avg(grades)
from duplicate2
group by firstname

it groups the results based on firstname, the problem is that i want all the other columns to also
be included in the result set, but when i put all the other columns in the select statement
it does not group the results , can someone help me out.what i want to achieve is the result of the above select statement in the select statement below;

select firstname, lastname, date,avg(grades)
from duplicate2
group by firstname, lastname,date
Post #854128
Posted Tuesday, January 26, 2010 10:10 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 1:10 PM
Points: 20,485, Visits: 14,141
Please provide sample data and what the desired output should look like. This will facilitate the process in helping you get a correct answer.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #854134
Posted Tuesday, January 26, 2010 10:17 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, March 19, 2013 2:21 PM
Points: 116, Visits: 160
here is sample data;

1 Nicholas Edet 80 2009-12-22 00:00:00.000
2 Nicholas Edet 95 2009-12-22 00:00:00.000
3 Nicholas Edet 90 2009-12-22 00:00:00.000
4 Emanuella Edet 76 2009-12-22 00:00:00.000
5 Emanuella Edet 80 2009-12-22 00:00:00.000
6 Emanuella Edet 97 2009-12-22 00:00:00.000
7 Emanuella Edet 76 2009-12-20 00:00:00.000
8 Nicholas Edet 95 2009-12-20 00:00:00.000
9 jackson Devo 70 2009-12-21 00:00:00.000
10 Sarah Palin 79 2009-12-21 00:00:00.000


then i want the results to be like this with all the columns included;

jackson Devo 70
Emanuella Edet 82
Nicholas Edet 90
Sarah Palin 79

any help is welcome and i appreciate it guys thank you
Post #854136
Posted Tuesday, January 26, 2010 10:33 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 1:10 PM
Points: 20,485, Visits: 14,141
And how would you like the date column handled? The date column does not really go well with your proposed output and requirements. Which date should one use to correlate to the average grade?



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #854151
Posted Tuesday, January 26, 2010 10:40 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, March 19, 2013 2:21 PM
Points: 116, Visits: 160
it doesnt matter as long as the result includes all the columns, you can exclude the id column because i dont know if its possible to include that in the result set
Post #854153
Posted Tuesday, January 26, 2010 10:43 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, January 18, 2012 7:43 AM
Points: 350, Visits: 406
Thats what Jason is asking.

Do you want the date column in the result set? If yes, what do you want the output to look like?
If you exclude id and date, then it looks like the SQL you have should work.



Post #854154
Posted Wednesday, January 27, 2010 12:10 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 1:10 PM
Points: 20,485, Visits: 14,141
vstitte (1/26/2010)
Thats what Jason is asking.

Do you want the date column in the result set? If yes, what do you want the output to look like?
If you exclude id and date, then it looks like the SQL you have should work.


Precisely. If you try to include the date column then you must also add it to the group by clause which will change your result set. However, if any date will work - then there are options available.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #854193
Posted Wednesday, January 27, 2010 5:24 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, December 03, 2013 1:16 AM
Points: 957, Visits: 62

I have tried this & it works well.....
when firstname,lastname and date are same then
definately it groups the grades and give the avearage result.

select firstname, lastname, date,avg(grades)
from duplicate2
group by firstname, lastname,date
Post #854304
Posted Thursday, January 19, 2012 8:16 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, July 19, 2013 11:48 AM
Points: 125, Visits: 235
I have the same *general* problem with a slight twist. In KlineandKing's scenario the name needed to be listed only once for the sum(grade).
In my case there are duplicate ssns in my table, and the fnames and lnames for each occurence of ssn are different and I need to have them listed in the output next to each ssn that has been identified as a duplicate. Please help.
---create table
CREATE TABLE [dbo].[employee](
[empid] [int] IDENTITY(1,1) NOT NULL,
[fname] [varchar](30) NULL,
[lname] [varchar](30) NULL,
[mngrid] [int] NULL,
[ssn] [int] NOT NULL)

---insert data
insert into employee
(fname, lname, mngrid, ssn)
values
('Larry', 'Bechold', 5, 213456123),
('Barry', 'Young', 5, 213456123),
('Rob', 'Sinclaire', 4, 212671777),
('Laban', 'Meese', NULL, 212671777),
('Sarge', 'Lutvok', NULL, 523656667)

---retrieve only rows containing duplicate ssns
select ssn, count(ssn) as countDuplicateSSN
from employee
group by ssn
having COUNT(*)>1;

---results
ssn countDuplicateSSN
212671777 2
213456123 2

I want output to look like this:
Barry Young 212671777
Laban Meese 212671777
Larry Bechold 213456123
Barry Young 213456123


-
Post #1239181
Posted Thursday, January 19, 2012 8:53 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 10:52 AM
Points: 4,252, Visits: 3,335
aitchkcandoo (1/19/2012)
I have the same *general* problem with a slight twist. In KlineandKing's scenario the name needed to be listed only once for the sum(grade).
In my case there are duplicate ssns in my table, and the fnames and lnames for each occurence of ssn are different and I need to have them listed in the output next to each ssn that has been identified as a duplicate. Please help.
---create table
CREATE TABLE [dbo].[employee](
[empid] [int] IDENTITY(1,1) NOT NULL,
[fname] [varchar](30) NULL,
[lname] [varchar](30) NULL,
[mngrid] [int] NULL,
[ssn] [int] NOT NULL)

---insert data
insert into employee
(fname, lname, mngrid, ssn)
values
('Larry', 'Bechold', 5, 213456123),
('Barry', 'Young', 5, 213456123),
('Rob', 'Sinclaire', 4, 212671777),
('Laban', 'Meese', NULL, 212671777),
('Sarge', 'Lutvok', NULL, 523656667)

---retrieve only rows containing duplicate ssns
select ssn, count(ssn) as countDuplicateSSN
from employee
group by ssn
having COUNT(*)>1;

---results
ssn countDuplicateSSN
212671777 2
213456123 2

I want output to look like this:
Barry Young 212671777
Laban Meese 212671777
Larry Bechold 213456123
Barry Young 213456123

Something like this --

select fname
,lname
,ssn
from ( select fname
,lname
,ssn
,ROW_NUMBER() over (
partition by ssn
order by ssn
) rowNumber
from employee ) a
where a.rowNumber > 1

(I followed you naming convention...)
Post #1239188
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse