Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / July 2008

Tip: Looking for answers? Try searching our database.

getting the aggregate from a grouping

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Janis Rough - 02 Jul 2008 22:17 GMT
I have this query where I can see the duplicate car_sightings because
they are grouped together.  However, what I want is the aggregate.
One record with the count of how many duplicate sightings for each
car, with the same mark, number and sighting date.      I tried one
column as
count(car_mark, car_number, car_date) as dupe_sightings and ORDER BY
dupe_sightings >1  but it wouldn't let me.
tia,
SELECT
distinct
l_e,
car_mark,
car_number,
location_city,
location_state,
sighting_date,
railroad,
destination_city,
destination_state

FROM

INTERNAL_CLM

GROUP BY

car_mark,
car_number,
sighting_date,
l_e,
location_city,
location_state,
sighting_date,
railroad,
destination_city,
destination_state

ORDER BY car_mark, car_number, sighting_date
;

-------------
I also tried a self-join, the count was right but it was on each
separate rows, I didn't get the aggregate rows totaled together
either.

SELECT
distinct
c.l_e,

c.car_mark,
c.car_number,
c.sighting_code,
c.location_city,
c.location_state,
c.sighting_date,
c.railroad,
c.destination_city,
c.destination_state

FROM INTERNAL_CLM as c
JOIN INTERNAL_CLM as d ON
c.car_mark = d.car_mark
AND c.car_number = d.car_number and
c.sighting_date = d.sighting_date and c.trip_id = d.trip_id

So I am missing something about aggregates.

thanks,
Roy Harvey (SQL Server MVP) - 02 Jul 2008 22:43 GMT
First, when using GROUP BY there is no reason to use DISTINCT.  Unless
you are grouping on columns not in the SELECT list it makes no
difference.  (If you ARE grouping on columns not in the SELECT list
you have bigger problems!)

Other than that if I understand your requirement it sounds like all
you need is to include COUNT(*) as a column of the output.

Roy Harvey
Beacon Falls, CT

>I have this query where I can see the duplicate car_sightings because
>they are grouped together.  However, what I want is the aggregate.
[quoted text clipped - 64 lines]
>
>thanks,
Tom Moreau - 02 Jul 2008 22:45 GMT
Try:

select
   car_mark,
   car_number,
   sighting_date
from
   INTERNAL_CLM
group by
   car_mark,
   car_number,
   sighting_date
having
   count 9*) > 1

Signature

  Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON   Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau

I have this query where I can see the duplicate car_sightings because
they are grouped together.  However, what I want is the aggregate.
One record with the count of how many duplicate sightings for each
car, with the same mark, number and sighting date.      I tried one
column as
count(car_mark, car_number, car_date) as dupe_sightings and ORDER BY
dupe_sightings >1  but it wouldn't let me.
tia,
SELECT
distinct
l_e,
car_mark,
car_number,
location_city,
location_state,
sighting_date,
railroad,
destination_city,
destination_state

FROM

INTERNAL_CLM

GROUP BY

car_mark,
car_number,
sighting_date,
l_e,
location_city,
location_state,
sighting_date,
railroad,
destination_city,
destination_state

ORDER BY car_mark, car_number, sighting_date
;

-------------
I also tried a self-join, the count was right but it was on each
separate rows, I didn't get the aggregate rows totaled together
either.

SELECT
distinct
c.l_e,

c.car_mark,
c.car_number,
c.sighting_code,
c.location_city,
c.location_state,
c.sighting_date,
c.railroad,
c.destination_city,
c.destination_state

FROM INTERNAL_CLM as c
JOIN INTERNAL_CLM as d ON
c.car_mark = d.car_mark
AND c.car_number = d.car_number and
c.sighting_date = d.sighting_date and c.trip_id = d.trip_id

So I am missing something about aggregates.

thanks,
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.