?
Solved

Sql Syntax Joins

Posted on 2008-10-23
4
Medium Priority
?
271 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 600 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 600 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 800 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

777 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