Solved

Sql Syntax Joins

Posted on 2008-10-23
4
264 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now