Solved

Tricky Group by with Sort?

Posted on 2008-06-13
5
169 Views
Last Modified: 2010-04-21
Hello-

I have the following tables:
Personnel
ListingStats
Listings

I need to create a report for my personnel for their listings that gives them a break down of the stats and source count under each. So for example:

Listing 1234
 Google.com  154
 Frontdoor.com  25
Listing 1256
 Google.com 133
 Frontdoor.com 25

The listingStats table can have the source defined from anywhere on the web, so I wont be able to know what that source is, but I need to group by the source as a whole and the count. Your help is greatly apprecited. I have included the table breakdown below and possible query?

Select viewdate, source from tblListingStats where ListingID in(Select ListingID from tblListings where PersonnelID = ListingagentID) Groupby source, viewdate order by ListingID


0
Comment
Question by:jfergy
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:frankytee
ID: 21784504
post all the relevant fields from each table. for eg
which table does PersonnelID and ListingagentID come from?
0
 

Author Comment

by:jfergy
ID: 21795634
tblListingStats: ID, ListingID, DateTime, Source
TblPersonnel: PersonnelID, FirstName, LastName
tblListings: ListingID, PersonnelID

PersonnelID to tblListings is a 1 to Many relationship
ListingID to tblListingStats is a 1 to many relationship

Thanks for the help!
0
 
LVL 19

Accepted Solution

by:
frankytee earned 110 total points
ID: 21799416
try
select p.PersonnelID, l.ListingID,s.Source,
count(s.ID) as SourceCount
from TblPersonnel p join tblListings l
on p.PersonnelID = l.PersonnelID
join tblListingStats s
on l.ListingID = s.ListingID
group by p.PersonnelID, l.ListingID,s.Source

if that doesn't work then post sample data of each table and your query result set
0
 

Author Closing Comment

by:jfergy
ID: 31467103
Thanks that was awesome!
0
 
LVL 19

Expert Comment

by:frankytee
ID: 21800455
thanks, you're welcome
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Designer 19 39
SQL Server 2012 r2 - Varible Table 3 23
Help creating a spatial object in SQL Server 4 20
performance query 4 19
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

785 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