If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Aggregate Incorrect with INNER JOIN

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-02-10, 15:37
ozzii ozzii is offline
Registered User
 
Join Date: Mar 2007
Posts: 194
Aggregate Incorrect with INNER JOIN

I have the following two tables

tbl_tickets

id - INT Primary Key
ticket_id VARCHAR(80)
start_time DATETIME
end_time DATETIME
category VARCHAR(80)


tbl_ticket_category_type

ticket_id VARCHAR(80) Primary Key also references tbl_tickets on ticket_id
category_type VARCHAR(80)

I want to work out average transaction time on tbl_tickets where category_type != 'No Show'

I've tried the following but it doubles the records before working out the average:

Code:
SELECT avg(datediff(s, start_time, end_time)) 
FROM tbl_tickets t1
INNER JOIN tbl_ticket_category_type t2 ON t2.ticket_id = t1.ticket_id
WHERE t2.category_type != 'No Show'
Any one know why that is?

to illustrate what i mean is instead of doing an avg if you do a count as follows you will note the count is doubled simply by doing an inner join:

Code:
SELECT count(t1.id) 
FROM tbl_tickets t1
INNER JOIN tbl_ticket_category_type t2 ON t2.ticket_id = t1.ticket_id
WHERE t2.category_type != 'No Show'
Reply With Quote
  #2 (permalink)  
Old 09-02-10, 19:23
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,085
dump your rows of data, the answer is duplicated rows
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On