Solved

Sql Syntax Joins

Posted on 2008-10-23
4
269 Views
Last Modified: 2010-03-20
Hello, I was wondering how I can modify the sql query I have below so that instead of setting @StatCode manully each time It would select all of the StatCodes in a table and complete the operation below for each one.

The table and field which contains the StatCode values is StatSource.StatCode.  I tried to do this myself but was unable to work through it. Hopefully after I see it done one more time i will be able to do it myself!


Thanks!
Declare @StatCode int
Set @StatCode = 8
select COUNT(VisitorID) As Clicks, COUNT(Distinct(VisitorID)) As UniqueVisitors
from outboundtraffic 
where VisitorID in(Select VisitorID from InboundTraffic where StatCode = @StatCode)

Open in new window

0
Comment
Question by:grogo21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Assisted Solution

by:AielloJ
AielloJ earned 150 total points
ID: 22792285
You should be able to get what you want with simple table joins.  Since VisitorID exists in outboundtraffic and inboundtraffic you can join those two tables, and since StatCode exists in both inboundtraffic and StatSource you can join those tables.  Try the query below.

select
  COUNT(ot.VisitorID) As Clicks,
  COUNT(Distinct(ot.VisitorID)) As UniqueVisitors
from
  outboundtraffic ot
 inner join
  InboundTraffic it
 on
  ot.VisitorID = it.VisitorID
 inner join
  StatSource ss
 on
  it.StatCode = ss.StatCode
0
 
LVL 9

Expert Comment

by:jamesgu
ID: 22792336
select b.StatCode, COUNT(a.VisitorID) As Clicks, COUNT(Distinct(a.VisitorID)) As UniqueVisitors
from outboundtraffic a, InboundTraffic b, StatSource c
WHERE a.VisitorID = b.VisitorID
AND b.StatCode = c.StatCode
GROUP BY b.StatCode
0
 
LVL 9

Assisted Solution

by:jamesgu
jamesgu earned 150 total points
ID: 22792344
assume table schema is

outboundtraffic
      VisitorID
      
InboundTraffic
      VisitorID
      StatCode
      
StatSource
      StatCode

select b.StatCode, COUNT(a.VisitorID) As Clicks, COUNT(Distinct(a.VisitorID)) As UniqueVisitors
from outboundtraffic a, InboundTraffic b, StatSource c
WHERE a.VisitorID = b.VisitorID
AND b.StatCode = c.StatCode
GROUP BY b.StatCode

Open in new window

0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 200 total points
ID: 22792826
Need to be a bit careful with joins and so on so that the record count does not exceed the outbound record count as a result of joining to additional tables.... So test the joins with a straight select as you were doing before...


select  SS.statcode, COUNT(OB.VisitorID) As Clicks, COUNT(Distinct(OB.VisitorID)) As UniqueVisitors
from outboundtraffic OB
inner join (Select VisitorID, statcode from InboundTraffic group by visitorid, statcode) as IB on IB.visitorID = OB.visitorID
inner join StatSource SS on SS.statcode = IB.statcode
group by SS.statcode
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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